How to add time stamp in the beginning of batch file result

我们两清 提交于 2019-12-08 03:43:39

问题


How can I add time stamp in the beginning of batch file results?

Here's my current code:

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

I want to the result to be similar to this:

[hh:mm:ss] Reply from 120.89.12.38: bytes=1 time=1ms TTL=59

回答1:


Small edit to add formatting:

It's tested and works well here - though the time format on this PC is 24 hour time, which makes it work well.

@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



回答2:


You can try like this :

@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%


来源:https://stackoverflow.com/questions/35739609/how-to-add-time-stamp-in-the-beginning-of-batch-file-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!