回复 21# Batcher
感谢大佬指点迷津,问题已解决。以下是修改过的代码。- @echo off
- rem cd /d "%~dp0"
- setlocal EnableDelayedExpansion
-
- rem set "INPUT_FILE=file.txt"
- set "INPUT_FILE=D:\Program Files (x86)\input.ini"
- set "OUTPUT_FILE=D:\Program Files (x86)\output.ini"
- set "DATE_TIME=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%"
- set "BAK_FILE=D:\Program Files (x86)\common_bak%DATE_TIME%.ini"
- copy "%INPUT_FILE%" "%BAK_FILE%"
-
- rem 删除上次的输出文件
- if exist "%OUTPUT_FILE%" del "%OUTPUT_FILE%"
-
- for /f "usebackq tokens=1,2 delims=:" %%o in (`findstr /n .* "%INPUT_FILE%"`) do (
- rem echo %%p
- if "%%p" equ "" (
- rem 输出空行
- echo. >>"%OUTPUT_FILE%"
- ) else (
- for /f "tokens=1* delims==" %%c in ("%%p") do (
- if /i "%%c"=="[SERVER_DATA_3]" (
- set "Mod_flag=true"
- rem echo ----------------!Mod_flag!------------
- )
- rem 包含字符串“#""[”则仅输出%%c,否则进行修改
- echo %%c| findstr "# [" >nul && (
- set line=%%c
- ) || (
- rem 修改键值对
- if /i "!Mod_flag!" equ "true" (
- if /i "%%c" == "SERVER_USER" (
- rem echo %%d
- set line=%%c=cccccc
- set "ChangeU=true"
- ) else if /i "%%c" == "SERVER_PASSWORD" (
- rem echo %%d
- set line=%%c=cccccc
- set "ChangeP=true"
- ) else (
- set line=%%c=%%d
- )
- rem 全部修改完毕后,重置Mod_flag标识
- if /i "!ChangeU!" equ "true" (
- if /i "!ChangeP!" equ "true" (
- set "Mod_flag=false"
- )
- )
- ) else (
- set line=%%c=%%d
- )
- )
- REM 在此处修改line变量的值
- echo !line!>>"%OUTPUT_FILE%"
- )
- )
- )
-
- move /y "%OUTPUT_FILE%" "%INPUT_FILE%"
- endlocal
- echo "文件处理完成"
- pause
复制代码
|