Antonio King
asked on
Replace line of text on numerous files at once...
Looking for a script that can search a folder for all files with extension ".htm"
If it finds one, then it would need to search within this file for a specific line number...
IE: open the file in notepad, Edit>Go To and type in the number.
The number of the line would be specified in the script. Lets say for example it would be line 10 (same for all of the files)...
Once its found this line, I'd like it to replace it with specific text I specify.
Many thanks in advance!
If it finds one, then it would need to search within this file for a specific line number...
IE: open the file in notepad, Edit>Go To and type in the number.
The number of the line would be specified in the script. Lets say for example it would be line 10 (same for all of the files)...
Once its found this line, I'd like it to replace it with specific text I specify.
Many thanks in advance!
ASKER
Line: 3
Char: 14
Error: Unexpected end of statement
Code: 800A0401
Source: Microsoft VBScript compilation error
Char: 14
Error: Unexpected end of statement
Code: 800A0401
Source: Microsoft VBScript compilation error
Dim objFolder=objFSO.GetFolder ("C:\MyFol der\")
should read:
Dim objFolder: Set objFolder=objFSO.GetFolder ("C:\MyFol der\")
should read:
Dim objFolder: Set objFolder=objFSO.GetFolder
ASKER
Line: 8
Char: 5
Error: Type mismatch: 'CreateTextFile'
Code: 800A00D
Source: Microsoft VBScript Runtime error
Char: 5
Error: Type mismatch: 'CreateTextFile'
Code: 800A00D
Source: Microsoft VBScript Runtime error
Sorry - change set objFile=CreateTextFile(Rep lace(file. path,".htm ",".work") )
to
set objFile=objFSO.CreateTextF ile(Replac e(file.pat h,".htm"," .work"))
to
set objFile=objFSO.CreateTextF
I would've tested this first, if I were at home...sorry about the trouble...
Try this with corrections. Also note that script assume that all files have at least 10 lines.
Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scrip ting.FileS ystemObjec t")
Dim objFolder:Set objFolder=objFSO.GetFolder ("C:\testi ng\")
strLineNumber=10
strNewData="This is the replaced text"
For Each File in objFolder.Files
If lCase(objFSO.GetExtensionN ame(file.n ame))="htm " Then
set objFile=objFSO.CreateTextF ile(Replac e(file.pat h,".htm"," .work"))
arrData=Split(objFSO.OpenT extFile(fi le.path).R eadAll,vbN ewLine)
For x = 0 to strLineNumber - 2
objFile.WriteLine arrData(x)
Next
objFile.WriteLine strNewData
For x = strLineNumber +1 to ubound(arrData)
objFile.WriteLine arrData(x)
Next
objFile.Close
objFSO.MoveFile Replace(file.path, ".work",".htm"), file.path
End If
Next
Set objFolder=Nothing
Set objFSO=Nothing
Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scrip
Dim objFolder:Set objFolder=objFSO.GetFolder
strLineNumber=10
strNewData="This is the replaced text"
For Each File in objFolder.Files
If lCase(objFSO.GetExtensionN
set objFile=objFSO.CreateTextF
arrData=Split(objFSO.OpenT
For x = 0 to strLineNumber - 2
objFile.WriteLine arrData(x)
Next
objFile.WriteLine strNewData
For x = strLineNumber +1 to ubound(arrData)
objFile.WriteLine arrData(x)
Next
objFile.Close
objFSO.MoveFile Replace(file.path, ".work",".htm"), file.path
End If
Next
Set objFolder=Nothing
Set objFSO=Nothing
OK, I see that u beat me to the chase. I actually tested it and it works.
ASKER
It does work... however i have a folder full of files with the extension ".work" left over... the original files havn't been modified?
Flip-flop these two lines then...this will read the file into an array and then create the new file, which simply replaces the existing...
arrData=Split(objFSO.OpenT extFile(fi le.path).R eadAll,vbN ewLine)
set objFile=CreateTextFile(Rep lace(file. path)
arrData=Split(objFSO.OpenT
set objFile=CreateTextFile(Rep
ASKER
Sorry to be a pain, could i change it a bit? Instead of searching for line number could I change it so it will search for a small piece of text and its found on a particular line... replace the whole line with the text I specify? Cheeers!
I believe this will make it simpler actually.
Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scrip ting.FileS ystemObjec t")
Dim objFolder: Set objFolder=objFSO.GetFolder ("C:\MyFol der\")
strOldData="This is the old data"
strNewData="This is the replaced text"
For Each File in objFolder.Files
If lCase(objFSO.GetExtensionN ame(file.n ame))="htm " Then
Set objFile=objFSO.OpenTextFil e(file.pat h)
arrData=objFile.ReadAll
objFile.Close
Set objFile=objFSO.CreateTextF ile(file.p ath)
objFile.Write Replace(arrData, strOldData, strNewData)
objFile.Close
Set objFile=Nothing
End If
Next
Set objFolder=Nothing
Set objFSO=Nothing
Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scrip
Dim objFolder: Set objFolder=objFSO.GetFolder
strOldData="This is the old data"
strNewData="This is the replaced text"
For Each File in objFolder.Files
If lCase(objFSO.GetExtensionN
Set objFile=objFSO.OpenTextFil
arrData=objFile.ReadAll
objFile.Close
Set objFile=objFSO.CreateTextF
objFile.Write Replace(arrData, strOldData, strNewData)
objFile.Close
Set objFile=Nothing
End If
Next
Set objFolder=Nothing
Set objFSO=Nothing
ASKER
oops! Works, but doesn't replace the whole line?
Example..
line I want to edit reads...
I WOULD LIKE TO REMOVE REMOVE THIS LINE
In the code, I could just put in "I WOULD LIKE" and it would replace the WHOLE line with whatever I specify.
Thanks sirbounty!
Example..
line I want to edit reads...
I WOULD LIKE TO REMOVE REMOVE THIS LINE
In the code, I could just put in "I WOULD LIKE" and it would replace the WHOLE line with whatever I specify.
Thanks sirbounty!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
:( still not replacing the WHOLE line...
The line I want to replace looks like this..
<td width="50"><img src="http://www.xxxxx.com/emailimages/.jpg" width="50" height="70"></td>
In the vbs script I put this...
strOldData="Industrial2007 "
strNewData="<td width="570"><img src="http://www..xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>"
The line should then look like this...
<td width="570"><img src="http://www.cbsbutler.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>
The line I want to replace looks like this..
<td width="50"><img src="http://www.xxxxx.com/emailimages/.jpg" width="50" height="70"></td>
In the vbs script I put this...
strOldData="Industrial2007
strNewData="<td width="570"><img src="http://www..xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>"
The line should then look like this...
<td width="570"><img src="http://www.cbsbutler.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>
ASKER
Sorry ignore the above...
The line I want to replace looks like this..
<td width="50"><img src="http://www.xxxxx.com/emailimages/Industrial.jpg" width="50" height="70"></td>
In the vbs script I put this...
strOldData="Industrial2007 "
strNewData="<td width="570"><img src="http://www..xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>"
The line should then look like this...
<td width="570"><img src="http://www.xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>
The line I want to replace looks like this..
<td width="50"><img src="http://www.xxxxx.com/emailimages/Industrial.jpg" width="50" height="70"></td>
In the vbs script I put this...
strOldData="Industrial2007
strNewData="<td width="570"><img src="http://www..xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>"
The line should then look like this...
<td width="570"><img src="http://www.xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>
Industrial doesn't match Industrial2007 though..?
I'm confused... : \
I'm confused... : \
ASKER
Oopps...
The line I want to replace looks like this..
<td width="50"><img src="http://www.xxxxx.com/emailimages/Industrial2007.jpg" width="50" height="70"></td>
In the vbs script I put this...
strOldData="Industrial2007 "
strNewData="<td width="570"><img src="http://www..xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>"
The line should then look like this...
<td width="570"><img src="http://www.xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>
The line I want to replace looks like this..
<td width="50"><img src="http://www.xxxxx.com/emailimages/Industrial2007.jpg" width="50" height="70"></td>
In the vbs script I put this...
strOldData="Industrial2007
strNewData="<td width="570"><img src="http://www..xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>"
The line should then look like this...
<td width="570"><img src="http://www.xxxxx.com/emailimages/signature-bottomimage.bmp" width="570" height="95"></td>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Happy to help, as always! :^)
Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scrip
Dim objFolder=objFSO.GetFolder
strLineNumber=10
strNewData="This is the replaced text"
For Each File in objFolder.Files
If lCase(objFSO.GetExtensionN
set objFile=CreateTextFile(Rep
arrData=Split(objFSO.OpenT
For x = 0 to strLineNumber - 2
objFile.WriteLine arrData(x)
Next
objFile.WriteLine strNewData
For x = strLineNumber +1 to ubound(arrData)
objFile.WriteLine arrData(x)
Next
objFile.Close
objFSO.MoveFile Replace(file.path, ".work",".htm"), file.path
End If
Next
Set objFolder=Nothing
Set objFSO=Nothing