Page 1 of 1

file Object

Posted: January 3rd, 2011, 5:04 pm
by philspitler
Hi,
I have been using a dialog box to open a file and read from it.

var f=File.openDialog("select a file");
var ok=f.open("r");

This works great but now I want to have the filename in a variable instead of using a dialog box.

I thought it would be something like this below,

var f=File.open("test.txt");
var ok=f.open("r");

But it's not right.

Looking through docs, I cannot seem to find the correct syntax.

Thanks.

Phil

Re: file Object

Posted: January 3rd, 2011, 7:14 pm
by Dan Ebberts
You have to specify the whole path:

var myFilePath = "C:/Temp";
var myFileName = "test.txt";

var myFile = new File(myFilePath + "/" + myFileName);
var fileOK = myFile.open("r");


Dan

Re: file Object

Posted: January 3rd, 2011, 7:27 pm
by philspitler
Yep, I just put the test.txt in there as an example.

It was the "new" that I was missing.

Thanks once again Dan.

Phil

Re: file Object

Posted: January 11th, 2011, 12:23 pm
by lloydalvarez
btw, the difference between adding the new and not is that if you have new File and the file does not exist then it will create a new file object if you don't add the new and the file doesn't exist you will get an error.

-Lloyd