![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 > > 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 Specs![]() |
| | #4 (permalink) |
| | 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 >> >> 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? 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 Specs![]() |
| | #5 (permalink) |
| | 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" Quote: > objFile.Write temp > objFile.Close |
My System Specs![]() |
![]() |
| 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 | |||