无法使用.bat文件捕获时差

时间:2015-02-24 08:22:17

标签: windows batch-file vbscript cmd

我有一个bat文件,我用它来生成一个LOG文件,在日志文件中,我试图捕获每个for循环的开始时间和结束时间。每当我想使用我的bat文件echo %TIME%时,我都无法获得任何不同的值。 bat文件捕获的第一个时间戳在整个程序中显示。 我创建的bat文件如下:

SETLOCAL ENABLEEXTENSIONS

SET me=%~n0

SET parentPath=%~dp0

cd C:\Users\bangshis\Desktop\TOOLS\New Folder

ECHO %me% : Creating file for logging time>myTimeLog.txt

ECHO  %me% : STARTING CONVERSION: %TIME%>>myTimeLog.txt

for %%a in ("C:\MyData\Work\InputFiles\TextFiles\*.txt") do (SETLOCAL

set STARTTIME=%TIME%

ECHO  %me% : TIME STARTING CONVERSION: %STARTTIME% >>myTimeLog.txt

ECHO  %me% : The File getting processed is: %%a >>myTimeLog.txt

textToDOC.bat %%a "C:\Work\Output_Dir\%%~na"

ENDLOCAL

SETLOCAL

set ENDTIME=%TIME%

ECHO  %me% : TIME ENDING CONVERSION: %ENDTIME% >>myTimeLog.txt

ENDLOCAL

)

ECHO  %me% : ENDING CONVERSION: %TIME%>>myTimeLog.txt

pause>nul

输出:

textToDOC_conv : Creating file for logging time

textToDOC_conv : STARTING CONVERSION: 13:28:28.99

textToDOC_conv : TIME STARTING CONVERSION: 13:28:28.99 

textToDOC_conv : The File getting processed is: C:\Work\InputFiles\Test\PLS.txt 

textToDOC_conv : TIME ENDING CONVERSION: 13:28:28.99 

textToDOC_conv : TIME STARTING CONVERSION: 13:28:28.99 

textToDOC_conv : The File getting processed is: C:\Work\InputFiles\Test\X2Q.txt 

请告诉我,为什么每次尝试发布时都会获得相同的时间戳?

1 个答案:

答案 0 :(得分:1)

试试这个:

SETLOCAL enabledelayedexpansion
SET me=%~n0
SET parentPath=%~dp0
cd C:\Users\bangshis\Desktop\TOOLS\New Folder
ECHO %me% : Creating file for logging time>myTimeLog.txt
ECHO  %me% : STARTING CONVERSION: %TIME%>>myTimeLog.txt

for %%a in ("C:\MyData\Work\InputFiles\TextFiles\*.txt") do (SETLOCAL
ECHO  %me% : TIME STARTING CONVERSION: !TIME! >>myTimeLog.txt
ECHO  %me% : The File getting processed is: %%a >>myTimeLog.txt
textToDOC.bat %%a "C:\Work\Output_Dir\%%~na"
ECHO  %me% : TIME ENDING CONVERSION: !Time! >>myTimeLog.txt
)

ECHO  %me% : ENDING CONVERSION: %TIME%>>myTimeLog.txt
pause>nul

当您想在代码块中使用变量时(即()之间的代码)您需要enabledelayedexpansion并使用感叹号而不是百分号。

由于%me%在整个代码中保持不变,因此您可以使用%而不会出现任何问题