|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Replace function on a non .txt file using vbscript
I am having a problem. I wish to open an .sql file and replace the string ***UserList*** with a variable, strUserList, which is a string. I have been googling for a couple of hours now and though I can find how to do it with a txt file, I can not find how to do it with a non-text file. It is like no one has ever thought you would want to!!! All I ever find is how to append to the bottom or overwrite the file. I need to open the file, read it, perform the replace and save the file.
If anyone can help me then I would VERY much appreciate it. this is what I have so far. Thanks for any help, Mike Code:
'Read source text file
FileName = WScript.Arguments(1)
Dim filesys, file, txtstream
set filesys = CreateObject ("Scripting.FileSystemObject")
set file = filesys.GetFile(FileName)
set txtstream = file.OpenAsTextStream (2, -2)
dFileContents = replace(txtstream, "***UserList***", strUserList, 1, 1, 1)
WriteFile file, dFileContents
txtstream.Close
Last edited by acidedge2004 : April 14th, 2008 at 06:09 AM. |
|
#2
|
|||
|
|||
|
Think stream editor. Use a one-two step approach, one that reads the source file line by line, edits the line as necessary, then writes the lines out to a new file. Rename or otherwise clean up your files when done.
__________________
====== Doug G ====== "Hide, hide witch! The good folk come to burn thee. Their keen enjoyment hid behind their gothic mask of duty." -Mark Clifton |
|
#3
|
|||
|
|||
|
I managed to get it working myself, so if anyone is interested here is the code, suppose another noob to VBScript like me could use it! This one copies the sql template, then uses the replace statement on it.
WScript.arguments: 1 = SQL template location 2 = location to copy the template to Code:
Dim filesys, filename, file1, file2, filename2, txtstream, dFileContents, contents
'Set the file object as the file you want
filename = WScript.Arguments(1)
set filesys = CreateObject ("Scripting.FileSystemObject")
set file1 = filesys.GetFile(filename)
'set txtstream as the open file in read mode, then read the contents to a variable
set txtstream = file1.OpenAsTextStream (ForReading, -2)
contents = txtstream.ReadAll
txtstream.Close
'create the replace statment
dFileContents = replace(contents, "***UserList***", strUserList, 1, 1, 1)
'Create a copy of the template
filename2 = WScript.Arguments(2)
set file2 = filesys.GetFile(FileName)
file2.Copy filename2
'set file2 to the newly created file
set file2 = filesys.GetFile(filename2)
'open the file as a stream in read mode this time
set txtstream = file2.OpenAsTextStream(ForWriting)
txtstream.WriteLine dFileContents
txtstream.Close
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Replace function on a non .txt file using vbscript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|