如何用批处理文件匹配下一个字符替换类文件指定变量值
Got it, let's tackle your problem. Your original script does a straight string replacement, but what you need is to target a specific variable name (like ISDEV) and update only its assigned value (the part between = and ; in lines like public boolean ISDEV = false;).
First, let's fix the critical flaw in your original script: you can't read from and write to the same file in a single FOR /F loop—this will overwrite the file before it's fully read, leading to data loss. We'll use a temporary file to avoid this.
Here's the revised script that does exactly what you need:
@echo off setlocal enabledelayedexpansion REM Set input parameters and file paths set "targetVar=%1" set "newValue=%2" set "sourceFile=Input.txt" set "tempFile=temp_modified.txt" REM Validate input parameters if "%targetVar%"=="" ( echo Error: Please provide the variable name as the first argument. echo Example: script.bat ISDEV true exit /b 1 ) if "%newValue%"=="" ( echo Error: Please provide the new value as the second argument. echo Example: script.bat ISDEV true exit /b 1 ) REM Process each line of the source file for /f "delims=" %%l in ('type "%sourceFile%"') do ( set "currentLine=%%l" REM Check if the line contains our target variable's assignment echo !currentLine! | findstr /i /c:"%targetVar% = " >nul if not errorlevel 1 ( REM For matching lines: replace the value between = and ; REM Step 1: Remove the trailing semicolon temporarily set "tempLine=!currentLine:;=!" REM Step 2: Split the line at the = sign to separate variable part and value part for /f "tokens=1,* delims==" %%a in ("!tempLine!") do ( REM Reconstruct the line with the new value and add back the semicolon set "modifiedLine=%%a=%newValue%;" echo !modifiedLine!>>"%tempFile%" ) goto skipOriginalLine ) REM Write the original line if no match echo !currentLine!>>"%tempFile%" :skipOriginalLine ) REM Replace the original file with the modified temp file move /y "%tempFile%" "%sourceFile%" >nul echo Success: Variable "%targetVar%" has been updated to "%newValue%" in "%sourceFile%" endlocal
Key Details Explained:
- Delayed Expansion: Enabled with
setlocal enabledelayedexpansionso we can modify and access variables inside theFORloop correctly (using!var!instead of%var%). - Temporary File: We write all modified content to
temp_modified.txtfirst, then replace the original file—this prevents data corruption from simultaneous read/write operations. - Targeted Matching: Uses
findstrto check if a line contains the target variable followed by=(to avoid partial matches likeISDEVELOPER). - Value Replacement: For matching lines, we split the line at the
=sign, keep everything before it, append the new value, and add back the trailing semicolon.
How to Use:
Run the script from the command line with two arguments:
your_script_name.bat ISDEV true
This will update lines like public boolean ISDEV = false; to public boolean ISDEV = true;.
Notes:
- The script is case-insensitive for variable matching (thanks to
/iinfindstr), but you can remove/iif you need exact case matching. - Works for lines where the variable assignment follows the pattern
[variable name] = [value];—adjust thefindstrpattern if your class files use different spacing (likeISDEV=false;without spaces).
内容的提问来源于stack exchange,提问作者Prabhakaran

