Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Script to replace a value in a text file then save a copy

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,63312
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
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!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Script to replace a value in a text file then save a copy - 8/11/2008 7:23:32 AM   
  chrismerritt

 

Posts: 2
Score: 0
Joined: 8/11/2008
Status: offline
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 >
 
 
Post #: 1
 
 RE: Script to replace a value in a text file then save ... - 8/11/2008 8:33:04 AM   
  chrismerritt

 

Posts: 2
Score: 0
Joined: 8/11/2008
Status: offline
OK I figured out why it was broken, it was not the code

apparently the .txt file I copied over from a Windows XP machine did not function well in Vista

creating the original source file again (copying and pasting the text) worked like charm!

(in reply to chrismerritt)
 
 
Post #: 2
 
 RE: Script to replace a value in a text file then save ... - 8/11/2008 8:46:57 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Actualy your code just copy the content verbatim. It doesn't replace anything.

A few suggestions:

insyead of


      

do


      

Close PRFFile too.


Similar problem here

[quote="dm4ever and Gigital.sKream"]Try opening it as a unicode file. [/quote]


      

_____________________________

Fred

(in reply to chrismerritt)
 
 
Post #: 3
 
 RE: Script to replace a value in a text file then save ... - 8/12/2008 1:51:30 AM   
  nilpo1

 

Posts: 21
Score: 0
Joined: 1/22/2006
Status: offline
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.



_____________________________

Scripting problems? Windows questions? Ask the Windows Guru!

(in reply to Fredledingue)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Script to replace a value in a text file then save a copy Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts