Windows 用于搜索和替换字符串的批处理文件
我需要一个批处理文件,该文件应在PC上搜索今天发布的名为“example.ini”的所有文件,并替换“example.ini”第二行中的字符串,它应替换Windows 用于搜索和替换字符串的批处理文件,windows,string,search,batch-file,Windows,String,Search,Batch File,我需要一个批处理文件,该文件应在PC上搜索今天发布的名为“example.ini”的所有文件,并替换“example.ini”第二行中的字符串,它应替换 SERVER\u IP=111.111.111.111与另一个服务器IPSERVER\u IP=222.222.222 这怎么可能呢?在一些示例文件上测试这一点 它将更改c:\上以及包含SERVER\u IP=212.83.63.108的子目录中的example.ini文件,并将其更改为SERVER\u IP=222.22.22.222 它使用
SERVER\u IP=111.111.111.111
与另一个服务器IPSERVER\u IP=222.222.222
这怎么可能呢?在一些示例文件上测试这一点 它将更改
c:\
上以及包含SERVER\u IP=212.83.63.108
的子目录中的example.ini
文件,并将其更改为SERVER\u IP=222.22.22.222
它使用一个名为repl.bat
from-
将repl.bat
与批处理文件放在同一文件夹中
@echo off
for /r c:\ %%a in (example.ini) do (
find "SERVER_IP=212.83.63.108" <"%%a" >nul && (
echo processing "%%a"
type "%%a"|repl "SERVER_IP=212\.83\.63\.108" "SERVER_IP=222.22.22.222" >"%%a.tmp"
move /y "%%a.tmp" "%%a" >nul
)
)
@echo关闭
对于/r c:\%%a在(example.ini)do中(
查找“服务器IP=212.83.63.108”nul&&(
回显处理“%%a”
键入“%%a”| repl“服务器IP=212\.83\.63\.108”“服务器IP=222.22.222”>“%%a.tmp”
移动/y“%%a.tmp”“%%a”>nul
)
)
在32位windows上提供Edlin(dos编辑器)
12
New text
e
是一个edlin脚本,在文件的第12行写入“新文本”。类型
edlin
然后
使用
edlin c:\folder\filename.ext < edlin.script
edlin c:\folder\filename.ext
您必须使用短文件名。文件必须在64K行以下。在今天在驱动器c的任何子目录中创建的名为example.ini的文件的第二行中,按String2更改String1:
@echo off
setlocal enableextensions enabledelayedexpansion
rem Arguments:
rem %1 = search string
rem %2 = replace string
rem [ %3 = file in where to replace ]
rem this parameters is internally used by the
rem batch file while processing
rem chech for parameters
if "%~2"=="" goto :EOF
rem retrieve parameters
set "_search=%~1"
set "_replace=%~2"
rem check if asked to replace in a file
rem this will be done recursively from this same batch
if not "%~3"=="" (
set "_file=%~3"
goto doFileReplace
)
forfiles /P C:\ /S /M example.ini /D +0 /C "cmd /c if @isdir==FALSE findstr /c:\"%_search%\" \"@path\" >nul && %~dpnx0 \"%_search%\" \"%_replace%\" @path "
goto :EOF
:doFileReplace
echo "%_file%"
set "_tempFile=%temp%\%~nx0.tmp"
break > "%_tempFile%"
(for /F "tokens=1,* delims=:" %%l in ('findstr /n /r "." "%_file%"') do (
if %%l EQU 2 (
set "_text=%%m"
echo !_text:%_search%=%_replace%!
) else (
echo %%m
)
))>>"%_tempFile%"
copy /Y "%_tempFile%" "%_file%" >nul
goto :EOF
SERVER\u IP=
在任何其他行上吗?SERVER\u IP=仅在第二行上。搜索整个驱动器并检查每个example.ini
以查找今天的日期可能会非常慢。有没有办法缩小搜索范围?example.ini
是否具有其他显著特征?可能是IP地址?SERVER_IP=212.83.63.108这是“example.ini”中的主IP批处理文件可以找到“example.ini”,但在尝试替换时,错误“repl”未被识别为内部或外部命令,可操作的程序或批处理虽然我在同一个文件夹中有repl.bat,但我要说的是您正在使用管理员权限提升批处理文件。将repl.bat
放在c:\windows
文件夹中,使其位于路径上。非常感谢它现在可以工作了,它能够替换我的“example.ini”中的字符串谢谢你所有的帮助effort@admdrewrepl.bat的代码已经托管在Stack Overflow和DosTips.com上,每次使用它都会创建大量的帖子,所以我回滚了您的编辑。
@echo off
setlocal enableextensions enabledelayedexpansion
rem Arguments:
rem %1 = search string
rem %2 = replace string
rem [ %3 = file in where to replace ]
rem this parameters is internally used by the
rem batch file while processing
rem chech for parameters
if "%~2"=="" goto :EOF
rem retrieve parameters
set "_search=%~1"
set "_replace=%~2"
rem check if asked to replace in a file
rem this will be done recursively from this same batch
if not "%~3"=="" (
set "_file=%~3"
goto doFileReplace
)
forfiles /P C:\ /S /M example.ini /D +0 /C "cmd /c if @isdir==FALSE findstr /c:\"%_search%\" \"@path\" >nul && %~dpnx0 \"%_search%\" \"%_replace%\" @path "
goto :EOF
:doFileReplace
echo "%_file%"
set "_tempFile=%temp%\%~nx0.tmp"
break > "%_tempFile%"
(for /F "tokens=1,* delims=:" %%l in ('findstr /n /r "." "%_file%"') do (
if %%l EQU 2 (
set "_text=%%m"
echo !_text:%_search%=%_replace%!
) else (
echo %%m
)
))>>"%_tempFile%"
copy /Y "%_tempFile%" "%_file%" >nul
goto :EOF