[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Replace line of text on numerous files at once...

Posted on 2007-04-05
20
Medium Priority
?
239 Views
Last Modified: 2010-04-16
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!
0
Comment
Question by:Antonio King
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 2
20 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858095
Untested, but this should work - please run it against test data first...

Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolder=objFSO.GetFolder("C:\MyFolder\")
strLineNumber=10
strNewData="This is the replaced text"
For Each File in objFolder.Files
  If lCase(objFSO.GetExtensionName(file.name))="htm" Then
    set objFile=CreateTextFile(Replace(file.path,".htm",".work"))
    arrData=Split(objFSO.OpenTextFile(file.path).ReadAll,vbNewLine)
    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
0
 

Author Comment

by:Antonio King
ID: 18858119
Line: 3
Char: 14
Error: Unexpected end of statement
Code: 800A0401
Source: Microsoft VBScript compilation error
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858196
Dim objFolder=objFSO.GetFolder("C:\MyFolder\")
should read:

Dim objFolder: Set objFolder=objFSO.GetFolder("C:\MyFolder\")
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Antonio King
ID: 18858212
Line: 8
Char: 5
Error: Type mismatch: 'CreateTextFile'
Code: 800A00D
Source: Microsoft VBScript Runtime error
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858249
Sorry - change set objFile=CreateTextFile(Replace(file.path,".htm",".work"))
to
set objFile=objFSO.CreateTextFile(Replace(file.path,".htm",".work"))
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858252
I would've tested this first,  if I were at home...sorry about the trouble...
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 18858285
Try this with corrections. Also note that script assume that all files have at least 10 lines.

Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolder:Set objFolder=objFSO.GetFolder("C:\testing\")
strLineNumber=10
strNewData="This is the replaced text"
For Each File in objFolder.Files
  If lCase(objFSO.GetExtensionName(file.name))="htm" Then
    set objFile=objFSO.CreateTextFile(Replace(file.path,".htm",".work"))
    arrData=Split(objFSO.OpenTextFile(file.path).ReadAll,vbNewLine)
    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
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 18858301
OK, I see that u beat me to the chase. I actually tested it and it works.
0
 

Author Comment

by:Antonio King
ID: 18858311
It does work... however i have a folder full of files with the extension ".work" left over... the original files havn't been modified?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858372
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.OpenTextFile(file.path).ReadAll,vbNewLine)
set objFile=CreateTextFile(Replace(file.path)
0
 

Author Comment

by:Antonio King
ID: 18858429
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!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858493
I believe this will make it simpler actually.

Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolder: Set objFolder=objFSO.GetFolder("C:\MyFolder\")
strOldData="This is the old data"
strNewData="This is the replaced text"
For Each File in objFolder.Files
  If lCase(objFSO.GetExtensionName(file.name))="htm" Then
    Set objFile=objFSO.OpenTextFile(file.path)
    arrData=objFile.ReadAll
    objFile.Close
    Set objFile=objFSO.CreateTextFile(file.path)
    objFile.Write Replace(arrData, strOldData, strNewData)
    objFile.Close
    Set objFile=Nothing
  End If
Next
Set objFolder=Nothing
Set objFSO=Nothing
0
 

Author Comment

by:Antonio King
ID: 18858562
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!
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 18858603
You mean you set it up like:
strOldData="I WOULD LIKE TO REMOVE REMOVE THIS LINE"
strNewData="I WOULD LIKE"

and it didn't work?

I wouldn't really want to take it this way, but you could loop through it...

Dim objFile
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolder: Set objFolder=objFSO.GetFolder("C:\MyFolder\")
strOldData="This is the old data"
strNewData="This is the replaced text"
For Each File in objFolder.Files
  If lCase(objFSO.GetExtensionName(file.name))="htm" Then
    arrData=Split(objFSO.OpenTextFile(file.path).ReadAll, vbNewLine)
    Set objFile=objFSO.CreateTextFile(file.path)
    For Each strLine in arrData
      objFile.WriteLine Replace(strLine, strOldData, strNewData)
    Next
    objFile.Close
    Set objFile=Nothing
  End If
Next
Set objFolder=Nothing
Set objFSO=Nothing
0
 

Author Comment

by:Antonio King
ID: 18858645
:( 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>
0
 

Author Comment

by:Antonio King
ID: 18858677
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>
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858695
Industrial doesn't match Industrial2007 though..?
I'm confused... : \
0
 

Author Comment

by:Antonio King
ID: 18858750
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>
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 2000 total points
ID: 18858834
So, you want to change the width from 50 to 570, and the image from Industrial2007 to signature-bottomimage...

You'll need to work around the quotes (")
Try this:

strOldData="<td width=" & chr(34) & "50" & chr(34) & "><img src=" & chr(34) & "http://www.xxxxx.com/emailimages/Industrial2007.jpg" & chr(34) & " width=" & chr(34) & "50" & chr(34) & " height=" & chr(34) & "70" & chr(34) & "></td>"

strNewData="<td width=" & chr(34) & "570" & chr(34) & "><img src=" & chr(34) & "http://www..xxxxx.com/emailimages/signature-bottomimage.bmp" & chr(34) & " width=" & chr(34) & "570" & chr(34) & " height=" & chr(34) & "95" & chr(34) & "></td>"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18858946
Happy to help, as always! :^)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question