Help! VBScript text file read/write adding garbage on Japanese windows...
I have a very basic VBScript that's launched from our WISE Installer that does a search and replace on a text file on the user's system. The script works fine on US Windows, but for some reason on Japanese Windows, it inserts hex "81 00 45 00 BF 00" before the 3rd byte at the beginning of the first line in the file. I've searched the net and seen issues regarding reading and writing binary, but nothing about text...
The main code is thus:
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
LocaleLCID = WshShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\Locale")function UpdateVersionNumbers(sFileName, SearchText, ReplaceText)
Const ForReading = 1
Const ForWriting = 2
Set objFile = FileSystemObject.OpenTextFile(sFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, SearchText, ReplaceText)
Set objFile = FileSystemObject.OpenTextFile(sFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
end functionQ/A found this at the last minute so I'm under the gun to fix it ASAP - any help folks could give would be most appreciated!
[3463 byte] By [
StickyC] at [2007-12-24]
Hi StickyC,
Try replacing your...
objFile.WriteLine strNewText
with...
objFile.WriteLine(1, strNewText)
I hope this solves your problem.
Thank you,
James
Actually, it turns out the solution was to set the unicode format bit in the OpenTextFile call. The new code looks like:
function UpdateVersionNumbers(sFileName, SearchText, ReplaceText)
Const ForReading = 1
Const ForWriting = 2
Const Unicode = -1
Set objFile = FileSystemObject.OpenTextFile(sFileName, ForReading, Unicode)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, SearchText, ReplaceText)
Set objFile = FileSystemObject.OpenTextFile(sFileName, ForWriting, Unicode)
objFile.WriteLine strNewText
objFile.Close
end function