如何在批处理文件结果的开头添加时间戳?
这是我目前的代码:
set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set tm=%time:~0,2%%time:~3,2%%time:~6,2%
ping www.google.com -t -l 1 >PingLog_%dt%-%tm%.txt
我希望结果与此类似:
[hh:mm:ss] Reply from 120.89.12.38: bytes=1 time=1ms TTL=59
答案 0 :(得分:2)
小编辑以添加格式:
它在这里经过测试并运行良好 - 虽然这台PC上的时间格式是24小时,这使它运行良好。
@echo off
setlocal enableDelayedExpansion
set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set tm=%time:~0,2%%time:~3,2%%time:~6,2%
for /l %%# in (1,1,100) do (
set t=!time:~0,2!:!time:~3,2!:!time:~6,2!
for /f "tokens=* delims=" %%# in ('ping www.google.com -t -l 1 -n 1^|find /i "reply"') do (
echo [!t!] %%#
)
)
pause & goto :EOF
答案 1 :(得分:1)
您可以尝试这样:
@echo off
setlocal enableDelayedExpansion
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause
Set TmpLog=TmpLog.txt
Set Log=PingLog_%MM%-%DD%-%YYYY%-%HH%-%Min%-%Sec%.txt
If exist %TmpLog% Del %TmpLog%
If exist %Log% Del %Log%
echo %Log%
(
for /l %%# in (1,1,100) do (
set t=!time:~0,2!:!time:~3,2!:!time:~6,2!
for /f "tokens=* delims=" %%# in ('ping www.google.com -t -l 1 -n 1^|find /i "TTL"') do (
echo [!t!] %%#
)
)
)>>%TmpLog%
cmd /u /c type %TmpLog% > %Log%
pause & Start "" %Log%