为了清楚起见,我已经查看了有关此主题的其他问题,但它们要么没有解决这个问题,要么通常不解决这个问题。
我怀疑这个问题与嵌套的 FOR 循环以及 SETLOCAL 的放置位置有关。
我有一个功能来检查文件是否自特定时间以来已被修改:
:CheckCopied result directory filename since -- returns whether the file has been modified in the time since 'since'
:: -- result [out] - 0=FAIL, 1=SUCCESS
:: -- directory [in] - target directory
:: -- filename [in] - target filename
:: -- since [in] - the time the script started
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set directory=%~2
set filename=%~3
set since=%~4
ECHO.CheckCopied directory="%directory%", filename=%filename%, since=%since%
FOR /F "tokens=1,2 delims= " %%d IN ('echo ^| dir "%directory%" ^| find "%filename%"') DO (
FOR /F "tokens=1,2 delims=:" %%h IN ('echo %%e') DO (
ECHO Hours: %since:~0,2% vs %%h
ECHO Minutes: %since:~3,2% vs %%i
SET hours=!since:~0,2!
SET minutes=%since:~3,2%
ECHO hours=%hours% minutes=%minutes%
SET /A minutesSince=60*hours+minutes
ECHO %minutesSince%
SET hours=%%h
SET minutes=%%i
ECHO hours=%hours% minutes=%minutes%
)
)
EXIT /b
这输出:
CheckCopied directory="<my directory>" filename=<my filename> since=15:18:34.98
Hours: 15 vs 15
Minutes: 18 vs 11
hours= minutes=
ECHO is off.
hours= minutes=
所以“SET hours=!since:~0,2!” 似乎不起作用。
我试过了:
SET hours=!since:~0,2!
SET hours=%since:~0,2%
SET /A hours=!since:~0,2!
Every combination I could think of of SETLOCAL ENABLEDELAYEDEXPANSION and ENDLOCAL
我假设这与 SETLOCAL 有关,但我找不到神奇的组合......
编辑:
非常感谢下面的 jeb。我已经按照他的建议重做了这个功能:
:CheckCopied result directory filename since -- returns whether the file has been modified in the last 5 minutes
:: -- result [out] - 0=FAIL, 1=SUCCESS
:: -- directory [in] - target directory
:: -- filename [in] - target filename
:: -- since [in] - the time the script started
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set directory=%~2
set filename=%~3
set since=%~4
ECHO.CheckCopied directory="%directory%", filename=%filename%, since=%since%
FOR /F "tokens=1,2 delims= " %%d IN ('echo ^| dir "%directory%" ^| find "%filename%"') DO (
FOR /F "tokens=1,2 delims=:" %%h IN ('echo %%e') DO (
ECHO Hours: %since:~0,2% vs %%h
ECHO Minutes: %since:~3,2% vs %%i
SET hours=!since:~0,2!
SET minutes=!since:~3,2!
ECHO hours=!hours! minutes=!minutes!
SET /A minutesSince=60*hours+minutes
ECHO !minutesSince!
SET /A hoursx=%%h
SET /A minutesx=%%i
ECHO hours=!hoursx! minutes=!minutesx!
SET /A minutesSincex=60*hoursx+minutesx
ECHO !minutesSincex!
)
)
ENDLOCAL
EXIT /b
总之,预期与必需:
SET hours=%since:~0,2% ==> SET hours=!since:~0,2!
ECHO hours=%hours% ==> ECHO hours=!hours!
SET hoursx=%%h ==> SET /A hoursx=%%h