0

I have a "search & replace within a file" script that works, but not as I expect. It has 2 problems:

  • it changes the oldtext with newtext regadless if the old text has more strings after it: I want to avoid this situation!

i'm calling that scrip from a batch file that contains:

cscript replace.vbs file.txt hello world

presuming file.txt contains:

house
abc.hellowide.use
abc.hello.use
hello
hellonurse

I want the script to change the file into this:

house
abc.hellowide.use
abc.world.use
wolrd
hellonurse

for now, the scrip result unwanted changes like this:

house
abc.worldwide.use
abc.world.use
wolrd
worldnurse

i want that only the exact old text will be replaced.

  • how can i make the script recurse subfolders under the root dir from where the script runs?

here is my script untill now:

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,1,1000,1)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
fthiella
  • 48,073
  • 15
  • 90
  • 106

2 Answers2

0

String (or token, or argument) are separated words by a space or double quotes, not dots. Then abc.hello.use aren't 3 words, it's a full word, so it's hard to accomplish whay you will.

Try this, but it don't work for .word. :

@echo off

:: By Elektro H@cker

Set "FILE=%~1"
Set "WORD_BEFORE=%~2"
Set "WORD_AFTER=%~3"

For /F "Tokens=*" %%# in ('Type "%FILE%"') do (
    Call :Replacer %%# 
    Call Set "Final_Line=%%FINAL_LINE:%FILE% %WORD_AFTER% %WORD_AFTER% =%%"
    call Echo %%FINAL_LINE:~1%%>>".\Output.txt"
    Call Set "Final_Line="
)

:Replacer
For %%@ in (%*) do ( 
    IF "%%@" EQU "%WORD_BEFORE%" (
        Call Set "Final_Line=%%FINAL_LINE%% %WORD_AFTER%"
    ) ELSE (
        Call Set "Final_Line=%%FINAL_LINE%% %%@"
    )
)
GOTO:EOF

For any other text manipulatios you can use my Batch subroutine:

@Echo OFF

:: TEXT MANIPULATOR ROUTINE v0.1
:: by Elektro H@cker

REM USE:
::TEXTMAN [ACTION] [IN LINE (Optional)] [FILE] [TEXT (Optional)]


REM ACTIONS:
REM 
REM  AL  = ADD_LEFT           * Add text to the beginning of a line
REM  AR  = ADD_RIGHT          * Add text to the end of a line
REM  E   = ERASE              * Delete a line
REM  I   = INSERT             * Add a empty line (Or a line with text)
REM  RL  = REPLACE_LINE       * Replace a line
REM  RS  = REPLACE_STRING     * Replace words of a line
REM  RSA = REPLACE_STRING_ALL * Replace words of all line
REM  C+  = CHARACTER_PLUS     * Delete the first "X" characters in all lines
REM  C-  = CHARACTER_LESS     * Delete the last  "X" characters in all lines
REM  L+  = LINE_PLUS          * Cut the first "X" lines
REM  L-  = LINE_LESS          * Cut the last  "X" lines


REM EXAMPLES:

:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
::
:: Deletes the line 3
:: Call :TEXTMAN E 3 "Test.txt"
::
:: Add a phrase to the beginning of line 3
:: Call :TEXTMAN AL 3 "Test.txt" "Elektro H@cker"
::
:: Add a phrase to the end of line 3
:: Call :TEXTMAN AR 3 "Test.txt" "Elektro H@cker"
::
:: Add a empty line at line 3
:: Call :TEXTMAN I 3 "Test.txt"
:: 
:: Add a line with a word at line 3
:: Call :TEXTMAN I 3 "Test.txt" "Elektro H@cker"
::
:: Replaces the line 3 with "Elektro H@cker"
:: Call :TEXTMAN RL 3 "Test.txt" "Elektro H@cker"
::
:: Replaces the words "Elektro" to "H@cker" in line 3
:: Call :TEXTMAN RS 3 "Test.txt" "Elektro" "H@cker"
::
:: Replaces the words "Elektro" to "H@cker" in all lines
:: Call :TEXTMAN RSA "Test.txt" "Elektro" "H@cker"
::
:: Deletes the first 3 characters in all lines
:: Call :TEXTMAN C+ 3 "Test.txt"
::
:: Deletes the last 3 characters in all lines
:: Call :TEXTMAN C- 3 "Test.txt"
::
:: Deletes the first 3 lines
:: Call :TEXTMAN L+ 3 "Test.txt"
::
:: Deletes the last 3 lines
:: Call :TEXTMAN L- 3 "Test.txt"
::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::

:TEXTMAN
(SET /A "A=0", "LINE=0", "TOTAL_LINES=0")  &  (CALL :%~1 %* || (ECHO Parametro incorrecto & Exit /B 1)) & (GOTO:EOF)
:AL
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %~4%%@ >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:AR
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %%@%~4 >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:E
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:I
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF     "%%LINE%%" EQU "%~2" (IF NOT "%~4" EQU "" (Echo %~4 >> "%~nx3.NEW") ELSE (Echo. >> "%~nx3.NEW"))" & Echo %%@ >> "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RL
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %~4 >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RS
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (CALL SET "STRING=%%@" && CALL ECHO %%STRING:%~4=%~5%% >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RSA
(For /F "usebackq tokens=*" %%@ in ("%~2") DO (CALL SET "STRING=%%@" && (CALL ECHO %%STRING:%~3=%~4%% >> "%~2.NEW"))) && (CALL :RENAMER "%~2") & (GOTO:EOF)
:C+
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set   "LINE=%%@" && (CALL ECHO %%LINE:~%~2%% >>    "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:C-
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set   "LINE=%%@" && (CALL ECHO %%LINE:~0,-%~2%% >> "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L+
(Call SET /A "A=%~2-1") && (Call TYPE "%~3" | @MORE +%%A%% > "%~nx3.NEW") && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L-
(FOR /F %%X IN ('TYPE "%~3"') DO (CALL SET /A "TOTAL_LINES+=1")) & (CALL SET /A "TOTAL_LINES-=%~2-1") & (For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" & Call echo " %%LINE%%!!| FINDSTR " %%TOTAL_LINES%% " && CALL :RENAMER "%~3" && GOTO:EOF || (Echo %%@ >> "%~nx3.NEW")))
:RENAMER
(REN "%~1" "%~nx1.BAK") & (MOVE /Y "%~nx1.BAK" "%TEMP%\" >NUL) & (REN "%~nx1.NEW" "%~nx1") & (GOTO:EOF)
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
0

You could use a regular expression to substitute "hello" to "world" only if "hello" is a single word. The regexp to match this is: \bhello\b. It will match hello and abc.hello.use but not not hellonurse and abc.hellowide.use.

This could be your script:

Const ForReading = 1
Const ForWriting = 2

Dim re
Set re = new regexp

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

re.Pattern = "\b" + strOldText + "\b"
re.Global = True
re.Multiline = True

strNewText = re.Replace(strText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

then you could call it this way to make it replace all *.txt files in current folder and subfolders:

for /r %a in (*.txt) do @cscript replace.vbs "%a" hello world

(there is still something to fix, for example in Arguments(1) or Arguments(2) contain special characters)

fthiella
  • 48,073
  • 15
  • 90
  • 106
  • One more thing. How can i make the script to permeate to all files in sudirectories under the Root dir ?(the place where i run the script). this abillity is crucial for my work to be done. – terminetorx Dec 06 '12 at 11:35
  • @terminetorx did you try to launch the script with the `for` command above? it should do what you need... but if there's any problem let me know – fthiella Dec 06 '12 at 14:46