用包含批处理脚本的注释(以;开头)行替换文件中的字符串

时间:2013-01-24 09:24:57

标签: batch-file

我有一个名为" abc.ini"内容如下:

;------------------------
; this file is a reffernce file
; you will get server path as below
admin_folder=E:\Temp\utils_630\admin**
VERSION=630
PORT=6304
IP of server=10.9.23.64

这里我需要将 utils_630 替换为 utils_640 我正在使用脚本来替换它。 但在输出中我没有得到;在行的开头,因为它在原始文件中。 我需要文件,因为它替换了所需的字符串。 对此的任何帮助都会很明显。谢谢!!!!!!!!!! 分号是行的开头,第4行以admin_folder开头。

1 个答案:

答案 0 :(得分:1)

你去吧

setlocal enabledelayedexpansion
for /f "eol= tokens=*" %%a in (abc.ini) do (
set line=%%a
echo !line! | find "utils_"
if not !errorlevel!==0 (
echo !line! | find "VERSION="
if not !errorlevel!==0 (
echo !line! >>new.ini
) else (
call :EXTRACT
)
) else (
call :EXTRACT
)
)
del abc.ini /f /q
ren new.ini abc.ini
pause >nul

:EXTRACT
set "line=!line:630=640!"
echo !line! >>new.ini