You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用批处理文件匹配下一个字符替换类文件指定变量值

Batch Script to Modify Variable Values in Class Files by Matching Variable Names

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 enabledelayedexpansion so we can modify and access variables inside the FOR loop correctly (using !var! instead of %var%).
  • Temporary File: We write all modified content to temp_modified.txt first, then replace the original file—this prevents data corruption from simultaneous read/write operations.
  • Targeted Matching: Uses findstr to check if a line contains the target variable followed by = (to avoid partial matches like ISDEVELOPER).
  • 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 /i in findstr), but you can remove /i if you need exact case matching.
  • Works for lines where the variable assignment follows the pattern [variable name] = [value];—adjust the findstr pattern if your class files use different spacing (like ISDEV=false; without spaces).

内容的提问来源于stack exchange,提问作者Prabhakaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:17:42