Copy link to clipboard
Copied
Hi.
Am am trying to access an external txt file which holds data for my script. I have looked at the famous "FindChangeByList" and tried to work from there. However, I am not going to need input from the user for the txt file location, it will always be the same:
myFilePath="/Mailer Support/savers.txt";//always going to be there
myFile = File(myFilePath);
myData = myFile.open("r", undefined,undefined);
myLine = myFile.readln(); //I know I need loops for this, but there is text on the first line of the TXT file
myText = myFile.read();
alert (myLine);//Blank!! myText = myFile.read(); alert (myText);//Blank also
Can someone help with this please.
I know I had a PDF file that detailed the way to call and work with files, but I cannot find it. Does someone know the name of it?
Cheers
Roy
Copy link to clipboard
Copied
I have now found the file "Javascript Tools Guide". I will look through in tomorrow to see if i can answer this one myself now.
Feel free to reply anyway...!!
Cheers
Roy
Copy link to clipboard
Copied
Your code works just fine for me. I'd suggest adding
myStatus = myFile.open("r", undefined,undefined);
if (myStatus == false) { alert ("Unable to open that file"); exit(0); }
to check if it actually exists (open returns a state flag, rather than 'data'). A quick experiment shows that JS doesn't care if a non-existant file is opened for reading (other than just about any other programming language I could care to name!), and indeed "reads" blank lines if it doesn't exist.
By the way, you used this line twice:
myText = myFile.read();
-- overwriting "myText" the second time. Since the first "read" already read up to the end of the file, the second time around the variable is emptied.
Copy link to clipboard
Copied
I think if(!myFile.exists){//exit} is a more elegent way of checking if the file exists... ![]()
Harbs
Copy link to clipboard
Copied
Nice thinking, but the txt file only contains two lines of text. No blank lines. (I checked that first). I have also checked the file name etc..... and all is ok.
I will get back to this later. No time now with my day job. ![]()
Roy
Copy link to clipboard
Copied
YEH!
I was acting on the information that the path myFilePath="/XXXX Support/savers.txt"; starts in the location of the active script. Not So. This actually takes me back to the root of the hard disk. No problems, just a lot of wasted time yesterday!
Thanks for the help, having the ability to confirm the existence of the file lead me there.
Thanks all once again
Roy
Copy link to clipboard
Copied
Maybe the first line is blank?
I usually just use read(), and then parse the text...
Harbs
Copy link to clipboard
Copied
Heh heh -- yes, that's also possible! First line blank, rest of the text gets read but then got thrown away by the next "read" ...
I bet I would have been flummoxed as well if my test file happened to start with a blank line.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more