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

File.read() returns nothing for .txt file

Community Beginner ,
Aug 23, 2017 Aug 23, 2017

Hi,

I'm searching a way to read (and subsequently edit) the contents of a text file on my disk. I thought it works with the File.read() function but when I run the following I get nothing from the file. How does it work? How do I get the string with the file content? I tried different functions like readln() etc but with no luck

var testtextfile = File("/c/users/bernhard-büro/desktop/text.txt");

testtextfile.open("r", undefined, undefined);

//also tried with .open("r"); and .open('r');

var fileContentsString = testtextfile.read();

   

alert("Full path: "+testtextfile.fsName+"\r\n"+
      "File content:"+fileContentsString+"\r\n"+
      "fileContentsString length:"+fileContentsString.length);
    
testtextfile.close();

this is the result: (and what is the proper way to format code in this forum?)

alertmessage.PNG

Thank you very much!
Cheers

TOPICS
Scripting
7.4K
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

correct answers 1 Correct answer

Guru , Aug 23, 2017 Aug 23, 2017

Often the cause is encoding related.

Try

var testtextfile = File("/c/users/bernhard-büro/desktop/text.txt");

testtextfile.encoding = 'UTF8'; // set to 'UTF8' or 'UTF-8'

testtextfile.open("r");

var fileContentsString = testtextfile.read();

testtextfile.close();

alert("Full path: " + testtextfile.fsName + "\r\n" +

    "File content:" + fileContentsString + "\r\n" +

    "fileContentsString length:" + fileContentsString.length);

To format the code you go to advanced editor

and then

2017-08-23_17-05-45.png

HTH

Trevor

Translate
Guide ,
Aug 23, 2017 Aug 23, 2017

Please try this..

var testtextfile = new File("/c/users/bernhard-büro/desktop/text.txt");

testtextfile.open('r');

fileContentsString = testtextfile.readln();

alert("File content:"+fileContentsString);

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
Guru ,
Aug 23, 2017 Aug 23, 2017

Often the cause is encoding related.

Try

var testtextfile = File("/c/users/bernhard-büro/desktop/text.txt");

testtextfile.encoding = 'UTF8'; // set to 'UTF8' or 'UTF-8'

testtextfile.open("r");

var fileContentsString = testtextfile.read();

testtextfile.close();

alert("Full path: " + testtextfile.fsName + "\r\n" +

    "File content:" + fileContentsString + "\r\n" +

    "fileContentsString length:" + fileContentsString.length);

To format the code you go to advanced editor

and then

2017-08-23_17-05-45.png

HTH

Trevor

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 Beginner ,
Aug 28, 2017 Aug 28, 2017

Hi Trevor, thanks a lot for that. I still have to find out what exactly is going on in detail about the character encoding on my system (changing it doesn't seem to be necessary in most cases) but now I can parse the text out of my file, great! Kind regards, Stefan

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
Guru ,
Aug 28, 2017 Aug 28, 2017

Hi Stefan

The need for the encoding is dependent on the contents of the file.

If the files contains just asci then you shouldn't need to set it. ü and most of the diacritics are asci extended and also shouldn't need setting but if the text contains Unicode characters you are generally going to need to set it.

Trevor  

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 ,
Aug 28, 2017 Aug 28, 2017

So it's safer always to set the encoding to UTF-8.

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 ,
Aug 23, 2017 Aug 23, 2017

Apart from the encoding, another problem might be the u+umlaut in your filename. If Trevor's suggestion doesn't help, rename the file so that its name doesn't have any accented characters, then try again.

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 ,
Aug 23, 2017 Aug 23, 2017

Hi Peter,

about the u umlaut problem:
I've seen this on Mac OS X where a ü in a file name can be composed out of:

u\u0308

Could that happen on Windows as well?

Regards,
Uwe

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 ,
Aug 23, 2017 Aug 23, 2017

> Could that happen on Windows as well?

No, it doesn't. One of the few areas where Windows behaves better than the Mac OSs

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
People's Champ ,
Aug 25, 2017 Aug 25, 2017

Hi

I second that. I discovered that recently while resolving image files instance on Mac Os. I got a textual reference with a diacritic such as "marché.jpg" and the file couldn't be found to my surprise. I coudl see the file but using File ("marché.jpg").exists would return false.

Fact is Mac os was using "marche[diacritic].jpg"

Painful indeed.

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
Guru ,
Aug 28, 2017 Aug 28, 2017

Unless you need to use UTF-16 like I did yesterday 😉

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 ,
Aug 28, 2017 Aug 28, 2017
LATEST

Fair point!

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 ,
Aug 23, 2017 Aug 23, 2017

If that was indeed the case: A very basic check to see if the file is opened succesfully would have shown that.

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 Beginner ,
Aug 28, 2017 Aug 28, 2017

Hi Peter, thanks for your answer. Taking the umlaut out of my filepath did not help. However, I still think it might be part of the problem because why else did changing the encoding setting help when usually it works well without? But this is a wild guess, I will still look into it and post it here if I can find out something relevant. regards

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