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/replace on file

Reply
 
Old 05-01-2009   #1 (permalink)
James


 
 

find/replace on file

Hello,

I'm new to vbscript. I want to open a text file find 2 paragraph makrs
and replace them with some text. When I run this code everything in the
file gets deleted. Can anyone tell me why and suggest a way fixing
this. If I remove the '{2}' from line 10 then script delete one
paragraph mark and replace it with text but if I try to specity 2 or
more paragraph marks all file contents are deleted. Is this regular
expression correct or is it the way the file is read into memory? I also
want to insert text at the start of the file and at the end of the file.
Any suggestions.



'Declare all the variables
Dim re, strSearchString, strNewText

Const ForReading = 1
Const ForWriting = 2

Set re = New RegExp


re.Pattern = "\n{2}"
re.IgnoreCase = True
re.Global = True


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
strSearchString = objFile.ReadAll
objFile.Close
If re.test(strSearchString) then
strNewText = re.replace(strSearchString, "','")
Else
End If

Set objFile = objFSO.OpenTextFile("C:\scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close


Thanks,

James

My System SpecsSystem Spec
Old 05-01-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: find/replace on file


"James" <jwanders@xxxxxx> wrote in message
news:zJEKl.7926$WT7.6910@xxxxxx
Quote:

> Hello,
>
> I'm new to vbscript. I want to open a text file find 2 paragraph makrs
> and replace them with some text. When I run this code everything in the
> file gets deleted. Can anyone tell me why and suggest a way fixing this.
> If I remove the '{2}' from line 10 then script delete one paragraph mark
> and replace it with text but if I try to specity 2 or more paragraph marks
> all file contents are deleted. Is this regular expression correct or is it
> the way the file is read into memory? I also want to insert text at the
> start of the file and at the end of the file. Any suggestions.
>
>
>
> 'Declare all the variables
> Dim re, strSearchString, strNewText
>
> Const ForReading = 1
> Const ForWriting = 2
>
> Set re = New RegExp
>
>
> re.Pattern = "\n{2}"
> re.IgnoreCase = True
> re.Global = True
>
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
> strSearchString = objFile.ReadAll
> objFile.Close
> If re.test(strSearchString) then
> strNewText = re.replace(strSearchString, "','")
> Else
> End If
>
> Set objFile = objFSO.OpenTextFile("C:\scripts\Text.txt", ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
>
>
> Thanks,
>
> James
Since you wish to search for a fixed text strings, you don't really need a
regular expression. You should also use "write" instead of "writeline"
unless you wish to add a terminating paragraph marker. The following code
will do the job nicely.

Dim strSearchString, objFSO, objFile
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("d:\Test.txt", ForReading)
strSearchString = objFile.ReadAll
objFile.Close

Set objFile = objFSO.OpenTextFile("c:\Test.txt", ForWriting)
objFile.Write Replace(strSearchString, VbCrLf, "','")
objFile.Close


My System SpecsSystem Spec
Old 05-18-2009   #3 (permalink)
James


 
 

Re: find/replace on file

Pegasus [MVP] wrote:
Quote:

> "James" <jwanders@xxxxxx> wrote in message
> news:zJEKl.7926$WT7.6910@xxxxxx
Quote:

>> Hello,
>>
>> I'm new to vbscript. I want to open a text file find 2 paragraph makrs
>> and replace them with some text. When I run this code everything in the
>> file gets deleted. Can anyone tell me why and suggest a way fixing this.
>> If I remove the '{2}' from line 10 then script delete one paragraph mark
>> and replace it with text but if I try to specity 2 or more paragraph marks
>> all file contents are deleted. Is this regular expression correct or is it
>> the way the file is read into memory? I also want to insert text at the
>> start of the file and at the end of the file. Any suggestions.
>>
>>
>>
>> 'Declare all the variables
>> Dim re, strSearchString, strNewText
>>
>> Const ForReading = 1
>> Const ForWriting = 2
>>
>> Set re = New RegExp
>>
>>
>> re.Pattern = "\n{2}"
>> re.IgnoreCase = True
>> re.Global = True
>>
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
>> strSearchString = objFile.ReadAll
>> objFile.Close
>> If re.test(strSearchString) then
>> strNewText = re.replace(strSearchString, "','")
>> Else
>> End If
>>
>> Set objFile = objFSO.OpenTextFile("C:\scripts\Text.txt", ForWriting)
>> objFile.WriteLine strNewText
>> objFile.Close
>>
>>
>> Thanks,
>>
>> James
>
> Since you wish to search for a fixed text strings, you don't really need a
> regular expression. You should also use "write" instead of "writeline"
> unless you wish to add a terminating paragraph marker. The following code
> will do the job nicely.
>
> Dim strSearchString, objFSO, objFile
> Const ForReading = 1
> Const ForWriting = 2
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("d:\Test.txt", ForReading)
> strSearchString = objFile.ReadAll
> objFile.Close
>
> Set objFile = objFSO.OpenTextFile("c:\Test.txt", ForWriting)
> objFile.Write Replace(strSearchString, VbCrLf, "','")
> objFile.Close
>
>
Thanks for that. How would I specify a search for 2 carriage returns?
Also I want to find the start of the file and end of the file and insert
text. How do I search for this?

James
My System SpecsSystem Spec
Old 05-18-2009   #4 (permalink)
Al Dunbar


 
 

Re: find/replace on file


"James" <jwanders@xxxxxx> wrote in message
news:NsfQl.40460$%_2.14511@xxxxxx
Quote:

> Pegasus [MVP] wrote:
Quote:

>> "James" <jwanders@xxxxxx> wrote in message
>> news:zJEKl.7926$WT7.6910@xxxxxx
Quote:

>>> Hello,
>>>
>>> I'm new to vbscript. I want to open a text file find 2 paragraph makrs
>>> and replace them with some text. When I run this code everything in the
>>> file gets deleted. Can anyone tell me why and suggest a way fixing
>>> this. If I remove the '{2}' from line 10 then script delete one
>>> paragraph mark and replace it with text but if I try to specity 2 or
>>> more paragraph marks all file contents are deleted. Is this regular
>>> expression correct or is it the way the file is read into memory? I also
>>> want to insert text at the start of the file and at the end of the file.
>>> Any suggestions.
>>>
>>>
>>>
>>> 'Declare all the variables
>>> Dim re, strSearchString, strNewText
>>>
>>> Const ForReading = 1
>>> Const ForWriting = 2
>>>
>>> Set re = New RegExp
>>>
>>>
>>> re.Pattern = "\n{2}"
>>> re.IgnoreCase = True
>>> re.Global = True
>>>
>>>
>>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>>> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
>>> strSearchString = objFile.ReadAll
>>> objFile.Close
>>> If re.test(strSearchString) then
>>> strNewText = re.replace(strSearchString, "','")
>>> Else
>>> End If
>>>
>>> Set objFile = objFSO.OpenTextFile("C:\scripts\Text.txt", ForWriting)
>>> objFile.WriteLine strNewText
>>> objFile.Close
>>>
>>>
>>> Thanks,
>>>
>>> James
>>
>> Since you wish to search for a fixed text strings, you don't really need
>> a regular expression. You should also use "write" instead of "writeline"
>> unless you wish to add a terminating paragraph marker. The following code
>> will do the job nicely.
>>
>> Dim strSearchString, objFSO, objFile
>> Const ForReading = 1
>> Const ForWriting = 2
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.OpenTextFile("d:\Test.txt", ForReading)
>> strSearchString = objFile.ReadAll
>> objFile.Close
>>
>> Set objFile = objFSO.OpenTextFile("c:\Test.txt", ForWriting)
>> objFile.Write Replace(strSearchString, VbCrLf, "','")
>> objFile.Close
>>
>>
> Thanks for that. How would I specify a search for 2 carriage returns?
This is not really a "search" for two carriage returns, but it will replace
each paired set of carriage returns with "~~~" and each remaining single
carriage return with "','":

dim temp
Set objFile = objFSO.OpenTextFile("c:\Test.txt", ForWriting)
temp = Replace(strSearchString, VbCrLf & VbCrLf, "~~~")
temp = Replace(temp, VbCrLf, "','")
objFile.Write temp
objFile.Close
Quote:

> Also I want to find the start of the file and end of the file and insert
> text. How do I search for this?
No need to "search" for these things as they are sitting right in front of
you (speaking words of wisdom... oh, let's just let it be ;-):

dim temp
Set objFile = objFSO.OpenTextFile("c:\Test.txt", ForWriting)
temp = Replace(strSearchString, VbCrLf & VbCrLf, "~~~")
temp = Replace(temp, VbCrLf, "','")
temp = "at beginning" & VBCRLF & test & VBCRLF & "at end"
objFile.Write temp
objFile.Close

/Al


My System SpecsSystem Spec
Old 05-18-2009   #5 (permalink)
ekkehard.horner


 
 

Re: find/replace on file

Al Dunbar schrieb:
[...]
Quote:

> dim temp
> Set objFile = objFSO.OpenTextFile("c:\Test.txt", ForWriting)
> temp = Replace(strSearchString, VbCrLf & VbCrLf, "~~~")
> temp = Replace(temp, VbCrLf, "','")
> temp = "at beginning" & VBCRLF & test & VBCRLF & "at end"
temp = "at beginning" & VBCRLF & temp & VBCRLF & "at end"
Quote:

> objFile.Write temp
> objFile.Close
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
find and replace a specific string in multiple files VB Script
Advanced find and replace using VBScript VB Script
Find & Replace in MSSQL Tables through PowerShell PowerShell
Find and Replace Utility ? Vista General
Find/Replace 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