Script to convert between text formats

Discuss and share scripts and script files...
Post Reply
zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Script to convert between text formats

Post 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 :)
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: Script to convert between text formats

Post 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.
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

ale
Posts: 127
Joined: 19 Feb 2009 23:23
Location: Italy

Re: Script to convert between text formats

Post 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

zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Re: Script to convert between text formats

Post 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.
Attachments
test.zip
(694 Bytes) Downloaded 131 times
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: Script to convert between text formats

Post 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.
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

ale
Posts: 127
Joined: 19 Feb 2009 23:23
Location: Italy

Re: Script to convert between text formats

Post 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");

zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Re: Script to convert between text formats

Post 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...
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

ale
Posts: 127
Joined: 19 Feb 2009 23:23
Location: Italy

Re: Script to convert between text formats

Post 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>");

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: Script to convert between text formats

Post 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.
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Re: Script to convert between text formats

Post 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)
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

Post Reply