vbscript replace text - one works two breaks the whole thing - vbscript

I want this script to replace two values in the same text file instead of one. However, if I uncomment line 12, it breaks the script. Do I have to make this into a loop, or can I do multiple replaces?
Sub ReplaceTxt()
'Writes values we got earlier to our unattend file '
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "***COMPNAME***", strCompname)
' strNewText = Replace(strText, "***Winkey***", strPoductkey) '
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strNewText
objFile.Close
End Sub

I think you will want to do the second replace on the string returned by the first one:
strNewText = Replace(strText, "***COMPNAME***", strCompname)
strNewText = Replace(strNewText , "***Winkey***", strPoductkey)
Otherwise you will lose the first replace, and only the second one will appear in the result.

Try this:
Sub ReplaceTxt() 'Writes values we got earlier to our unattend file'
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strText = Replace(strText, "COMPNAME", strCompname)
strText = Replace(strText, "Winkey", strPoductkey)
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strText
objFile.Close
End Sub
By doing it the way you were, you were using the original, unused text twice, overwriting the first replace when you did the second.

I'm sure my if statement is ugly to the real coders out there, but here's how I got it to work
Sub ReplaceTxt() 'Writes values we got earlier to our unattend file'
Const ForReading = 1
Const ForWriting = 2
counter = 1
For Each searchterm In Array("COMPNAME", "Winkey")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
If counter < 2 Then
strText = Replace(strText, searchterm, strCompname)
Else
strText = Replace(strText, searchterm, strProductKey)
End If
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strText
objFile.Close
counter = counter + 1
Next
End Sub

Well.. Thats easy.. Bueno ni tanto.. I was trying to do this some time. and i found the answer:
Sub ReplaceTxt()
'Writes values we got earlier to our unattend file '
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "***COMPNAME***", strCompname)
strNewText2 = Replace(strNewText, "***Winkey***", strPoductkey) '
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strNewText2
objFile.Close
End Sub
Tanks, Gracias. José Villa From Culiacán, Sinaloa Mexico

Related

Delete preceding lines from text file if line has value

i have a text file with numbers and i must delete the lines before a given number.
45048
67113.88
74484
597.6
1945.65
8714.5
32085.9
741.12
1721.39
35266.7
8260.71
23635.8
40487.5
40702.18
29544.74
110000
810000
3161000
29201.91
33000
So i must delete the lines before number 110000 . Or in another day i must delete the lines before 1721.39. I don't know how to make the deletion before the user input value. or just delete first 25 line if user prompt 25...
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "forward", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, ".", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.Write strNewContents
objFile.Close
Try this for delete number of lines, And try yourself others.
Const FOR_READING = 1
Const FOR_WRITING = 2
strFileName = "C:\txt.txt"
NumberOfLinesToBeDelete=inputbox("How many lines want to delete from beginning","Delete","")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING)
strContents = objTS.ReadAll
objTS.Close
arrLines = Split(strContents, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
For i=0 To UBound(arrLines)
If i > (NumberOfLinesToBeDelete - 1) Then
objTS.WriteLine arrLines(i)
End If
Next

Facing issue in replace string in a file using vbscript

I have a text file which contains below data
mkdir language
Here is my vbscript which replace language string in text file
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "language", "english,french,spanish")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
I am getting output mkdir english,french,spanish but what i want output in text file as shown below
mkdir english
mkdir french
mkdir spanish
How to achieve this please help
This will work too...
Const ForReading = 1
Const ForWriting = 2
Dim langs
langs = Array("english","french","spanish")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
tempTxt = ""
strNewText = ""
For Each i in langs
tempTxt = "mkdir " + i + vbCrLf
strNewText = strNewText + tempTxt
Next
strNewText = Replace(strText, "mkdir language", strNewText)
Set objFile = objFSO.OpenTextFile("Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Here try this variation:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
For Each strLanguage In Array("english", "french", "spanish")
objFile.WriteLine Replace(strText, "language", strLanguage)
Next
objFile.Close
Use Join to build a string from an array:
>> c = "mkdir "
>> l = Split("english,french,spanish", ",")
>> WScript.Echo c & Join(l, vbCrLf & c)
>>
mkdir english
mkdir french
mkdir spanish
Of course, c could come from a file.
(But I suspect your real world problem isn't shown in your simplified sample)

VB Scripting error.. Object Required

This is my first day with VB scripting. I found following code to search and replace text in a text file but when I run that using the following command
cscript replace.vbs "test.txt" "Jim" "James"
I get an error saying
replace.vbs(6, 1) Microsoft VBScript runtime error: Object required: 'Scripting'
Here is the code
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject(Scripting.FileSystemObject)
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText 'WriteLine adds extra CRLF
objFile.Close
Use
Set objFSO = CreateObject("Scripting.FileSystemObject")
(mark the quotes, CreateObject() needs a string)

Trying to insert delimiter into a text document

I am trying to place a vertical bar at certain points in each line of a text file. My code is pretty straightforward I'm pretty sure...but when i try running it nothing happens. I don't even get an error. The file that it is supposed to write to just remains a blank text file
Const ForReading = 1
Const ForWriting = 2
arrCommas = Array(10,14,21,24,39,43,46,61,72,79,82,85,88,91,94,97,101,142,173,189,192,198,205,211,218,222,229,236,240)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsedited\mbsfact102013_linebreaks.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
intLength = Len(strLine)
For Each strComma in arrCommas
strLine = Left(strLine, strComma - 1) + "|" + Mid(strLine, strComma, intLength)
Next
strText = strText & strLine & vbCrLf
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsfinal\mbsfact102013_delimited.txt", ForWriting)
objFile.Write strText
objFile.Close
Const ForReading = 1
Const ForWriting = 2
arrCommas = Array(10,14,21,24,39,43,46,61,72,79,82,85,88,91,94,97,101,142,173,189,192,198,205,211,218,222,229,236,240)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsedited\mbsfact102013_linebreaks.txt", ForReading)
strTextFile = objFile.ReadAll
objFile.Close
aTextFile = Split(strTextFile, vbCRLF)
strText = ""
For Each strLine In aTextFile
intLength = Len(strLine)
For Each strComma in arrCommas
strLine = Left(strLine, strComma - 1) + "|" + Mid(strLine, strComma, intLength)
Next
strText = strText & strLine & vbCrLf
Next
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsfinal\mbsfact102013_delimited.txt", ForWriting)
objFile.Write strText
objFile.Close

Modifying multiple text files with VBScript

i need help with this VBScript
What I'm trying to do here is modify the logfile to remove the extra spaces inside. (I just got this script actually somewhere on the net.)
It works if i specify just a single file but I'm trying to modify multiple files. Using the wildcard character as i did below did not work either (sorry I'm not so good with vbs)
Also does anyone know how we can do this without creating a new output file? just modify the original file. Thanks in advance..
Set objFSO = CreateObject("Scripting.FileSystemObject")
'change this line to wherever you want to read the input from.
Set objTextFile = objFSO.OpenTextFile("D:\access*.log",1)
Set objNewFile = objFSO.CreateTextFile("D:\access*_new.log")
Do Until objTextFile.AtEndOfStream
myString = objTextFile.Readline
objNewFile.WriteLine(Replace (myString, " ", " "))
Loop
In addition to my comment: The Scripting Guy explains exactly your replacement case: multiple spaces by one, with a regular expression:
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = " {2,}"
strSearchString = _
"Myer Ken, Vice President, Sales and Services"
strNewString = objRegEx.Replace(strSearchString," ")
Wscript.Echo strNewString
The Scripting Guy also explains how you can change a text file:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
And on the same technet.microsoft you can find how you can easily iterate over all files. You can use a regular expression again to see if the file is matching your (wildcard) pattern, in your case ^access.*\.log$:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name, objFile.Size
Next
This should give you all the ingredients to create your script.
The freeze-dried version:
Const ForReading = 1
Const ForWriting = 2
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim reZap : Set reZap = New RegExp
reZap.Global = True
reZap.Pattern = " +"
Dim oFile
For Each oFile In goFS.GetFolder("..\testdata\14620676").Files
WScript.Echo "----", oFile.Name
Dim sAll : sAll = oFile.OpenAsTextStream(ForReading).ReadAll()
WScript.Echo sAll
oFile.OpenAsTextStream(ForWriting).Write reZap.Replace(sAll, " ")
WScript.Echo oFile.OpenAsTextStream(ForReading).ReadAll()
Next
that makes no sense at all without #AutomatedChaos' admirable contribution (+1), but avoids growing the file's tail by using .Write instead of .WriteLine.

Resources