All Forums >> [Scripting] >> WSH & Client Side VBScript >> Script to replace a value in a text file then save a copy Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Hey all, I would like to create a script that edits a value in a text file, then saves a copy of the file.
I have been looking at example scripts and trying to get mine to work with mixed success, I have succeeded to open a file, read it, and then write to another text file, however my original text gets lost in translation and becomes garbled
This is an example of the code I am using:
I figured before I started trying to replace text I would try to copy the file over word for word,, using the .Readall command it would copy the data as is but using .Readline somehow messes up the text.
I even tried to change the source file so that it contains only 3 lines of plain text:
Source contents:
Text Goes Here
After the above script is run it creates the file if it does not exist, and then the text looks like this:
ÿþT e x t
G o e s
H e r e
Everything is spaced out and the first line of the text output always says ÿþ...
< Message edited by chrismerritt -- 8/11/2008 7:32:18 AM >
Your problem lied in the fact that both files were using different encoding. This could be a difference in codepages between the two machines. Manually setting the encoding prevents this problem when working with files from different systems.
Also, it's generally a good idea to avoid the ReadAll method unless you are working with very small text files. Reading the entire contents of a large text file into the memory buffer can wreak havoc. To be safe, just work line by line as you were before and wrap your Replace function around that line. That is, of course, unles you're replacing something that spans more than one line.