find and replace a specific string in multiple files


  1.    29 Dec 2008 #1
    Mitul Guest

    find and replace a specific string in multiple files


    basically, i have a vbscript which does a find and replace on single file,
    below i've shared the script code.

    '[cscript|wscript] replace.vbs Find Replacewith File

    'Find … Required. Substring being searched for.
    'Replacewith … Required. Replacement substring.
    'File … Source and destination file for the replacement

    Dim FileName, Find, ReplaceWith, FileContents, dFileContents
    Find = WScript.Arguments(0)
    ReplaceWith = WScript.Arguments(1)
    FileName = WScript.Arguments(2)

    'Read source text file
    FileContents = GetFile(FileName)

    'replace all string In the source file
    dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)

    'Compare source And result
    if dFileContents <> FileContents Then
    'write result If different
    WriteFile FileName, dFileContents

    Wscript.Echo "Replace done."
    If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
    Wscript.Echo _
    ( (Len(dFileContents) - Len(FileContents)) /
    (Len(ReplaceWith)-Len(Find)) ) & _
    " replacements."
    End If
    Else
    Wscript.Echo "Searched string Not In the source file"
    End If

    'Read text file
    function GetFile(FileName)
    If FileName<>"" Then
    Dim FS, FileStream
    Set FS = CreateObject("Scripting.FileSystemObject")
    on error resume Next
    Set FileStream = FS.OpenTextFile(FileName)
    GetFile = FileStream.ReadAll
    End If
    End Function

    'Write string As a text file.
    function WriteFile(FileName, Contents)
    Dim OutStream, FS

    on error resume Next
    Set FS = CreateObject("Scripting.FileSystemObject")
    Set OutStream = FS.OpenTextFile(FileName, 2, True)
    OutStream.Write Contents
    End Function


    Now, I have a different requirement, I have atleast 20 different files
    stored at different folder locations in the same drive, these files have has
    got common strings in it, what I want is a script which would simply prompt
    the user to find "string" replace "string" at "location". For example,
    "Cscript.exe replace.vbs "serach string" "replace string" "C:\xyz\abc".
    Or
    Maybe I can dump all those 20 files in a text file and execute a batch file
    or a vbs to run on that text file and do a find replace.

    Could this be done? Any help is appreicated.

    Thanks,
    -Mitul
      My System SpecsSystem Spec

  2.    29 Dec 2008 #2
    Pegasus \(MVP\) Guest

    Re: find and replace a specific string in multiple files



    "Mitul" <Mitul@xxxxxx> wrote in message
    news:F1FCC651-186D-4D1D-8424-AFAD9E49EBEC@xxxxxx

    > basically, i have a vbscript which does a find and replace on single file,
    > below i've shared the script code.
    >
    > '[cscript|wscript] replace.vbs Find Replacewith File
    >
    > 'Find . Required. Substring being searched for.
    > 'Replacewith . Required. Replacement substring.
    > 'File . Source and destination file for the replacement
    >
    > Dim FileName, Find, ReplaceWith, FileContents, dFileContents
    > Find = WScript.Arguments(0)
    > ReplaceWith = WScript.Arguments(1)
    > FileName = WScript.Arguments(2)
    >
    > 'Read source text file
    > FileContents = GetFile(FileName)
    >
    > 'replace all string In the source file
    > dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)
    >
    > 'Compare source And result
    > if dFileContents <> FileContents Then
    > 'write result If different
    > WriteFile FileName, dFileContents
    >
    > Wscript.Echo "Replace done."
    > If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
    > Wscript.Echo _
    > ( (Len(dFileContents) - Len(FileContents)) /
    > (Len(ReplaceWith)-Len(Find)) ) & _
    > " replacements."
    > End If
    > Else
    > Wscript.Echo "Searched string Not In the source file"
    > End If
    >
    > 'Read text file
    > function GetFile(FileName)
    > If FileName<>"" Then
    > Dim FS, FileStream
    > Set FS = CreateObject("Scripting.FileSystemObject")
    > on error resume Next
    > Set FileStream = FS.OpenTextFile(FileName)
    > GetFile = FileStream.ReadAll
    > End If
    > End Function
    >
    > 'Write string As a text file.
    > function WriteFile(FileName, Contents)
    > Dim OutStream, FS
    >
    > on error resume Next
    > Set FS = CreateObject("Scripting.FileSystemObject")
    > Set OutStream = FS.OpenTextFile(FileName, 2, True)
    > OutStream.Write Contents
    > End Function
    >
    >
    > Now, I have a different requirement, I have atleast 20 different files
    > stored at different folder locations in the same drive, these files have
    > has
    > got common strings in it, what I want is a script which would simply
    > prompt
    > the user to find "string" replace "string" at "location". For example,
    > "Cscript.exe replace.vbs "serach string" "replace string" "C:\xyz\abc".
    > Or
    > Maybe I can dump all those 20 files in a text file and execute a batch
    > file
    > or a vbs to run on that text file and do a find replace.
    >
    > Could this be done? Any help is appreicated.
    >
    > Thanks,
    > -Mitul
    What's "c:\xyz\abc"?


      My System SpecsSystem Spec

  3.    30 Dec 2008 #3
    Mitul Guest

    Re: find and replace a specific string in multiple files


    > What's "c:\xyz\abc"?

    That's the location of files where that specific common string is to be
    found and replaced by another string.

    Let me give you an example:

    I have several JNLP and resource files stored in
    <Application_Home>\some-directory\1.jnlp, which has got a string ->
    https://houston-dc-1:80

    This similar string is located in several files in the hierarcy of above
    directories and sub-directories, example the strings are common in all these
    files, 2.JNLP, 3.JNLP, abc.resrouce, pqr.resource etc etc.

    What I want to achieve is, maybe modify the existing vbs script or have a
    new script to replace that string to -> https://chicago-dc-1:80, this change
    should reflect in all the above JNLP and resource files which are located in
    <Application_Home>\some-directory\ or sub directories which are in the same
    drive.


    Original vbs script usage is as below, from command prompt do following:

    replace.vbs^https://houston-dc-1:80^https://chicago-dc-1:80^C:\app_location\jboss\1.JNLP

    By running above script, it finds and replaces the string in only 1.JNLP,
    however what i would like to do is to run the command once and the changes
    should get reflected in all the JNLP and resouces files which are located in
    various directories and subdirectories on same drive.

    Would that be possible to do? Appreciate your help.

    Thanks,
    -Mitul




    --
    Mitul


    "Pegasus (MVP)" wrote:

    >
    > "Mitul" <Mitul@xxxxxx> wrote in message
    > news:F1FCC651-186D-4D1D-8424-AFAD9E49EBEC@xxxxxx

    > > basically, i have a vbscript which does a find and replace on single file,
    > > below i've shared the script code.
    > >
    > > '[cscript|wscript] replace.vbs Find Replacewith File
    > >
    > > 'Find . Required. Substring being searched for.
    > > 'Replacewith . Required. Replacement substring.
    > > 'File . Source and destination file for the replacement
    > >
    > > Dim FileName, Find, ReplaceWith, FileContents, dFileContents
    > > Find = WScript.Arguments(0)
    > > ReplaceWith = WScript.Arguments(1)
    > > FileName = WScript.Arguments(2)
    > >
    > > 'Read source text file
    > > FileContents = GetFile(FileName)
    > >
    > > 'replace all string In the source file
    > > dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)
    > >
    > > 'Compare source And result
    > > if dFileContents <> FileContents Then
    > > 'write result If different
    > > WriteFile FileName, dFileContents
    > >
    > > Wscript.Echo "Replace done."
    > > If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
    > > Wscript.Echo _
    > > ( (Len(dFileContents) - Len(FileContents)) /
    > > (Len(ReplaceWith)-Len(Find)) ) & _
    > > " replacements."
    > > End If
    > > Else
    > > Wscript.Echo "Searched string Not In the source file"
    > > End If
    > >
    > > 'Read text file
    > > function GetFile(FileName)
    > > If FileName<>"" Then
    > > Dim FS, FileStream
    > > Set FS = CreateObject("Scripting.FileSystemObject")
    > > on error resume Next
    > > Set FileStream = FS.OpenTextFile(FileName)
    > > GetFile = FileStream.ReadAll
    > > End If
    > > End Function
    > >
    > > 'Write string As a text file.
    > > function WriteFile(FileName, Contents)
    > > Dim OutStream, FS
    > >
    > > on error resume Next
    > > Set FS = CreateObject("Scripting.FileSystemObject")
    > > Set OutStream = FS.OpenTextFile(FileName, 2, True)
    > > OutStream.Write Contents
    > > End Function
    > >
    > >
    > > Now, I have a different requirement, I have atleast 20 different files
    > > stored at different folder locations in the same drive, these files have
    > > has
    > > got common strings in it, what I want is a script which would simply
    > > prompt
    > > the user to find "string" replace "string" at "location". For example,
    > > "Cscript.exe replace.vbs "serach string" "replace string" "C:\xyz\abc".
    > > Or
    > > Maybe I can dump all those 20 files in a text file and execute a batch
    > > file
    > > or a vbs to run on that text file and do a find replace.
    > >
    > > Could this be done? Any help is appreicated.
    > >
    > > Thanks,
    > > -Mitul
    >
    > What's "c:\xyz\abc"?
    >
    >
    >
      My System SpecsSystem Spec

  4.    30 Dec 2008 #4
    Pegasus \(MVP\) Guest

    Re: find and replace a specific string in multiple files


    You can do this with a VB Script file or with a batch file. Here is a batch
    file solution:

    @echo off
    set ext=JNLP
    set dir=D:\Application Home\some directory
    dir /b /s "%dir%\*.%ext%" > "%temp%\dir.txt"
    for /F "delims=" %%a in ('type "%temp%\dir.txt"') do (
    cscript //nologo c:\tools\replace.vbs https://houston-dc-1:80
    https://chicago-dc-1:80 "%%a"
    )

    "Mitul" <mits2000@xxxxxx> wrote in message
    news:72B381CC-51D0-4C68-A3EE-F744EE2C1E27@xxxxxx

    >> What's "c:\xyz\abc"?
    >
    > That's the location of files where that specific common string is to be
    > found and replaced by another string.
    >
    > Let me give you an example:
    >
    > I have several JNLP and resource files stored in
    > <Application_Home>\some-directory\1.jnlp, which has got a string ->
    > https://houston-dc-1:80
    >
    > This similar string is located in several files in the hierarcy of above
    > directories and sub-directories, example the strings are common in all
    > these
    > files, 2.JNLP, 3.JNLP, abc.resrouce, pqr.resource etc etc.
    >
    > What I want to achieve is, maybe modify the existing vbs script or have a
    > new script to replace that string to -> https://chicago-dc-1:80, this
    > change
    > should reflect in all the above JNLP and resource files which are located
    > in
    > <Application_Home>\some-directory\ or sub directories which are in the
    > same
    > drive.
    >
    >
    > Original vbs script usage is as below, from command prompt do following:
    >
    > replace.vbs^https://houston-dc-1:80^https://chicago-dc-1:80^C:\app_location\jboss\1.JNLP
    >
    > By running above script, it finds and replaces the string in only 1.JNLP,
    > however what i would like to do is to run the command once and the changes
    > should get reflected in all the JNLP and resouces files which are located
    > in
    > various directories and subdirectories on same drive.
    >
    > Would that be possible to do? Appreciate your help.
    >
    > Thanks,
    > -Mitul
    >
    >
    >
    >
    > --
    > Mitul
    >
    >
    > "Pegasus (MVP)" wrote:
    >

    >>
    >> "Mitul" <Mitul@xxxxxx> wrote in message
    >> news:F1FCC651-186D-4D1D-8424-AFAD9E49EBEC@xxxxxx

    >> > basically, i have a vbscript which does a find and replace on single
    >> > file,
    >> > below i've shared the script code.
    >> >
    >> > '[cscript|wscript] replace.vbs Find Replacewith File
    >> >
    >> > 'Find . Required. Substring being searched for.
    >> > 'Replacewith . Required. Replacement substring.
    >> > 'File . Source and destination file for the replacement
    >> >
    >> > Dim FileName, Find, ReplaceWith, FileContents, dFileContents
    >> > Find = WScript.Arguments(0)
    >> > ReplaceWith = WScript.Arguments(1)
    >> > FileName = WScript.Arguments(2)
    >> >
    >> > 'Read source text file
    >> > FileContents = GetFile(FileName)
    >> >
    >> > 'replace all string In the source file
    >> > dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)
    >> >
    >> > 'Compare source And result
    >> > if dFileContents <> FileContents Then
    >> > 'write result If different
    >> > WriteFile FileName, dFileContents
    >> >
    >> > Wscript.Echo "Replace done."
    >> > If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
    >> > Wscript.Echo _
    >> > ( (Len(dFileContents) - Len(FileContents)) /
    >> > (Len(ReplaceWith)-Len(Find)) ) & _
    >> > " replacements."
    >> > End If
    >> > Else
    >> > Wscript.Echo "Searched string Not In the source file"
    >> > End If
    >> >
    >> > 'Read text file
    >> > function GetFile(FileName)
    >> > If FileName<>"" Then
    >> > Dim FS, FileStream
    >> > Set FS = CreateObject("Scripting.FileSystemObject")
    >> > on error resume Next
    >> > Set FileStream = FS.OpenTextFile(FileName)
    >> > GetFile = FileStream.ReadAll
    >> > End If
    >> > End Function
    >> >
    >> > 'Write string As a text file.
    >> > function WriteFile(FileName, Contents)
    >> > Dim OutStream, FS
    >> >
    >> > on error resume Next
    >> > Set FS = CreateObject("Scripting.FileSystemObject")
    >> > Set OutStream = FS.OpenTextFile(FileName, 2, True)
    >> > OutStream.Write Contents
    >> > End Function
    >> >
    >> >
    >> > Now, I have a different requirement, I have atleast 20 different files
    >> > stored at different folder locations in the same drive, these files
    >> > have
    >> > has
    >> > got common strings in it, what I want is a script which would simply
    >> > prompt
    >> > the user to find "string" replace "string" at "location". For example,
    >> > "Cscript.exe replace.vbs "serach string" "replace string" "C:\xyz\abc".
    >> > Or
    >> > Maybe I can dump all those 20 files in a text file and execute a batch
    >> > file
    >> > or a vbs to run on that text file and do a find replace.
    >> >
    >> > Could this be done? Any help is appreicated.
    >> >
    >> > Thanks,
    >> > -Mitul
    >>
    >> What's "c:\xyz\abc"?
    >>
    >>
    >>

      My System SpecsSystem Spec

find and replace a specific string in multiple files

Similar Threads
Thread Forum
Any program that can search within 1000 mdi or tiff files for a specific text string?
Any program that can search within 1000 mdi or tiff files for a specific text string? Does anyone know any? I have a whole batch of mdi and tiff...
Software
find in files and replace
i am trying to replace values in files. I get this "in use" message. Is this because the gc command still has the file open? How do others do...
PowerShell
VBScript String Replace - Remove / Replace Characters in String
VBScript String Replace http://www.code-tips.com/2009/04/vbscript-string-clean-function-remove.html Remove or replace specific characters from...
VB Script
Replace Nth character in a string
Hello, is it possible to replace a character at a arbitrary position in a string ? For example : I want to replace the third and hundredth...
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
Re: String manipulation using replace etc.
Hi, The objective is to replace all "o" (alphabet - lower or upper) to "0" (number zero) in the string "opt". opt is a user input and hence...
VB Script
search and replace string
I have a folder full of php files. I want to replace all occurances of 'geocities.com' with 'tripod.com'. Is there a win32 shell or powershell...
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 21:22.
    .