[英]Date Parsing in Windows Batch File
我遇到以下批處理文件的問題。 首次運行時,它不會在文件中正確輸出日期。 在第一次運行時,我得到以下信息:
*Start of batch file ~4,2dt:~6,2dt:~2,2dt:~8,2dt:~10,2*
*End of batch file ~4,2dt:~6,2dt:~2,2dt:~8,2dt:~10,2*
在下一次運行中,它可以正常運行:
*Start of batch file 10/18/13 06:46*
*End of batch file 10/18/13 06:46*
要注意的一件事是,該日志文件在第一次運行時不存在,因此它可能與此有關?!?!?
這是我的批處理文件:
set logFile=C:\log.txt
echo %logFile%
REM Get the Start Date and Time and Parse it out
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set start=%dt:~4,2%/%dt:~6,2%/%dt:~2,2% %dt:~8,2%:%dt:~10,2%
ECHO Start of batch file %start% >>%logFile%
REM Run some procedure
REM Get the End Date and Time and Parse it out
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set end=%dt:~4,2%/%dt:~6,2%/%dt:~2,2% %dt:~8,2%:%dt:~10,2%
ECHO End of batch file %end% >>%logFile%
任何幫助/建議將不勝感激。
For語句的內容在執行前已展開,因此dt首先為空。
將“ setlocal ENABLEDELAYEDEXPANSION”添加到批處理文件的頂部。 還要用%替換%! 對於dt的所有擴展。 因此,設置的開始和結束字符串變為“!dt:〜4,2!/!dt:〜6,2!/!dt:〜2,2!!dt:〜8,2!:!dt:〜10, 2!”
通過運行“ set /?”查看“ set”幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.