在windows批处理脚本中时不更改时间

在windows批处理脚本中时不更改时间,windows,batch-file,Windows,Batch File,我有一个从s3存储桶下载文件的脚本,我想记录它所花费的时间。所以我有一个for循环,它遍历每个s3文件,然后如果大小大于设置值,我下载该文件。在if语句中,我回显一行,其中包括文件名和时间。问题是时间值不会改变。我创建了一个非常简单的示例来说明这个问题 @echo off setlocal enabledelayedexpansion set CUR_YYYY=%date:~10,4% set CUR_MM=%date:~4,2% set CUR_DD=%date:~7,2% set SU

我有一个从s3存储桶下载文件的脚本,我想记录它所花费的时间。所以我有一个for循环,它遍历每个s3文件,然后如果大小大于设置值,我下载该文件。在if语句中,我回显一行,其中包括文件名和时间。问题是时间值不会改变。我创建了一个非常简单的示例来说明这个问题

@echo off

setlocal enabledelayedexpansion

set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%

set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%

cd C:\Users\Public\code
echo %time% %date%> Outside_IF_%SUBFILENAME%.log
timeout /t 10
echo %time% %date%>> Outside_IF_%SUBFILENAME%.log
timeout /t 10
echo %time% %date% - Welcome Here>> Outside_IF_%SUBFILENAME%.log
timeout /t 10
echo %time% %date% - Welcome Here Again>> Outside_IF_%SUBFILENAME%.log
if not exist "1.txt" (
   echo %time% %date%> Inside_IF_%SUBFILENAME%.log
   timeout /t 10
   echo %time% %date%>> Inside_IF_%SUBFILENAME%.log
   timeout /t 10
   echo %time% %date% - Welcome Here>> Inside_IF_%SUBFILENAME%.log
   timeout /t 10
   echo %time% %date% - Welcome Here Again>> Inside_IF_%SUBFILENAME%.log
)
那么我怎样才能让外面的人在里面工作呢


任何帮助都将不胜感激

问题在于,当括号中的代码块最初被解析时,所有变量都会展开,而不是当其中的代码行实际运行时。(这就是为什么您应该看到每个“%TIME%”值完全相同的原因)。由于您已经启用了延迟扩展,因此需要使用它,以便在运行行时计算这些值

例如:

@Echo关闭
SetLocal EnableDelayedExpansion
对于/F“令牌=1-3 Delims=/”%%G In(
“%SystemRoot%\System32\robocy.exe\:。/NJH/L^
^|%SystemRoot%\System32\find.exe“123”
)设置“子文件名=%%G%%H%%I”
CD/D“%PUBLIC%\code”2>编号转到:EOF
1> “外部如果子文件名%.log”Echo%TIME%%DATE%
“%SystemRoot%\System32\timeout.exe”/T 10
1> >“外部\u如果\u%子文件名%.log”Echo%TIME%%DATE%
“%SystemRoot%\System32\timeout.exe”/T 10
1> >“外部\u如果\u%子文件名%.log”Echo%TIME%%DATE%-欢迎光临
“%SystemRoot%\System32\timeout.exe”/T 10
1> >“外部\u如果\u%子文件名%.log”Echo%TIME%%DATE%-欢迎再次光临
如果不存在“1.txt”(
1> “如果子文件名为%.log,则返回时间!!日期!”!
“%SystemRoot%\System32\timeout.exe”/T 10
1> >“如果子文件名为%.log”Echo!TIME!!DATE!
“%SystemRoot%\System32\timeout.exe”/T 10
1> >“Inside\u IF\u%SUBFILENAME%.log”Echo!TIME!!DATE!-欢迎光临
“%SystemRoot%\System32\timeout.exe”/T 10
1> >“Inside\u IF\u%SUBFILENAME%.log”Echo!TIME!!DATE!-欢迎再次光临
)