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

Mar 21, 2006 at 11:03pm

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

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

#25006
Mar 21, 2006 at 11:10pm

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

-Joshua

#73010
Mar 21, 2006 at 11:12pm

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

a

#73011
Mar 21, 2006 at 11:21pm

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

#73012
Mar 21, 2006 at 11:41pm

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

#73013
Mar 21, 2006 at 11:47pm

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

#73014
Mar 21, 2006 at 11:54pm

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

wes

#73015
Mar 21, 2006 at 11:55pm

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

#73016
Mar 21, 2006 at 11:57pm

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

#73017
Mar 22, 2006 at 12:09am

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

#73018
Mar 22, 2006 at 12:12am

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

#73019
Mar 22, 2006 at 12:17am

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

thanks,

a

#73020
Mar 22, 2006 at 1:03pm

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

#73021
Feb 20, 2010 at 12:36pm

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?

#73022
Feb 20, 2010 at 12:36pm

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

#73023
Mar 8, 2010 at 6:33pm

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
#73024

You must be logged in to reply to this topic.