Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[JS][CS3] Loading text into memory

Participant ,
Jan 25, 2010 Jan 25, 2010

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

TOPICS
Scripting
803
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 25, 2010 Jan 25, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2010 Jan 25, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 25, 2010 Jan 25, 2010

I think if(!myFile.exists){//exit} is a more elegent way of checking if the file exists...

Harbs

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 25, 2010 Jan 25, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 25, 2010 Jan 25, 2010
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 25, 2010 Jan 25, 2010

Maybe the first line is blank?

I usually just use read(), and then parse the text...

Harbs

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2010 Jan 25, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines