Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - find and replace a specific string in multiple files

Reply
 
Old 12-29-2008   #1 (permalink)
Mitul


 
 

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
Old 12-29-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: find and replace a specific string in multiple files


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

> 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
Old 12-30-2008   #3 (permalink)
Mitul


 
 

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:
Quote:

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

> > 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
Old 12-30-2008   #4 (permalink)
Pegasus \(MVP\)


 
 

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
Quote:
Quote:

>> 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:
>
Quote:

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

>> > 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
Reply

Thread Tools


Similar Threads
Thread Forum
Replace Nth character in a string VB Script
Newbie question: replace multiple strings in multiple text files VB Script
Re: String manipulation using replace etc. VB Script
Re: String manipulation using replace etc. VB Script
search and replace string PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46