Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using vbscript to open file and replace text

Status
Not open for further replies.

wharp

Programmer
Jul 9, 2003
27
US
I'm trying to use a combination of scripts and batch files to automate some tasks I do when setting up computers. I need to open a specific file in notepad, search for some text, and when it finds that text, replace it with something else. I'm having trouble getting the vbscript file to interact with notepad. I have a batch file that runs my .vbs file and .bat file. I'm using sendkeys to access notepads menu and select the replace option and then enter the find text and replace text. My knowledge of vbscript is very limited so I could be heading in the wrong direction entirely. What I have so far is:

set WshShell = CreateObject("WScript.Shell")
WScript.Sleep 500
WshShell.SendKeys "^h" 'Shortcut for Replace
WshShell.SendKeys "search text" 'The text I want to search for
WshShell.SendKeys "{Tab}"
WshShell.SendKeys "Replace text" 'The text I want to enter
WshShell.SendKeys "{Enter}"


I can't get the vbscript to actually send the keystrokes to notepad. What am I doing wrong? Does anybody have any tips on this or a better way to accomplish this?
 
If you use the FSO and read in the file then you can just use

Set FSO = CreateObject("Scripting.FileSystemObject")
Set tempOBJ = FSO.OpenTextFile("FILE TO READ IN", ForReading)
FileInfo = tempOBJ.ReadAll
Replace(FileInfo, "search text", "Replace text")
Set tempOBJ = FSO.CreateTextFile("DestinationFile", ForWriting)
tempOBJ.Writeline FileInfo
tempOBJ.Close
Set tempOBJ = Nothing
Set FSO = Nothing
 
That code gives me an error of "Cannot Use Parenthesis When Calling a Sub" on line 4.
 
Add this to the script

Const ForReading = 1
Const ForWriting = 2
 
I can't get that to work either. If I change the line to " Call Replace(FileInfo, "search text", "Replace text")" then it runs with no error and creates the output file, however, it does not replace the text.
 
Try this:
Code:
Set FSO=CreateObject("Scripting.FileSystemObject")
Set TF=FSO.OpenTextFile("YourFile",1) ' 1=ForReading
inp=TF.ReadAll
out=Replace(inp,"search text","Replace text")
TF.close
Set TF=FSO.CreateTextFile("YourFile",True)
TF.Writeline out
TF.Close

Hope This Help
PH.
 
Thanks, that works great! Can it be modified to replace 2 things? I tried this, but it doesn't work.

Set FSO=CreateObject("Scripting.FileSystemObject")
Set TF=FSO.OpenTextFile("YourFile",1) ' 1=ForReading
inp=TF.ReadAll
out1=Replace(inp,"search text1","Replace text1")
out2=Replace(inp,"search text2","Replace text2")
TF.close
Set TF=FSO.CreateTextFile("YourFile",True)
TF.Writeline out1
TF.Writeline out2
TF.Close

I would assume that I would need to replace the first, write it back to tf and then replace the second?
 
Try this:
Code:
Set FSO=CreateObject("Scripting.FileSystemObject")
Set TF=FSO.OpenTextFile("YourFile",1) ' 1=ForReading
inp=TF.ReadAll
out1=Replace(inp,"search text1","Replace text1")
out2=Replace(out1,"search text2","Replace text2")
TF.close
Set TF=FSO.CreateTextFile("YourFile",True)
TF.Writeline out2
TF.Close

Hope This Help
PH.
 
oops forgot to do
FileInfo = Replace(FileInfo, "search text", "Replace text")


 
Sweet, got it working. Thanks PHV!

If it wasn't a friday afternoon I would have figured that one out by myself spazman. Just one of those simple thing you overlook sometimes. Thanks for your help too!
 
You should also check out the Regular Expressions if you want clean fast replacing.
 
Kinda offtopic but...
Blaine2 can you explain how can a RegExp be faster then a normal replace?
Couldnt find any explanation just use it instead of normal replace.

________
George, M
 
In smaller files it's not noticeable different, but in large files it's definitely quicker. The real advantage of learning Regular Exp is that they are so much more flexible than "Replace" and have more compact syntax.

It took me several reads through the WSH docs for pieces of the power to sink in. Then it's also worth reading what Perl does with Regular Expressions because Perl has done them for a long time and there are lots of posts to study. O'Reilly also has a book just on using Regular Expresssions if you really get into them.

They are an alternative to "Replace" with a definite place in your vocabulary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top