Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 14th, 2008, 05:59 AM
acidedge2004 acidedge2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 114 acidedge2004 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 12 m 9 sec
Reputation Power: 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.

Reply With Quote
  #2  
Old April 14th, 2008, 05:14 PM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 12th Plane (10500 - 10999 posts)
 
Join Date: Jun 2003
Posts: 10,715 Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 21 m 37 sec
Reputation Power: 688
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

Reply With Quote
  #3  
Old April 15th, 2008, 04:18 AM
acidedge2004 acidedge2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 114 acidedge2004 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 12 m 9 sec
Reputation Power: 1
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 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Replace function on a non .txt file using vbscript


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway