如何在文件中仅搜索和替换精确的字符串大小写

时间:2012-11-28 12:00:09

标签: vbscript batch-file batch-rename

我有一个“搜索和替换文件”脚本可以正常工作,但不像我期望的那样。它有两个问题:

  • 如果旧文本后面有更多字符串,它将oldtext更改为newtext无效:我想避免这种情况!

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

  

cscript replace.vbs file.txt hello world

假设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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

只有当“hello”是单个单词时,才可以使用正则表达式将“hello”替换为“world”。与此匹配的正则表达式为:\bhello\b。它将与helloabc.hello.use匹配,但不会与hellonurseabc.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)中包含特殊字符)