0

我有一个“在文件中搜索和替换”脚本有效,但不是我期望的那样。它有两个问题:

  • 如果旧文本后面有更多字符串,它会更改oldtextwith newtextregadless:我想避免这种情况!

我正在从包含以下内容的批处理文件中调用该脚本:

cscript replace.vbs file.txt 你好世界

假设 file.txt 包含:

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

我希望脚本将文件更改为:

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

目前,脚本会导致不需要的更改,如下所示:

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

我希望只替换确切的旧文本。

  • 如何使脚本从脚本运行的根目录下递归子文件夹?

到目前为止,这是我的脚本:

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
4

2 回答 2

0

字符串(或标记或参数)是由空格或双引号分隔的单词,而不是点。那么abc.hello.use不是 3 个词,它是一个完整的词,所以很难做到随心所欲。

试试这个,但它不适用于.

@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

对于任何其他文本操作,您可以使用我的 Batch 子例程:

@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)
于 2012-11-28T13:19:31.353 回答
0

只有当“hello”是一个单词时,您才可以使用正则表达式将“hello”替换为“world”。与此匹配的正则表达式是:\bhello\b. 它将匹配helloandabc.hello.use但不匹配hellonurseand abc.hellowide.use

这可能是您的脚本:

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

那么您可以这样调用它,使其替换当前文件夹和子文件夹中的所有 *.txt 文件:

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

(还有一些问题需要修复,例如在 Arguments(1) 或 Arguments(2) 中包含特殊字符)

于 2012-11-28T13:40:56.640 回答