0

I am using the following batch script:

@echo off

set host=upc.hu

set logfile=%host%.log

echo. >> %logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)

:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping)

I need to add the date in YMD format and the time in HM format e.g:`

2017.06.02 12:39 Log_%host%.log (Would prefer 20170602 1239 format)

Tried to set the date and add it to the logfile name but I have some kind of problem with the syntax because only the the year added or nothing at all. Could you help me with the script?

EDIT:

Tried to add only %date% to the logfile like this:

set logfile=%date%.log

If I simply echo the date it works just fine but the file name is 2017 without the month, day and .log part.

2b1c
  • 75
  • 8
  • 1
    Previous question [Format date and time in a Windows batch script](https://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script?noredirect=1&lq=1) – Sam Denty Jun 03 '17 at 11:05
  • @SamDenty Thanks, I tried to add to the logfile after setting the date but still got the same issue. Nothing shown when the file is created, only the %host%.log name. – 2b1c Jun 03 '17 at 11:16
  • 2
    Possible duplicate of [How to get current datetime on Windows command line, in a suitable format for using in a filename?](https://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us) – aschipfl Jun 03 '17 at 11:31
  • 1
    You cannot include `:` (colon) within a Windows file name. – dbenham Jun 04 '17 at 04:45

0 Answers0