Solved Multiple File Search and Replace

AltNaMara

Member
I would be grateful for any help anyone can give with this VBS script. I originally just wanted to have a script which would cycle through a folder of HTML files and then list the title text of all the HTML files in an output file. I managed this then realized that what I really required as well was the ability to amend the title text, save the HTML file and output the amended text to the list in the output file.

I have managed, after a fashion, to cobble up this script which lets me view the text between the title tags [ie <title>text</title>], amend it and output the amended title text to a list in the outputfile. However, I'm now stuck because I still need to work out how to save each amended HTML file in the folder while using their original file name.

I am a newbie so my knowledge as yet is still quite limited and I put the script together using whatever samples I could find. I have searched for scripts which will cover this final stage I need to include but have drawn a blank.

Any help, pointers, script directions would be very welcome

Thank you

AltnNaMara
 

Attachments

  • Show_Title.vbs
    992 bytes · Views: 23

My Computer

System One

  • Manufacturer/Model
    cOMPAQ NX 6325 laptop
    CPU
    1.6
I would be grateful for any help anyone can give with this VBS script. I originally just wanted to have a script which would cycle through a folder of HTML files and then list the title text of all the HTML files in an output file. I managed this then realized that what I really required as well was the ability to amend the title text, save the HTML file and output the amended text to the list in the output file.

I have managed, after a fashion, to cobble up this script which lets me view the text between the title tags [ie <title>text</title>], amend it and output the amended title text to a list in the outputfile. However, I'm now stuck because I still need to work out how to save each amended HTML file in the folder while using their original file name.

I am a newbie so my knowledge as yet is still quite limited and I put the script together using whatever samples I could find. I have searched for scripts which will cover this final stage I need to include but have drawn a blank.

Any help, pointers, script directions would be very welcome

Thank you

AltnNaMara

Hello AltNaMara and welcome to the forums :party:

There aren't many of us here who have much experience with VBS, but I have asked a member, who I think may know a bit of VBS, to help. All I can do with VBS is make the CD drive open and close repeatedly... and that isn't particularly useful! ;)

Tom
 

My Computer

System One

  • Manufacturer/Model
    Build #1
    CPU
    Intel Core i7 3770K @4.4GHz
    Motherboard
    ASUS P8Z77-V PRO
    Memory
    Corsair Vengeance 2x4GB DDR3 1600MHz Low Profile (White)
    Graphics Card(s)
    Gigabyte Radeon HD 7850 (2GB GDDR5)
    Sound Card
    Integrated on motherboard
    Monitor(s) Displays
    23" LG LCD/LED IPS
    Screen Resolution
    1920*1080
    Hard Drives
    Samsung EVO 128GB SSD
    Seagate Barracuda 2TB 7200rpm
    2x500GB Seagate FreeAgent 5400rpm
    PSU
    Corsair TX650W V2 (80+ Bronze)
    Case
    NZXT Phantom 410
    Cooling
    Corsair H100 Water Cooler, 1x140mm and 1x120mm stock fans
    Keyboard
    Microsoft Desktop 2000 Wireless Keyboard
    Mouse
    Microsoft Desktop 2000 Wireless Mouse
    Internet Speed
    95 Mb/s Download 70 Mb/s Upload
Apologies everyone I have attached the wrong script. Here is the correct one

AltNaMara
 

Attachments

  • HTML_Title_Changer.vbs
    2 KB · Views: 19

My Computer

System One

  • Manufacturer/Model
    cOMPAQ NX 6325 laptop
    CPU
    1.6
Have worked some more on this script and it now replaces the original text between the "title" tags with the amended text however I am stumped as I can't get the script to save the amended HTML file. Running it produces an error message: 800A0186 - Object doesn't support this property or method; 'name'. Can anyone help point out where I am going wrong.

Thank you

AltNaMara
 

Attachments

  • HTML_Title_Changer.vbs
    2.4 KB · Views: 18
Last edited:

My Computer

System One

  • Manufacturer/Model
    cOMPAQ NX 6325 laptop
    CPU
    1.6
I haven't done a lot of VBS. But tell me if this line is assigning a value to Name or if it is only testing equality:

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='F:\VBS-Data_Analysis'} Where " _
& "ResultClass = CIM_DataFile")

If Name is being assigned 'F:\VBS-Data_Analysis' that may be your problem as Name is read-only property according to this doc:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394130(v=vs.85).aspx#properties


If not then I'll let someone better in VBS figure it out. :)
 

My Computer

System One

  • Manufacturer/Model
    HP Pavilion m9515y
    CPU
    Phenom X4 9850
    Memory
    8 GB
    Graphics Card(s)
    Some Radeon Cheapie with 512 MB Ram
    Monitor(s) Displays
    CRT
    Screen Resolution
    1280x1024
    Hard Drives
    750 GB SATA 3G
    2 SIIG Superspeed docks w/WD Caviar Black Sata II or III
Hi MilesAhead thank you for your reply. I should have said that the error message quotes the error as arising at line 94 of the script ie "strFilePath = objFile.Name".

I can't figure out what is wrong as I thought that the same variable "objFile.Name" would be used as this time writing to the file is being called

Thank you

AltNaMara
 

My Computer

System One

  • Manufacturer/Model
    cOMPAQ NX 6325 laptop
    CPU
    1.6
I think where you are running into a problem is you are using For Each objFile in (yadda yadda)

then you immediately assign something to that variable. Use 2 different variables. Also I think the error is because TextStream Object has no property "Name" I've rarely used file system scripting so I'm just looking the stuff up as I go along. As a general rule when you do For Each the placeholder variable that gets the instance in the loop should only be read from, not assigned to or deleted as it breaks the bag of associations(or at least can cause items to be skipped.. unexpected results etc..)
 

My Computer

System One

  • Manufacturer/Model
    HP Pavilion m9515y
    CPU
    Phenom X4 9850
    Memory
    8 GB
    Graphics Card(s)
    Some Radeon Cheapie with 512 MB Ram
    Monitor(s) Displays
    CRT
    Screen Resolution
    1280x1024
    Hard Drives
    750 GB SATA 3G
    2 SIIG Superspeed docks w/WD Caviar Black Sata II or III
Hello AltNaMara

I'm sorry for the late response,
but here is a script which I hope does what you want.
If there are other questions, just ask.
I will do my best to answer it.
I have omitted the text file Totals.txt.
If you still want to have it, I can work something out.


Sweet


____________________


Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='J:\VBS-Data_Analysis'} Where " _
& "ResultClass = CIM_DataFile")
For Each objFile In colFiles
If objFile.Extension = "html" Then
Set objFile = objFSO.OpenTextFile(objFile.Name, ForReading)
strContents = objFile.ReadAll
objFile.Close
strStartText = "<title>"
strEndText = "</title>"
intStart = InStr(strContents, strStartText)
intStart = intStart + Len(strStartText)
intEnd = InStr(strContents, strEndText)
intCharacters = intEnd - intStart
strCount = Mid(strContents, intStart, intCharacters)
MsgBox strCount
Message = "Please Enter New Title "
Text1 = "User input canceled "
Text2 = "You entered: " & vbCrLf
result = InputBox(Message)
If result = "" Then
MsgBox Text1
Else
MsgBox Text2 & result
End If
Set oFSo = CreateObject("Scripting.FileSystemObject")
Set sptF = oFSo.CreateTextFile("J:\VBS-Data_Analysis\" & result & ".html", True)
OstrNewText = Replace(strContents, strCount, Result)
sptF.WriteLine OstrNewText
sptF.Close
strText = strText & strCount & vbCrLf
End If : Next


____________________
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5738G-644G32MN
    CPU
    Intel Core 2 Duo - clockspeed 2000 MHz
    Motherboard
    Acer JV50
    Memory
    RAM 4 GB - SO-DIMM DDR3
    Graphics Card(s)
    NVIDIA GeForce - G105M - 512 MB
    Screen Resolution
    1366 x 768 Pixels
    Hard Drives
    Hitachi HTS543232L9A300
    320 GB - Type SATA - Speed 5400 rpm
    Keyboard
    Azerty
    Mouse
    Logitech Wireless Mouse M505
    Internet Speed
    Currently 14.5 MB/Sec which means 1.8 MB/Sec effective
Hi Sweet, apologies for the delay in responding but this is the first chance I have had to have a look. A thousand thanks for your script! It successfully puts the "amended text" between the title tags in the HTML files, something I couldn't achieve on my own! However, the resultant HTML file is saved using the "amended text" as the file name instead of the original HTML file name. I have had a look but wasn't able to figure out how to do it. I could certainly get round it by using a batch renamer on all the HTML files in the folder but I would be really grateful if you could amend the script for me.

Many thanks

AltNaMara
 

My Computer

System One

  • Manufacturer/Model
    cOMPAQ NX 6325 laptop
    CPU
    1.6
Hi AltNaMara

First take a look at the script I've attached.
It also creates a folder called Totals in the folder VBS-Data_Analysis,
and then places the text files (With date and time in the filename), containing information about the changed titles,
in the folder Totals.

I suppose you only want to change the title of the HTML file, without creating a new file.
I'll see what I can cobble together, but now I dive into my bed,
because it's getting late.

Sweet
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5738G-644G32MN
    CPU
    Intel Core 2 Duo - clockspeed 2000 MHz
    Motherboard
    Acer JV50
    Memory
    RAM 4 GB - SO-DIMM DDR3
    Graphics Card(s)
    NVIDIA GeForce - G105M - 512 MB
    Screen Resolution
    1366 x 768 Pixels
    Hard Drives
    Hitachi HTS543232L9A300
    320 GB - Type SATA - Speed 5400 rpm
    Keyboard
    Azerty
    Mouse
    Logitech Wireless Mouse M505
    Internet Speed
    Currently 14.5 MB/Sec which means 1.8 MB/Sec effective
Hi AltNaMara

I rewrote the script because the structure was not workable for me.
You have to keep in mind that I'm also just a beginner when it comes to programming and scripting.
Anyway, I've managed to cobble something together which I hope it fulfills your needs.
The script now only changes the Title of the HTML file which I guess is what you wanted.
Let me now, if there is anything you would like to change about the script, and I'll see what i can do.

Hoping to be helpfull
Sweet
 
Last edited:

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5738G-644G32MN
    CPU
    Intel Core 2 Duo - clockspeed 2000 MHz
    Motherboard
    Acer JV50
    Memory
    RAM 4 GB - SO-DIMM DDR3
    Graphics Card(s)
    NVIDIA GeForce - G105M - 512 MB
    Screen Resolution
    1366 x 768 Pixels
    Hard Drives
    Hitachi HTS543232L9A300
    320 GB - Type SATA - Speed 5400 rpm
    Keyboard
    Azerty
    Mouse
    Logitech Wireless Mouse M505
    Internet Speed
    Currently 14.5 MB/Sec which means 1.8 MB/Sec effective
Hi, Sweet, this is really impressive stuff and the use of notepad to input the file name is a great idea. However, the only item missing now is the text file with a list of all the "amended" or "not-amended" HTML file <titles>. If you could see your way to include that it would be fantastic!

Thank you so much!

AltNaMara
 

My Computer

System One

  • Manufacturer/Model
    cOMPAQ NX 6325 laptop
    CPU
    1.6
Hey AltNaMara

Yes, I had forgotten to do that,
but after I had uploaded the file HTML Title Changer ReEdited.vbs,
I immediately wrote some code to implement this.
You will probably have downloaded the first file I've uploaded.
If you go to post #11, and download the file again,
you will notice that now a text file called Totals.txt is created in the folder J:\VBS-Data_Analysis,
with information about the changed Titles.

Hope to have helped
Sweet
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5738G-644G32MN
    CPU
    Intel Core 2 Duo - clockspeed 2000 MHz
    Motherboard
    Acer JV50
    Memory
    RAM 4 GB - SO-DIMM DDR3
    Graphics Card(s)
    NVIDIA GeForce - G105M - 512 MB
    Screen Resolution
    1366 x 768 Pixels
    Hard Drives
    Hitachi HTS543232L9A300
    320 GB - Type SATA - Speed 5400 rpm
    Keyboard
    Azerty
    Mouse
    Logitech Wireless Mouse M505
    Internet Speed
    Currently 14.5 MB/Sec which means 1.8 MB/Sec effective
Hi, Sweet, I can't thank you enough for the superb job you have done in solving this for me. I have learned ever so much from the "bells and whistles" you added and the change of approach. Even so, if you regard yourself as a beginner, I haven't even got on the first rung of the ladder yet!

This script now gives me everything I was looking for and, in fact, I doubt there is even any 3rd party software out there which can do what it does. It will save me hours of time!

My grateful thanks and appreciation for giving me your time and dedication

AltNaMara
 

My Computer

System One

  • Manufacturer/Model
    cOMPAQ NX 6325 laptop
    CPU
    1.6
You're very welcome AltNaMara.
It was a pleasure to help you.
And as a bonus, I learned something from it.
I admire that you, as a beginner, have succeeded to cobble together some code even though it was not perfect.
We must all learn, and I can only say, keep doing what you do, you will definitely be a good programmer someday.
If you have any questions in the future, don't hesitate to come knocking on our door.
For now, have a great year and make the best of it.

Thank you and kind regards
Sweet
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5738G-644G32MN
    CPU
    Intel Core 2 Duo - clockspeed 2000 MHz
    Motherboard
    Acer JV50
    Memory
    RAM 4 GB - SO-DIMM DDR3
    Graphics Card(s)
    NVIDIA GeForce - G105M - 512 MB
    Screen Resolution
    1366 x 768 Pixels
    Hard Drives
    Hitachi HTS543232L9A300
    320 GB - Type SATA - Speed 5400 rpm
    Keyboard
    Azerty
    Mouse
    Logitech Wireless Mouse M505
    Internet Speed
    Currently 14.5 MB/Sec which means 1.8 MB/Sec effective
Back
Top