![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 approximate key, so what I decided to do was export that key, then do a search and replace on the .reg file (since it's a text file and since I know a wee bit more about editing text files than I do the registry), then import the file back into the registry. So now my question is how to do multiple searches in a more efficient way than what I'm currently doing. What I need to search for is the following string (and (Chr(34) is a double quote): Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "#" & Chr(34) ....where # can be any digit from 0-9. My replace string is always the same; only the search string will be different. Below is my current way of doing it, where I'm opening the file, searching for a string and replacing it, then closing the file, then repeating that process X number of times. Ideally I'd like to make the code more flexible so that if I had to search, say, 100 different numbers, or 1,000, I wouldn't have to open & close the file a hundred or a thousand times. Here's my current code: Const ForReading = 1 Const ForWriting = 2 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading) ' 1st search and replace strSearch1 = Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "0" & Chr(34) strReplace = Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "3" & Chr(34) strText = objFile.ReadAll objFile.Close strNewText = Replace(strText, strSearch1, strReplace) Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForWriting) objFile.WriteLine strNewText objFile.Close ' 2nd search and replace Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading) strSearch2 = Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "1" & Chr(34) strText = objFile.ReadAll objFile.Close strNewText = Replace(strText, strSearch2, strReplace) Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForWriting) objFile.WriteLine strNewText objFile.Close ....and so on for each iteration of the search string. Is there a better way I can do this, maybe with regular expressions? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: multiple search and replaces in a text file "Tony Logan" <TonyLogan@xxxxxx> wrote in message news:8E817948-E002-4671-8FF8-36400AE3A452@xxxxxx Quote: > 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 > approximate key, so what I decided to do was export that key, then do a > search and replace on the .reg file (since it's a text file and since I > know > a wee bit more about editing text files than I do the registry), then > import > the file back into the registry. > > So now my question is how to do multiple searches in a more efficient way > than what I'm currently doing. > > What I need to search for is the following string (and (Chr(34) is a > double > quote): > Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "#" & Chr(34) > > ...where # can be any digit from 0-9. My replace string is always the > same; > only the search string will be different. > > Below is my current way of doing it, where I'm opening the file, searching > for a string and replacing it, then closing the file, then repeating that > process X number of times. Ideally I'd like to make the code more flexible > so > that if I had to search, say, 100 different numbers, or 1,000, I wouldn't > have to open & close the file a hundred or a thousand times. Here's my > current code: > > Const ForReading = 1 > Const ForWriting = 2 > > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading) > > ' 1st search and replace > strSearch1 = Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "0" & > Chr(34) > strReplace = Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "3" & > Chr(34) > strText = objFile.ReadAll > objFile.Close > strNewText = Replace(strText, strSearch1, strReplace) > > Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForWriting) > objFile.WriteLine strNewText > objFile.Close > > > ' 2nd search and replace > Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading) > > strSearch2 = Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "1" & > Chr(34) > strText = objFile.ReadAll > objFile.Close > strNewText = Replace(strText, strSearch2, strReplace) > > Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForWriting) > objFile.WriteLine strNewText > objFile.Close > > ...and so on for each iteration of the search string. > > Is there a better way I can do this, maybe with regular expressions? efficiencies I think would be worth applying. First, your code does things like: open file write strNewText into file close file open file read file into strText close file this is functionally identical to this statement: [strText = strNewText], such that if you replaced each such instance with this command, the effect would be the same. But that is still not the best solution... Second, your script consists of blocks of text that are virtually identical except in a few details. A better way to model that type of structure would be either a loop that iterates through a list of the individual search strings or a series of calls to a subroutine that contained the common code, and all that preceded by an open/readall/close and followed by an open/write/close. Here is a bit of an idea: strReplacePfx = """WakeUpModeCap""=" strReplace = """3""" Searchlist = array( """0""","""1""","""2""","""4""","""5""") subReadFromFile strData for each strSearch in searchlist strData = fcnReplace( strData, strReplacePfx & strSearch, strReplacePfx & strReplace ) next subWriteToFile strData The two sub references could either be written as subs or replaced by the actual code such as you have used. /Al |
My System Specs![]() |
| | #3 (permalink) |
| | Re: multiple search and replaces in a text file Tony Logan wrote: Quote: > > What I need to search for is the following string (and (Chr(34) is a > double quote): > Chr(34) & "WakeUpModeCap" & Chr(34) & "=" & Chr(34) & "#" & Chr(34) > > ...where # can be any digit from 0-9. My replace string is always the > same; only the search string will be different. > Const ForWriting = 2 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading) strText = objFile.ReadAll objFile.Close Set objRE = New RegExp objRE.Pattern = "(\x22WakeUpModeCap\x22=\x22)\d(\x22)" objRE.Global = True strNewText = objRE.Replace(strText, "$13$2") Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForWriting) objFile.WriteLine strNewText objFile.Close -- Steve Too much sanity may be madness. And maddest of all, to see life as it is and not as it should be! -Miguel de Cervantes |
My System Specs![]() |
| | #4 (permalink) |
| | Re: multiple search and replaces in a text file Steve and Al, thanks for the replies. Steve, your code looks like exactly what I need. But... something I noticed after my first post is that any search and replace I do on my chunk of .reg file is wiping out a big portion of the file, including the text I'm trying to search and replace. It does this no matter whether I use Steve's code, or whether I used a more simple search and replace expression that only looks for one string and replaces it with another string (I'm using a Hey Scripting Guy! example, from http://www.microsoft.com/technet/scr...5/hey0208.mspx). The truly weird thing is that when I set the search and replace strings to strings not in the .reg file, you'd think my result would be the original ..reg file without any changes, but nope, there's still a big section of the file missing. I started thinking that maybe I was getting this result because of working with a reg file, but then I tested with a very simple text file containing just a couple lines from the start of the reg file, and I get the same end result: missing text and gibberish. For example, if my text file looks like this: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}] And then I search for "Dan" and replace it with "Bob", you'd think I'd get the above without any changes as my end result. Instead I get this: W 318}] One last thing I noticed: if I make my test text file just a bunch of names separated by returns, my resulting text file is fine. So are the line breaks in the reg file possibly causing my results, and if so, is there a way to deal with it? "Steve" wrote: Quote: > Const ForReading = 1 > Const ForWriting = 2 > > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading) > strText = objFile.ReadAll > objFile.Close > > Set objRE = New RegExp > objRE.Pattern = "(\x22WakeUpModeCap\x22=\x22)\d(\x22)" > objRE.Global = True > strNewText = objRE.Replace(strText, "$13$2") > > Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForWriting) > objFile.WriteLine strNewText > objFile.Close > > -- > Steve > > Too much sanity may be madness. And maddest of all, to see life as it is > and not as it should be! -Miguel de Cervantes |
My System Specs![]() |
| | #5 (permalink) |
| | Re: multiple search and replaces in a text file A bit more info: 1) Changing the line endings doesn't make a difference. Took the same 2-line chunk of reg file I posted in the previous message, manually changed the line endings in the file, and got the same result. 2) Changing the search and replace strings doesn't make a bit of difference on the end result. I still wind up with the same chunk of missing text no matter whether I do a search and replace with "test" and "blah" or "Black Sabbath" and "Led Zeppelin". Here's the code I'm using, which I copied straight from the Hey Scripting Guy page and modified with my own search and replace terms: Const ForReading = 1 Const ForWriting = 2 strFilePath = "C:\Test.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) strText = objFile.ReadAll objFile.Close strNewText = Replace(strText, "Black Sabbath", "Led Zeppelin") Set objFile = objFSO.OpenTextFile(strFilePath, ForWriting) objFile.WriteLine strNewText objFile.Close |
My System Specs![]() |
| | #6 (permalink) |
| | Re: multiple search and replaces in a text file Tony Logan wrote: Quote: > > A bit more info: > 1) Changing the line endings doesn't make a difference. Took the same > 2-line chunk of reg file I posted in the previous message, manually > changed the line endings in the file, and got the same result. OpenTextFile defaults to ASCII format. You need to open the file in Unicode format: Const ForReading = 1 Const ForWriting = 2 Const Unicode = -1 strFilePath = "C:\Test.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading, False, Unicode) strText = objFile.ReadAll objFile.Close strNewText = Replace(strText, "Black Sabbath", "Led Zeppelin") Set objFile = objFSO.OpenTextFile(strFilePath, ForWriting, False, Unicode) objFile.WriteLine strNewText objFile.Close -- Steve The only way to discover the limits of the possible is to go beyond them into the impossible. -Arthur C. Clarke |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Search and Replace text file very slow | PowerShell | |||
| typing replaces text-fix | Live Mail | |||
| Multiple -replaces | PowerShell | |||
| Search for Text in UTF-8 File | Vista General | |||
| Search and replace in a text file? | PowerShell | |||