Forums > Javascript

is-this-path-a-file-or-folder test in js?


Mar 21 2006 | 11:03 pm

hello,

how does one test a path (coming in as a string argument to some method of the js) is a file or a folder?

perhaps there’s a very simple way but my attempts with the filetype or pathname methods of a Folder object don’t seem to do the trick…

thanks,
a

Mar 21 2006 | 11:10 pm

I would suggest you try to open as folder. If not successful, try to
open as file.

-Joshua

Mar 21 2006 | 11:12 pm

yes, i was tryin that but i wasn’t sure hot to test the "success" of an open-as-folder attempt.
a hint?

a

Mar 21 2006 | 11:21 pm

On Mar 21, 2006, at 3:12 PM, Ali Momeni wrote:

>
> yes, i was tryin that but i wasn’t sure hot to test the "success"
> of an open-as-folder attempt.
> a hint?

Okay, I see that there is no good test for success w/ Folder. You
could try the other way, try to open as a file and check the
File.isopen property.

-Joshua

Mar 21 2006 | 11:41 pm

On Mar 21, 2006, at 6:21 PM, Joshua Kit Clayton wrote:

>
> On Mar 21, 2006, at 3:12 PM, Ali Momeni wrote:
>
>>
>> yes, i was tryin that but i wasn’t sure hot to test the "success"
>> of an open-as-folder attempt.
>> a hint?
>
> Okay, I see that there is no good test for success w/ Folder. You
> could try the other way, try to open as a file and check the
> File.isopen property.

Another way to is to make a system call and do "rm

". If
the thing you’re testing gets deleted, then it was a file.

– John

Mar 21 2006 | 11:47 pm

how would you make a system call and
would that "rm" work on xp?

Mar 21 2006 | 11:54 pm

Methinks that was a joke, no? unless of course you want to delete the
file you are trying to use.

wes

Mar 21 2006 | 11:55 pm

On Mar 21, 2006, at 3:47 PM, Ali Momeni wrote:

>
> how would you make a system call and
> would that "rm" work on xp?

Obviously a ridiculous "solution", but you can make system calls with
Java relatively easily. Might need to execute different system call
on XP than OS X, of course.

http://www.maxobjects.com/?request=shell

-Joshua

Mar 21 2006 | 11:57 pm

i know you can make system calls easily with Java,
but we’re talkin about js….
similar?
?

Mar 22 2006 | 12:09 am

On Mar 21, 2006, at 3:57 PM, Ali Momeni wrote:

> i know you can make system calls easily with Java,
> but we’re talkin about js….
> similar?

Nope. Gotta use java or C for that.

-Joshua

Mar 22 2006 | 12:12 am

Testing if something is a file or a folder is really easy in JAVA. I
would just create a tiny mxj utility for doing this.

wes

Mar 22 2006 | 12:17 am

thanks all.
turns out, joshua’s 2nd suggestion of attempting to open as a File and test the isopen property works perfectly.

thanks,

a

Mar 22 2006 | 1:03 pm

John Nowak wrote:
> Another way to is to make a system call and do "rm

". If
> the thing you’re testing gets deleted, then it was a file.

Whenever I do this, I wonder why my clients never came back to me…;-)

Stefan

[][] [][][] [][] [][][]
[][][][][][][][][][][][][][][]

Stefan Tiedje
Klanggestalter
Electronic Composition
&
Improvisation

/~~~~~
\ /|() ()|
))))) )| | |( \
/// _/)/ )))))
___/ ///

————————-x—-
–_____———–|———–
–(_|_ —-|—–|—–()—-
— _|_)—-|—–()———–
———-()————x—–

14, Av. Pr. Franklin Roosevelt,
94320 Thiais, France
Phone at CCMIX +33-1-57 42 91 09

Feb 20 2010 | 12:36 pm

I’m trying to implement Joshua’s solution, testing if a file is open to see if it exists. I’m half-way there. If the file DOES exist, changing the access to "readwrite", then writeline doesn’t seem to do anything!

var foo = new File("foolio.txt");
if(!foo.isopen)
{
post("File foolio.txt doesn’t exist.n");
foo = new File("foolio.txt","readwrite");
foo.writeline("New file created here today!");
}
else
{
post("File exists.n");
foo.access = "readwrite";
foo.writeline("Overwriting stuff.");
foo.eof = foo.position;

}
foo.close();

=======================

any suggestions?

Feb 20 2010 | 12:36 pm

The line "Overwtiing stuff." never gets written to the file…

Mar 08 2010 | 6:33 pm

Hey, sorry the reply is a bit late, but in case you haven’t solved this yet try the code below. You need to access the file object without creating or opening anything at the beginning and then it should work.

lh


// filefinder.js

var target = this.patcher.filepath.slice(0,this.patcher.filepath.lastIndexOf("/")+1)+"foolio.txt";
var foo = new File();

function bang() {
    foo.open();
    if(!foo.isopen) {
        post("File foolio.txt doesn't exist.n");
        foo = new File(target,"readwrite");
        foo.writeline("New file created here today!");
    } else {
        post("File exists.n");
        foo.position = foo.eof;
        foo.writeline("Overwriting stuff.");
    }
    foo.close();
}

autowatch = 1;

// EOF
Viewing 16 posts - 1 through 16 (of 16 total)

Forums > Javascript