3

I am trying to replace text in a file repeatedly. However, once I replace the text once, it gets replaced again.

I am using a .vbs file that replaces text, and a .bat file that executes the .vbs file repeatedly. I am trying to execute it in such a way that after text is replaced, it is not replaced again.

I have one .vbs file called replace.vbs that I got from this link:

How can you find and replace text in a file using the Windows command-line environment?

Const ForReading = 1    
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close

strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText  'WriteLine adds extra CR/LF
objFile.Close

And I have a .bat file that executes replace.vbs repeatedly (the full script runs replace.vbs 56 times)

SET abcpath=C:\test\myFile.txt

cscript replace.vbs %abcpath% "Hello World" "Testing"
cscript replace.vbs %abcpath% "Testing" "Text"

PAUSE

The text in myFile.txt is "Hello World Testing" the intended output is "Testing Text" however the output is "Text Text". How can I have the second time replace.vbs is executed ignore all text that was replaced during the first time it was executed?

It is possible that There is a simpler solution that is evading me, so I will back up a step. What I am actually trying to do is take a text file that has the letters a to g (upper and lowercase) followed by up to 3 apostrophes or up to 4 commas (at no point does it have commas and apostrophes). there are 52 combinations and I am trying to map each possible combination to one letter (52 different letters because its case sensitive)

  • 1
    Well, for a start, why don't you replace "Testing" at first and then replace "Hello World" after that and see if that works for you. – 41686d6564 stands w. Palestine Jun 29 '19 at 18:48
  • I just used "Hello World" and "Testing" as examples to avoid confusion. (overly specific details incoming) what I am actually trying to do is take a text file that has the letters a to g (upper and lowercase) followed by up to 3 apostrophes or up to 4 commas (at no point does it have commas and apostrophes). there are 52 combinations and I am trying to map each possible combination to one letter (52 different letters because its case sensitive) – Daniel Garcia Jun 29 '19 at 18:56
  • 1
    This seems like key information. Please [edit] the question to add this information with examples so it's easier to understand. – 41686d6564 stands w. Palestine Jun 29 '19 at 19:10
  • This sounds to be solvable with proper RegEx support, don't reinvent the wheel - take a look at [JRepl.bat](https://www.dostips.com/forum/viewtopic.php?t=6044) BTW [edit] your question to contain additional information including some samples. –  Jun 29 '19 at 20:16

2 Answers2

1

Without seeing the actual sequences that you are trying to map it is difficult to suggest what to do, but in general map the longest sequences first. Also, if a sequence is mapped to something that could be replaced by another mapping, temporarily map it first to some unique sequence that will not be replaced by subsequent mappings. Do that for every mapping. Then, once all mapping have been done, replace the temporary unique sequences by what you wanted them mapped to initially.

In your example, you would map "Hello World" to some sequence that does not appear in your data, say "--1--", and once all mappings are done, map "--1--" to "Testing".

Here is how you would modify your replave.vbs script to implement what I am suggesting.

SET abcpath=C:\test\myFile.txt

cscript replace.vbs %abcpath% "Hello World" "--1--"
cscript replace.vbs %abcpath% "Testing" "--2--"

cscript replace.vbs %abcpath% "--1--" "Testing"
cscript replace.vbs %abcpath% "--2--" "Text"

PAUSE
RobertBaron
  • 2,817
  • 1
  • 12
  • 19
-1

I think the initial Replace() results in myFile.txt containing: "Testing Testing"

When you call the script a second time, it finds both "Testing" values and replaces them with "Text."

You'd have to modify your batch file like the following to get your desired output:

SET abcpath=C:\test\myFile.txt

cscript replace.vbs %abcpath% "Hello" "Testing"
cscript replace.vbs %abcpath% "World Testing" "Text"

PAUSE

And the replacement sequence would be something like:

1. Hello World Testing
2. Testing World Testing
3. Testing Text
Mark Moretto
  • 2,344
  • 2
  • 15
  • 21