Find & Replace with VBScript

find and replace multiple times with csv file

Page 1 of 1

7 Replies - 19015 Views - Last Post: 04 March 2009 - 11:00 AM Rate Topic: -----

#1 playinpearls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-March 09

Find & Replace with VBScript

Posted 03 March 2009 - 08:25 AM

i have this script:


strCSVFile = "example.csv"
strTemplate = "master file.ini"

' Read .csv file
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
	(strCSVFile, 1)

' Split .csv into an array
Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.Readline
	arrStringList = Split(strNextLine , ",")
	strFind = arrStringList(0)
	strReplace = arrStringList(1)
	strSaveFile = arrStringList (2)
	GoDoWork
Loop


i'm trying to edit it so it will read multiple lines from a csv file and find and replace from it. I have tried for months to get it to work, and i'm stuck except for starting completely over from scratch. I am also an extreme noob. I'm trying to confirm that this would work:

strCSVFile = "example.csv"
strTemplate = "master file.ini"

' Read .csv file
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
	(strCSVFile, 1)

' Split .csv into an array
Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.Readline
	arrStringList = Split(strNextLine , ",")
	[b]strFind = arrStringList(0)
	strReplace = arrStringList(1)
	strFind2 = arrStringList(2)
	strReplace2 = arrStringList(3)
	strSaveFile = arrStringList (4)[/b]
	GoDoWork
Loop



i dont know if this is the problem i'm running into, this is only the begining of the script. what happens is it finds the first, replaces with the second, then it ignores the rest except for renaming the file, or it will rename it with strFind2.

i can post more of the script where the problem may be, i figured i would start here.


thanks!

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
-Joey

Is This A Good Question/Topic? 0
  • +

Replies To: Find & Replace with VBScript

#2 cbrickhouse  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 01-March 09

Re: Find & Replace with VBScript

Posted 03 March 2009 - 08:39 AM

i have some vb.net code, why are you using vbscript?

	Public Function GetFileContents(ByVal FullPath As String, Optional ByRef ErrInfo As String = "") As String
		Dim strContents As String
		Dim objReader As StreamReader
		objReader = New StreamReader(FullPath)
		strContents = objReader.ReadToEnd()
		objReader.Close()
		Return strContents
	End Function

	Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean
		Dim Contents As String
		Dim bAns As Boolean = False
		Dim objReader As StreamWriter
		Try
			objReader = New StreamWriter(FullPath)
			objReader.Write(strData)
			objReader.Close()
			bAns = True
		Catch Ex As Exception
			ErrInfo = Ex.Message

		End Try
		Return bAns
	End Function

Dim scontents As String = GetFileContents("C:\example.csv")
scontents = Replace(scontents, "str to replace", "replace with")
dim thistrus as boolean = SaveTextToFile(scontents, "C:\example.csv")


Was This Post Helpful? 1

#3 playinpearls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-March 09

Re: Find & Replace with VBScript

Posted 03 March 2009 - 08:44 AM

just cause i'm really new, and i want to be able to use it on mulitple computers....do i have to install anything to get it to work in VB.net?

i've got .net installed already. i dont really know much about programming. the guy that wrote this for me did the whole script in about 20 minutes. i figured it would be easier to just edit it and run it.
Was This Post Helpful? 0
  • +
  • -

#4 cbrickhouse  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 01-March 09

Re: Find & Replace with VBScript

Posted 03 March 2009 - 08:49 AM

create a new console project with vb.net and change "file.csv" to whatever your file is, and change

scontents = Replace(scontents, "text to change", "change to")

to what you want to replace, and run it. it will work

Imports System.IO
Module Module1

	Sub Main()
		Dim scontents As String = GetFileContents("C:\file.csv")
		Dim i As Integer = 1
		scontents = Replace(scontents, "text to change", "change to")
		MsgBox(SaveTextToFile(scontents, "C:\file.csv"))
	End Sub

	Public Function GetFileContents(ByVal FullPath As String, Optional ByRef ErrInfo As String = "") As String
		Dim strContents As String
		Dim objReader As StreamReader
		objReader = New StreamReader(FullPath)
		strContents = objReader.ReadToEnd()
		objReader.Close()
		Return strContents
	End Function

	Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean
		Dim bAns As Boolean = False
		Dim objReader As StreamWriter
		Try
			objReader = New StreamWriter(FullPath)
			objReader.Write(strData)
			objReader.Close()
			bAns = True
		Catch Ex As Exception
			ErrInfo = Ex.Message

		End Try
		Return bAns
	End Function
End Module


Was This Post Helpful? 0
  • +
  • -

#5 playinpearls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-March 09

Re: Find & Replace with VBScript

Posted 03 March 2009 - 10:14 AM

oops, i for got to put framework....i only have .net framework...

also, i need it to read the csv file so it wil find , replace, and save as a new text file. it will be as so:

OPEN FILE,FIND,REPLACE,FIND,REPLACE,FIND ,REPLACE,SAVE AS

FILE.INI,RF001,RF051,192.168.0.1,192.168.0.51,0001,0050,NEW0050.INI
FILE.INI,RF001,RF052,192.168.0.1,192.168.0.52,0001,0051,NEW0052.INI
FILE.INI,RF001,RF053,192.168.0.1,192.168.0.53,0001,0052,NEW0053.INI
FILE.INI,RF001,RF054,192.168.0.1,192.168.0.54,0001,0053,NEW0054.INI

This post has been edited by playinpearls: 03 March 2009 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#6 cbrickhouse  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 01-March 09

Re: Find & Replace with VBScript

Posted 03 March 2009 - 10:21 AM

download vb.net express edition for free.

http://www.microsoft.com/Express/

make a new "console application"
load the code i gave you into it

run it

it will do exactly what you want, open file, replace, save
Was This Post Helpful? 0
  • +
  • -

#7 playinpearls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-March 09

Re: Find & Replace with VBScript

Posted 03 March 2009 - 01:04 PM

i guess i didn't give enough info...my appologies

the script you have does work...perfectly...but i was only referring to one portion of my need.

the full script i need is one that will open a master .ini file, read a csv file and change the parameters based on:

find,replace,find,replace,save as

the save as will be to a new ini file based on the last entry of the csv file.

but i have to be able to edit the number of "find and replaces"
sometimes there will be just one, sometimes up to 5 different occurances...and i'm using this to create literally thousands of files, so editting for each file will not work.

i see the form application...that would be awesome, i'll be working on that next, but for now, i just want one i can run from console i guess....

i'll be doing more reasearch also...but if your interested in tackling it i would sure appreciate it...

thanks!! B)

This post has been edited by playinpearls: 03 March 2009 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 playinpearls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-March 09

Re: Find & Replace with VBScript

Posted 04 March 2009 - 11:00 AM

Here is my full script. It does exaclty what i want, but it will only edit one field. I need it to edit up to 5 fields.



strCSVFile = "example.csv"
strTemplate = "master file.ini"

' Read .csv file
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
	(strCSVFile, 1)

' Split .csv into an array
Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.Readline
	arrStringList = Split(strNextLine , ",")
	strFind = arrStringList(0)
	strReplace = arrStringList(1)
	strSaveFile = arrStringList (2)
	GoDoWork
Loop

' ~~~~~~~~~~~~~~Functions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Open Template file
Function GoDoWork()
Dim arrFileLines()
i = 0

'Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strTemplate, 1)

Dim FileName, Find, ReplaceWith, FileContents, dFileContents

Find = strFind
ReplaceWith = strReplace
FileName = strSaveFile

'Read template text file
FileContents = GetFile(strTemplate)

'replace all strings In the source file
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)

'Compare source And result
if dFileContents <> FileContents Then
'write result If different
WriteFile FileName, dFileContents
End If

objFile.Close
End Function	
	
'Read text file
Function GetFile(FileName)
If FileName<>"" Then
Dim FS, FileStream
Set FS = CreateObject("Scripting.FileSystemObject")
on Error Resume Next
Set FileStream = FS.OpenTextFile(FileName)
GetFile = FileStream.ReadAll
End If
End Function

'Write text file.
Function WriteFile(FileName, Contents)
Dim OutStream, FS
on Error Resume Next
Set FS = CreateObject("Scripting.FileSystemObject")
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function 
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1