CMD / BATCH - 必须运行批处理脚本3次才能获得完整结果

时间:2015-06-09 17:25:04

标签: windows batch-file cmd

@echo off
REM returns current date and time as var datestamp in YYYYMMDD_hhmm format
REM         current date as variable date in YYYYMMDD format
REM         current time as variable time in hhmm format
for /f "tokens=2,3,4 delims=/ " %%a in ('DATE /T') do set date=%%c%%a%%b
for /f "tokens=1,2 delims=: " %%a in ('TIME /T') do set hr=%%a
for /f "tokens=1,2 delims=: " %%a in ('TIME /T') do set mi=%%b
for /f "tokens=1,2,3 delims=: " %%a in ('TIME /T') do set ampm=%%c
set time=%hr%%mi%

IF %ampm% EQU PM ( 
    REM in case of 12 PM we don't need to perform any actions
    IF %hr% EQU 12 GOTO end
    REM in case of another hour 1-11 PM we need to reformat it to 24-hour format by adding 12 hours
    set /A hour=1%hr%+12-100
    set time=%hour%%mi%
) ELSE (
    REM only in case of 12 AM we need to reformat it to 24-hour format by extracting 12 hours (00 hours)
    IF %hr% EQU 12 (
        set /A temp=1%hr%-12-100
        set hour=0%temp%
        set time=%hour%%mi%
    )
)
GOTO end
:end
set datestamp=%date%_%time%

这只是以24小时格式生成日期时间的脚本(也许这是更好的方法)。但它不起作用。在显示完整结果之前,我必须运行它3次:

D:\utils\scripts>getDateStamp.cmd & echo %datestamp%
%datestamp%

D:\utils\scripts>getDateStamp.cmd & echo %datestamp%
20150609_55

D:\utils\scripts>getDateStamp.cmd & echo %datestamp%
20150609_1855

这可能是什么问题? 谢谢!

0 个答案:

没有答案