CreateTextfile() > write does not work - windows

VBScript does delivers an "Illegal Argument" message when trying to write the text shown below to file using the following code. If I change resultStr to some test text, it works. What could be the problem?
Set resFile = fs.CreateTextfile(resFilePath, true)
resFile.write resultStr
resFile.close
Contents of resultStr:

Your string looks like it contains non-ASCII characters. You need to pass an extra True argument to CreateTextfile to open the text file using a Unicode encoding (probably UTF-16 on Windows).
If you want to write UTF-8 to the file, see Writing UTF8 text to file.

Related

CMD: clip command issue

I have found a weird issue using the clip command of Widows CMD.
I created a simple text file containing this text:
^.*A{0,0}.*$
Then I ran the command clip < PATH_TO_THE_TEXT_FILE in CMD.
Finally, I tried pasting the copied text into text editors such as Notepad and Notepad++, and what I got was some weird Japanese characters. This issue can be reproduced every time and on different PCs.
Can you please tell me what is causing this issue and how can I make the clip command copy the actual text in the text file, and not the weird Japanese characters?
what is causing this
Incorrect encoding conversion, ASCII String -> bytes -> UTF-16 String
// JavaScript code to emulate this
bytes = new TextEncoder("UTF-8").encode( '^.*A{0,0}.*$' )
new TextDecoder("UTF-16LE").decode( bytes )
how can I make the clip command copy the actual text
type PATH_TO_THE_TEXT_FILE | clip
Sorry, update the answer.
Update: clip works if there is a newline in the end of the file.
Based on what 7cc said, I figued out how to make it work.
I need to create the text file using the encoding UTF-16LE, and then it works.

Convert text to ascii code using DOS

I came here looking for going the other way (opposite to the way posted in
echo -e equivalent in Windows?):
I have a text file with ASCII chars, to be converted to a text file with ASCII equivalent of the characters in the text file.
eg. a text file containing "--" to be changed to a text file containing "4545".
Anyone got dos code for this?
Thanks,
For Microsoft QBasic
dim character$(1):dim number%
open "inputfile" for Binary as 1
open "outputfile" for Binary as 2
while not eof(1)
get #1,,character$:put #2,,asc(character$)
'here one byte string is converted into character number
wend
close

Why is \r\n being converted to \n when a string is saved to a file?

The string is originating as a return value from:
> msg = imap.uid_fetch(uid, ["RFC822"])[0].attr["RFC822"]
In the console if I type msg, a long string is displayed with double quotes and \r\n separating each line:
> msg
"Delivered-To: email#test.com\r\nReceived: by xx.xx.xx.xx with SMTP id;\r\n"
If I match part of it with a regex, the return value has \r\n:
> msg[/Delivered-To:.*?\s+Received:/i]
=> "Delivered-To: email#test.com\r\nReceived:"
If I save the string to a file, read it back in and match it with the same regex, I get \n instead of \r\n:
> File.write('test.txt', msg)
> str = File.read('test.txt')
> str[/Delivered-To:.*?\s+Received:/i]
=> "Delivered-To: email#test.com\nReceived:"
Is \r\n being converted to \n when the string is saved to a file?
Is there a way to save the string to a file, read it back in without the line endings being modified?
This is covered in the IO.new documentation:
The following modes must be used separately, and along with one or more of the modes seen above.
"b" Binary file mode
Suppresses EOL <-> CRLF conversion on Windows. And
sets external encoding to ASCII-8BIT unless explicitly
specified.
"t" Text file mode
In other words, Ruby, like many other languages, senses the OS it's on and will automatically translate line-ends between "\r\n" <-> "\n" when reading/writing a file in text mode. Use binary mode to avoid translation.
str = File.read('test.txt')
A better practice would be to read the file using foreach, which negates the need to even care about line-endings; You'll get each line separately. An alternate is to use readlines, however it uses slurping which can be very costly on large files.
Also, if you're processing mail files, I'd strongly recommend using something written to do so rather than write your own. The Mail gem is one such package that's pre-built and well tested.

Incompatible character encodings error

I'm trying to run a ruby script which generates translated HTML files from a JSON file. However I get this error:
incompatible character encodings: UTF-8 and CP850
Ruby
translation_hash = JSON.parse(File.read('translation_master.json').force_encoding("ISO-8859-1").encode("utf-8", replace: nil))
It seems to get stuck on this line of the JSON:
Json
"3": "Klassisch geschnittene Anzüge",
because there is a special character "ü". The JSON file's encoding is ANSI. Any ideas what could be wrong?
Try adding # encoding: UTF-8 to the top of the ruby file. This tells ruby to interpret the file with a different encoding. If this doesn't work try to find out what kind of encoding the text uses and change the line accordingly.
IMHO your code should work if the encoding of the json file is "ISO-8859-1" and if it is a valid json file.
So you should first verify if "ISO-8859-1" is the correct encoding and
by the way if the file is a valid json file.
# read the file with the encoding, you assume it is correct
json_or_not = File.read('translation_master.json').force_encoding("ISO-8859-1")
# print result and ckeck if something is obscure
puts json_or_not

Is it possible to specify newline type while reading a file in ruby

I frequently deal with UTF-16LE files encoded on windows which have a \r\n carriage return. There is no problem converting the file to UTF-8 by using:
File.new(filepath, 'r:utf-16le:utf-8')
But this of course does not get rid of the \r. The way I currently get rid of them is with
str.gsub("\r", "")
But it would be nice to take care of it while reading the file in. String#encode has :cr_newline, :crlf_newline, and :universal_newline options which convert all newlines to a desired kind of newline. Is there a way to apply these or similar options while reading in a file?
The method IO#gets takes an optional argument that allows you to pass a string to define how to separate the lines:
file = File.new(filepath, 'r:utf-16le:utf-8')
while (line = file.gets("\r\n"))
...
end

Resources