Multiple File Search and Replace


  1.    03 Jan 2012 #1
    Join Date : Jan 2012
    Posts : 7
    Vista Home Premium 32bit
    Local Time: 01:21

    Multiple File Search and Replace


    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
    Multiple File Search and Replace Attached Files
      My System SpecsSystem Spec

  2.    03 Jan 2012 #2
    Join Date : Mar 2010
    England
    Posts : 3,642
    Windows 8.1 Pro x64
    Local Time: 08:21
    uk uk england

     

    Re: Multiple File Search and Replace


    Quote Originally Posted by AltNaMara View Post
    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

    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 System SpecsSystem Spec

  3.    03 Jan 2012 #3
    Join Date : Jan 2012
    Posts : 7
    Vista Home Premium 32bit
    Local Time: 01:21


      Thread Starter

    Re: Multiple File Search and Replace


    Apologies everyone I have attached the wrong script. Here is the correct one

    AltNaMara
    Multiple File Search and Replace Attached Files
      My System SpecsSystem Spec

  4.    05 Jan 2012 #4
    Join Date : Jan 2012
    Posts : 7
    Vista Home Premium 32bit
    Local Time: 01:21


      Thread Starter

    Re: Multiple File Search and Replace


    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
    Multiple File Search and Replace Attached Files
    Last edited by AltNaMara; 05 Jan 2012 at 16:00. Reason: Spelling error
      My System SpecsSystem Spec

  5.    05 Jan 2012 #5
    Join Date : Jan 2008
    Posts : 2,442
    Vista Home Premium 64 bit SP1
    Local Time: 03:21
    usa

     

    Re: Multiple File Search and Replace


    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/libr...spx#properties


    If not then I'll let someone better in VBS figure it out.
      My System SpecsSystem Spec

  6.    05 Jan 2012 #6
    Join Date : Jan 2012
    Posts : 7
    Vista Home Premium 32bit
    Local Time: 01:21


      Thread Starter

    Re: Multiple File Search and Replace


    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 System SpecsSystem Spec

  7.    05 Jan 2012 #7
    Join Date : Jan 2008
    Posts : 2,442
    Vista Home Premium 64 bit SP1
    Local Time: 03:21
    usa

     

    Re: Multiple File Search and Replace


    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 System SpecsSystem Spec

  8.    06 Jan 2012 #8
    Join Date : Jun 2010
    Belgium
    Posts : 189
    Vista Home Premium 32bit
    Local Time: 08:21
    belgium european union

     

    Re: Multiple File Search and Replace


    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


    ____________________
    Multiple File Search and Replace Attached Files
      My System SpecsSystem Spec

  9.    07 Jan 2012 #9
    Join Date : Jan 2012
    Posts : 7
    Vista Home Premium 32bit
    Local Time: 01:21


      Thread Starter

    Re: Multiple File Search and Replace


    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 System SpecsSystem Spec

  10.    07 Jan 2012 #10
    Join Date : Jun 2010
    Belgium
    Posts : 189
    Vista Home Premium 32bit
    Local Time: 08:21
    belgium european union

     

    Re: Multiple File Search and Replace


    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
    Multiple File Search and Replace Attached Files
      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Multiple File Search and Replace

Similar Threads
Thread Forum
Search and replace text inside an htm file
All, I found this neat litle code to find and replace some text within a file: ${c:\b.txt} = ${c:\b.txt} -replace "prod","test" I need to...
PowerShell
search log file and write multiple lines
Hi, Ive written some code to search a log file for a specified pattern and write the lines containing the pattern to a separate text file. In...
VB Script
Search and Replace text file very slow
Hello, I have text files approx 16-20mb in size. They are flat files with approx 18-20 thousand records each. Every night I have to search them...
PowerShell
multiple search and replaces in a text file
Initially I posted this as a registy question: how to edit the registry if I knew the value I was looking for, but not the exact key. I know the...
VB Script
search and replace in binary file
I'm trying to do a search and replace in a binary file, which is a user-specific file used by a web-conferencing application. I'm trying to preset...
VB Script
Newbie question: replace multiple strings in multiple text files
Hi there -- I've never created a script before now, and what I want to do is periodically have a script go into fifteen text files and do...
VB Script
Search and replace in a text file?
Is there an easy way to search for a string in a text file, and replace it with something else? Preferably using a regex, but it's not really that...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 02:21.
    .