批量设置var并不总是在循环中工作

时间:2017-05-23 20:20:49

标签: loops batch-file set

有时我的电话:GetTime程序有时不工作。 有时我的档案只是命名.rar而不是例如23.05.2017_22-02-07.rar

为什么会这样?大部分时间它工作正常....

    for /f "usebackq delims=" %%a in (`"%MD5% default_main_GlobalData.CSF"`) do set StartMD5=%%a
    for %%a in (%StartMD5%) do set StartMD5=%StartMD5:~0,32%
    :Proc1
    if exist del "%Workdir%\SniperGhostWarrior3_Savegames\Hidden\.rar"
    if exist del "%Workdir%\SniperGhostWarrior3_Savegames\Profile\.rar"
    for /f "usebackq delims=" %%a in (`"%MD5% default_main_GlobalData.CSF"`) do set CurrentMD5=%%a
    for %%a in (%CurrentMD5%) do set CurrentMD5=%CurrentMD5:~0,32%
    if not "%CurrentMD5%" == "%StartMD5%" (
    call :GetTime
    %Winrar% a -inul "%Workdir%\SniperGhostWarrior3_Savegames\Hidden\Temp.rar" "C:\Users\Administrator\Saved Games\Sniper Ghost Warrior 3\"
    %Winrar% a -inul "%Workdir%\SniperGhostWarrior3_Savegames\Profile\Temp.rar" "D:\Spiele\Sniper Ghost Warrior 3\win_x64\Profile"
    ren "%Workdir%\SniperGhostWarrior3_Savegames\Hidden\Temp.rar" "%CurrentTime%.rar" >nul 2>&1
    ren "%Workdir%\SniperGhostWarrior3_Savegames\Profile\Temp.rar" "%CurrentTime%.rar" >nul 2>&1
    set "StartMD5=%CurrentMD5%"
    echo Game Saved. Backup created. %CurrentTime%
    )
    goto :Proc1
    :GetTime
    set "CurrentTime=%date%_%time:~0,-3%"
    set word=-
    call set CurrentTime=%%CurrentTime::=%word%%%
    goto :eof

2 个答案:

答案 0 :(得分:0)

请参阅有关delayedexpansion的无尽文章。您在子例程中设置currenttime,但代码块中使用的currenttime是遇到currenttimefor的值,而不是由call :gettime更改的值if not "%CurrentMD5%" ==...子程序。

最简单的方法:将{{1}}移至{{1}}

之前

答案 1 :(得分:0)

在循环中,变量在循环结束时取其值。 如果您想在循环中使用它们,请将此行放在批处理的顶部:

SETLOCAL ENABLEDELAYEDEXPANSION

并在循环内部调用变量:

!var!
祝你好运!