使用批处理文件将版本号读写到文件中

时间:2016-08-11 10:45:56

标签: batch-file

我每次运行时都会尝试编写一个批处理文件来增加版本号,但是我很担心"对于/ f"以及使用命令提示符测试批处理文件时的行为。请帮我解决一下这个。 这是我的批处理文件

for /f "tokens=2,3 " %%i in (version.h) do (
    set /a number=%%j+1
    echo %%i
    echo %%j
    echo %number%
    if %%i==REVISION (
        echo line1
        echo #define %%i "%number%" >> temp.file
    ) else (
        echo line2
        echo #define %%i %%j >> temp.file
    )
)
del /q version.h
ren temp.file version.h

这是我的version.h

#define MAJOR "1" 
#define MINOR "0" 
#define REVISION "242" 

批处理文件只能在第一次运行时产生正确的结果(#define REVISION" 243"),并且在第二次运行时产生奇怪的结果(#define REVISION" 0" )。第三次跑步的结果是正确的(" #define REVISION" 244"),但第四次跑步再次变得奇怪(#define REVISION" 1"),等等。 似乎我没有解析正确的字符串,所以每次都无法得到正确的结果。 我键入"表示/?"在命令提示符下阅读帮助信息,但仍然无法理解,请帮我这个。任何回复都会很感激!

1 个答案:

答案 0 :(得分:0)

以下脚本执行您想要的操作并保留目标文件中存在的空行和特殊字符。没有涉及临时文件,文件修改就地完成。

所以这里是代码 - 引用它的工作原理的解释性说明:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "FILE=%~1" & rem // (provide the target file as command line argument)
set "DIRECTIVE=#define" & rem // (name of the directive to search)
set "DEFINITION=REVISION" & rem // (name of the definition to search)
set "CASESENS=" & rem // (set to non-empty for case-sensitive searches)
set "QUOTED="^" & rem // (set to non-empty for quoting returned number)

rem // Resolve arguments and options:
if not defined FILE ((>&2 echo ERROR: no file specified!) & exit /B 1)
if defined CASESENS (set "CASESENS=") else (set "CASESENS=/I")
if defined QUOTED (set "QUOTED="^") else (set "QUOTED=")

rem // Loop through all lines in the target file:
setlocal EnableDelayedExpansion
for /F delims^=^ eol^= %%L in ('
    rem/ /* Prefix lines with line numbers to not lose empty ones; ^& ^
    rem/    after having read file, deplete its entire content: */ ^& ^
    findstr /N /R "^^" "!FILE!" ^& ^> "!FILE!" break
') do (
    endlocal
    set "FLAG="
    set "LINE=%%L"
    rem // Split line into three tokens:
    for /F "tokens=1-3 eol= " %%I in ("%%L") do (
        set "FIELD1=%%I"
        set "FIELD2=%%J"
        set "NUMBER=%%~K"
        setlocal EnableDelayedExpansion
        rem // Check first token for matching directive name:
        if %CASESENS% "!FIELD1:*:=!"=="!DIRECTIVE!" (
        rem // Check second token for matching definition name:
            if %CASESENS% "!FIELD2!"=="!DEFINITION!" (
                endlocal
                rem // Increment number of third token:
                set /A "NUMBER+=1"
                set "FLAG=#"
                setlocal EnableDelayedExpansion
            )
        )
        endlocal
    )
    setlocal EnableDelayedExpansion
    rem // Write output line into target file:
    >> "!FILE!" (
        rem // Check whether dirctive and definition matched:
        if defined FLAG (
            rem // Match found, so write new line with incremented number:
            echo(!DIRECTIVE! !DEFINITION! %QUOTED%!NUMBER!%QUOTED%
        ) else (
            rem // No match found, so write original line:
            echo(!LINE:*:=!
        )
    )
)
endlocal

endlocal
exit /B