Page 1 of 1

Script to convert between text formats

Posted: 03 Mar 2009 19:04
by zer0
Hello guys,

Ever since scripting got readfile() and writefile() functions I was curious if it could convert between different text formats. However, I didn't need that to be done so I didn't ask. Cue present day, I need to convert a NFO file that contains a bunch of ASCII characters into a text file. Basically, the reason is that Notepad doesn't want to open particular NFO files correctly, but does if they are saved as TXTs. I tried the following line of code, but no success:

Code: Select all

writefile("<crlf>.txt", readfile("<crlf>,t"));
Also, the name needs to be preserved but just the extension changed. So if I have a file called file.nfo the output needs to be file.txt

Can someone, jacky hint hint ;), offer some assistance and tell me where I am going wrong?

Thanks very much :)

Re: Script to convert between text formats

Posted: 03 Mar 2009 23:00
by Muroph
<crlf> is interpreted as a new line.
what you want is <curitem>.
look here for more info.

i think this is what you want.

Code: Select all

writefile("<curpath>\<curbase>.txt", readfile("<curitem>"),,tu)
it will read a file (ASCII or whatever) and save as unicode with a txt extension.

Re: Script to convert between text formats

Posted: 03 Mar 2009 23:16
by ale
As a total beginner to scripting I was writing this too to learn, and had this line of code :)

Code: Select all

$filedata = readfile("<curbase>.nfo"); writefile("<curbase>.txt", $filedata);
or

Code: Select all

$filedata = readfile("<curbase>.nfo"); writefile("<curbase>.txt", $filedata, , tu);
to convert to Unicode

Re: Script to convert between text formats

Posted: 04 Mar 2009 01:51
by zer0
Thanks to both of you. While your code does write a text file, it does not do the same kind of conversion. When opened in Notepad the formatting that I would achieve if I was to open the original NFO in Wordpad and save as TXT file does not show in the converted file. I've attached the file in question and hope there is some remedy in this situation. My initial, and current, though is that the kind of writing that XYplorer does and the kind of "Save As" Wordpad does are entirely different even though both are asked to write a TXT file.

Re: Script to convert between text formats

Posted: 04 Mar 2009 04:01
by Muroph
zer0 wrote:My initial, and current, though is that the kind of writing that XYplorer does and the kind of "Save As" Wordpad does are entirely different even though both are asked to write a TXT file.
well, they are kind of different.
AFAIK, wordpad will write the txt file using the UTF-8 encoding, while the script wil use the UTF-16 encoding.
zer0 wrote:When opened in Notepad the formatting that I would achieve if I was to open the original NFO in Wordpad and save as TXT file does not show in the converted file.
if you try to open the nfo file on notepad you'll see the text without formatting as well.
my conclusion is that wordpad somehow fixes this.
but if you only want to read the file you can use one of xyplorer's previews.
or maybe the incredibly useful quick file edit script.

ale wrote:As a total beginner to scripting I was writing this too to learn, and had this line of code :)

Code: Select all

$filedata = readfile("<curbase>.nfo"); writefile("<curbase>.txt", $filedata);
or

Code: Select all

$filedata = readfile("<curbase>.nfo"); writefile("<curbase>.txt", $filedata, , tu);
to convert to Unicode
i'm no expert myself, but here are some tips:
1. you can't use <curbase>. you have to specify the whole path, and <curbase> only gives the file name without path or extension, so "<curitem>" (without .nfo) is the best choice for readfile.
it works on writefile as well, but the name of the converted file will be "file.nfo.txt". that's why i used "<curpath>\<curbase>".
2.the 1st script won't necessarily write a unicode file.
it will use ASCII encoding if possible.
the "tu" argument is what forces the script to use unicode.
3.since readfile is a function, you can put it directly inside the writefile command without having to use a variable.

Re: Script to convert between text formats

Posted: 04 Mar 2009 12:43
by ale
zer0 wrote:Thanks to both of you. While your code does write a text file, it does not do the same kind of conversion. When opened in Notepad the formatting that I would achieve if I was to open the original NFO in Wordpad and save as TXT file does not show in the converted file. I've attached the file in question and hope there is some remedy in this situation. My initial, and current, though is that the kind of writing that XYplorer does and the kind of "Save As" Wordpad does are entirely different even though both are asked to write a TXT file.
I think the problem with the file you posted may be related to line endings and it's not about Unicode, I tried to write a new script, may you test it? I hope it works better now

Code: Select all

regexreplace $data, readfile("<curitem>"), "\n", "<crlf>"; writefile("<curpath>\<curbase>.txt", "$data");

Re: Script to convert between text formats

Posted: 04 Mar 2009 13:11
by zer0
Muroph wrote:well, they are kind of different.
AFAIK, wordpad will write the txt file using the UTF-8 encoding, while the script wil use the UTF-16 encoding.
Muroph wrote:if you try to open the nfo file on notepad you'll see the text without formatting as well.
my conclusion is that wordpad somehow fixes this.
but if you only want to read the file you can use one of xyplorer's previews.
Yes, it seems that Wordpard is doing something that XYplorer isn't doing even though both should be doing the same thing. I wonder if it's the encoding that XY uses that is causing the problem. Hmmm...

Re: Script to convert between text formats

Posted: 04 Mar 2009 13:13
by ale
Muroph wrote:1. you can't use <curbase>. you have to specify the whole path, and <curbase> only gives the file name without path or extension, so "<curitem>" (without .nfo) is the best choice for readfile.
it works on writefile as well, but the name of the converted file will be "file.nfo.txt". that's why i used "<curpath>\<curbase>".
2.the 1st script won't necessarily write a unicode file.
it will use ASCII encoding if possible.
the "tu" argument is what forces the script to use unicode.
3.since readfile is a function, you can put it directly inside the writefile command without having to use a variable.
Thanks a lot for your tips,
About the first point, I see as <curitem> is more appropriate, because it will also work for any extension but may I ask why <curbase> can't be used? or maybe it's a question about relative paths more than about <curbase>. I will say what I thought so far... readfile and writefile with a simple file name will read and write that file from and to the current folder because they accept a file name relative to the current path and I thought that if a file is selected the current path is actually the folder where the file is as we can see with something like echo ("<curpath>");

Re: Script to convert between text formats

Posted: 04 Mar 2009 14:26
by Muroph
ale wrote:Thanks a lot for your tips,
About the first point, I see as <curitem> is more appropriate, because it will also work for any extension but may I ask why <curbase> can't be used? or maybe it's a question about relative paths more than about <curbase>. I will say what I thought so far... readfile and writefile with a simple file name will read and write that file from and to the current folder because they accept a file name relative to the current path and I thought that if a file is selected the current path is actually the folder where the file is as we can see with something like echo ("<curpath>");
actually, i've never used relative paths in xy.
i've had some nasty experiences with them in some other apps, so now i always use the full path. :x
but, you're right. :oops:
just use <curname> in readfile and it should work.

Re: Script to convert between text formats

Posted: 04 Mar 2009 20:33
by zer0
ale wrote:I think the problem with the file you posted may be related to line endings and it's not about Unicode, I tried to write a new script, may you test it? I hope it works better now

Code: Select all

regexreplace $data, readfile("<curitem>"), "\n", "<crlf>"; writefile("<curpath>\<curbase>.txt", "$data");
Yep, your script works just right, thank you very much 8)