批处理脚本-查找和替换目录中多个文件中的文本,而无需用户安装任何程序或将其他文件添加到我的批处理脚本中 [英] Batch Script - Find and replace text in multiple files in a directory without asking user to install any program or add other files to my batch script
问题描述
我需要在文件夹及其子文件夹中的所有文本文件中搜索文本(例如FOO)并将其替换为另一个文本(例如BAR)的批处理文件.
I need batch file that searches for a text (eg., FOO) and replaces it with another text (eg., BAR) in all the text files within a folder and it's sub-folders.
我需要将此批处理文件提供给用户.因此,不可能要求用户安装其他任何东西,而且我也不想将其他文件添加到我的批处理脚本中,这是否还可以通过?我找到了许多解决此问题的答案,但每个人都建议安装其他程序或向批处理脚本中添加文件.有人可以帮我吗?
I need to give this batch file to the user. So, it is not possible to ask the user to install anything else and also i don't wanna add other files to my batch script, is that even passable? I found many answer for this issue but everyone advise to install other program or to add a file to the batch script . Can someone please help me with this?
推荐答案
这是一个简单而纯净的batch-文件解决方案-我们称之为replac.bat
:
Here is a simple and pure batch-file solution -- let us call it replac.bat
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~3" & rem // (path to root directory; third command line argument)
set "_MASK=%~4" & rem // (file search pattern; fourth command line argument)
set "_SEARCH=%~1" & rem // (search string; first command line argument)
set "_REPLAC=%~2" & rem // (replace string; second command line argument)
set "_CASE=#" & rem // (clear for case-insensitive search)
set "_RECURS=#" & rem // (clear for non-recursive search)
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (path to temporary file)
rem // Validate passed command line arguments, apply defaults:
if not defined _SEARCH exit /B 1
if not defined _ROOT set "_ROOT=."
if not defined _MASK set "_MASK=*.txt"
rem // Prepare `if` option (case-insensitivity) for later use:
if defined _CASE (set "IFSW=") else (set "IFSW=/I")
rem // Prepare `for` option (recursion) for later use:
if defined _RECURS (set "FOROPT=/R") else (set "FOROPT=")
rem // Change into root directory temporarily:
pushd "%_ROOT%" || exit /B 1
rem // Loop through all matching files in the directory tree:
for %FOROPT% %%F in ("%_MASK%") do (
rem // Write to temporary file:
> "%_TMPF%" (
set "FLAG="
rem /* Read current file line by line; use `findstr` to precede every line by
rem its line number and a colon `:`; this way empty lines appear non-empty
rem to `for /F`, which avoids them to be ignored; otherwise empty lines
rem became lost: */
for /F "delims=" %%L in ('findstr /N "^" "%%~fF"') do (
rem // Store current line text:
set "LINE=%%L"
setlocal EnableDelayedExpansion
rem // Remove line number prefix:
set "LINE=!LINE:*:=!"
rem // Skip replacement for empty line text:
if defined LINE (
rem /* Use `for /F` loop to avoid trouble in case search or replace
rem strings contain quotation marks `"`: */
for /F "tokens=* delims=*= eol=~" %%K in ("!_SEARCH!=!_REPLAC!") do (
rem // Split search and replace strings:
for /F "tokens=1 delims== eol==" %%I in ("%%K") do (
rem // Query to handle case-sensitivity:
if %IFSW% "!LINE!"=="!LINE:%%I=%%I!" (
rem // Detect whether replacement changes line:
if not "!LINE!"=="!LINE:%%K!" (
rem // Actually do the sub-string replacement:
set "LINE=!LINE:%%K!"
set "FLAG=#"
)
)
)
)
)
rem // Output the resulting line text:
echo(!LINE!
if defined FLAG (endlocal & set "FLAG=#") else (endlocal)
)
)
rem // Check whether file content would change upon replacement:
if defined FLAG (
rem // Move the temporary file onto the original one:
> nul move /Y "%_TMPF%" "%%~fF"
) else (
rem // Simply delete temporary file:
del "%_TMPF%"
)
)
popd
endlocal
exit /B
要使用此脚本,请分别在第一个命令行参数中提供搜索字符串,在第二个命令行参数中提供替换字符串.第三个参数定义默认为当前工作目录的根目录,第四个参数定义默认为*.txt
的文件模式:
To use this script, provide the search string as the first and the replace string as the second command line argument, respectively; the third argument defines the root directory which defaults to the current working directory, and the fourth one defines the file pattern which defaults to *.txt
:
replac.bat "Foo" "Bar"
以下限制适用:
- 所有匹配文件必须是具有Windows样式换行符的纯ASCII/ANSI文本文件;
- 文件中的行以及搜索和替换字符串都不得长于大约8190字节/字符;
- 搜索字符串不能为空,不能以
*
或~
开头,并且不能包含=
; - 搜索和替换字符串不得包含
!
或^
;
- all matching files must be plain ASCII/ANSI text files with Windows-style line-breaks;
- neither the lines in the files nor the search and replace strings may be longer than approximately 8190 bytes/characters;
- the search string must not be empty, it must not begin with
*
or~
, and it must not contain=
; - the search and replace strings must not contain
!
or^
;
这篇关于批处理脚本-查找和替换目录中多个文件中的文本,而无需用户安装任何程序或将其他文件添加到我的批处理脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!