即使使用EnableDelayedExpansion,%time%也不会在for循环中扩展

时间:2013-07-03 22:48:51

标签: loops for-loop time batch-file

所以我注意到%time%变量在批处理文件的日志中不准确,如下所示。输出包含在代码下方...它会在进入for循环时获取当前时间,但不会随着时间的推移更新它。这些是大型zip文件,有时需要几分钟才能解压缩。任何人都知道这里发生了什么?

代码:

@echo off
setlocal EnableDelayedExpansion

for /f "tokens=1-26 delims=/:. " %%a in ('echo %date% %time%') do set today=%%d%%b%%c
echo %date% %time% process start >> h:\logfiles\unzip\%today%.log

:BANK1
set bank=BANK1
call:process BANK1LOCKBOX.lbx.6*.zip
call:process BANK1LOCKBOX.lbx.3*.zip

:BANK2
set bank=BANK2
call:process BANK2LOCKBOX.A*.zip PASSWORD1
call:process BANK2LOCKBOX.R*.zip PASSWORD2

echo %date% %time% process complete >> h:\logfiles\unzip\%today%.log
goto:eof

:process
set thisfile=%1
set pword=-y
if not [%2] equ [] set pword=-p%2 -y
if exist f:\banks\%bank%\zipped\%thisfile% (
    echo    %time% %thisfile% begin >> h:\logfiles\unzip\%today%.log
    for /f "tokens=1-2 delims=;" %%a in ('dir /b f:\banks\%bank%\zipped\%thisfile%') do (
        echo        %time% %%a start >> h:\logfiles\unzip\%today%.log
        7z x f:\banks\%bank%\zipped\%%a -of:\banks\%bank%\* %pword%
        if %errorlevel%==0 (
            move /y f:\banks\%bank%\zipped\%%a f:\banks\%bank%\processed\
            echo        %time% %%a complete >> h:\logfiles\unzip\%today%.log
        )
    )
    if exist f:\banks\%bank%\zipped\%thisfile% (
        move /y f:\banks\%bank%\zipped\%thisfile% f:\banks\%bank%\unprocessed\
        echo        %time% %thisfile% failed >> h:\logfiles\unzip\%today%.log
    )
    echo    %time% %thisfile% end >> h:\logfiles\unzip\%today%.log
)

日志看起来像这样:

Tue 07/02/2013 10:40:55.70 process start 
    10:40:55.72 BANK1LOCKBOX.lbx.6*.zip begin 
        10:40:55.72 BANK1LOCKBOX.lbx.60.zip start 
        10:40:55.72 BANK1LOCKBOX.lbx.60.zip complete 
        10:40:55.72 BANK1LOCKBOX.lbx.64.zip start 
        10:40:55.72 BANK1LOCKBOX.lbx.64.zip complete 
    10:40:55.72 BANK1LOCKBOX.lbx.6*.zip end 
    10:41:14.02 BANK1LOCKBOX.lbx.3*.zip begin 
        10:41:14.02 BANK1LOCKBOX.lbx.391.zip start 
        10:41:14.02 BANK1LOCKBOX.lbx.391.zip complete 
        10:41:14.02 BANK1LOCKBOX.lbx.343.zip start 
        10:41:14.02 BANK1LOCKBOX.lbx.343.zip complete 
    10:41:14.02 BANK1LOCKBOX.lbx.3*.zip end 
    10:42:48.28 BANK2LOCKBOX.A*.zip begin 
        10:42:48.28 BANK2LOCKBOX.A_0_1.zip start 
        10:42:48.28 BANK2LOCKBOX.A_0_1.zip complete 
        10:42:48.28 BANK2LOCKBOX.A_6_1.zip start 
        10:42:48.28 BANK2LOCKBOX.A_6_1.zip complete 
        10:42:48.28 BANK2LOCKBOX.A_2_1.zip start 
        10:42:48.28 BANK2LOCKBOX.A_2_1.zip complete 
    10:42:48.28 BANK2LOCKBOX.A*.zip end 
    10:44:42.33 BANK2LOCKBOX.R*.zip begin 
        10:44:42.33 BANK2LOCKBOX.R_1359_1.zip start 
        10:44:42.33 BANK2LOCKBOX.R_1359_1.zip complete 
        10:44:42.33 BANK2LOCKBOX.R_1365_1.zip start 
        10:44:42.33 BANK2LOCKBOX.R_1365_1.zip complete 
        10:44:42.33 BANK2LOCKBOX.R_1371_1.zip start 
        10:44:42.33 BANK2LOCKBOX.R_1371_1.zip complete 
    10:44:42.33 BANK2LOCKBOX.R*.zip end 
Tue 07/02/2013 10:45:25.91 process complete 

1 个答案:

答案 0 :(得分:1)

与分析时的任何其他%var%一样,

%time%。然后将该值替换为执行,因此它不会发生变化。

如果您想要当前时间,则需要setlocl enabledelayedexpansion并使用!time!代替%time%