0

I have a batch file that is running 24/7 to check if a certain Programm is running and start it if not. It checks every 10 seconds and than writes an Console Entry as such.

    @echo off

    for /L %%n in (1,0,10) do (
    tasklist /nh /fi "imagename eq AutoPrinter.exe" | find /i                 
    "AutoPrinter.exe" >nul && (
    echo %date% %time:~0,8% AutoPrinter already running
    echo %date% %time:~0,8% AutoPrinter already running>>         
    autostarterlog.txt
    ) || (
    start AutoPrinter.exe
    echo %date% %time:~0,8% AutoPrinter started
    echo %date% %time:~0,8% AutoPrinter started >> autostarterlog.txt
    ) 
    timeout /T 10 
    )

However timestamps are not updating so the output looks like this

   20.06.2019 10:45:41 AutoPrinter started

   20.06.2019 10:45:41 AutoPrinter already running

   20.06.2019 10:45:41 AutoPrinter already running

   etc...

Is there anyway to update the timestamps so they display the actual time and not always the starting time?

Freddy789
  • 506
  • 4
  • 15

2 Answers2

0
@echo off
cls
:start
tasklist /nh /fi "imagename eq AutoPrinter.exe" | find /i                 
"AutoPrinter.exe" >nul && (
   echo %date% %time:~0,8% AutoPrinter already running
   autostarterlog.txt (
      echo %date% %time:~0,8% AutoPrinter already running
   )
) || (
   start AutoPrinter.exe
   echo %date% %time:~0,8% AutoPrinter started
   autostarterlog.txt (
      echo %date% %time:~0,8% AutoPrinter started
   )
) 
timeout /T 10 
goto start

Try using goto rather than looping. Above example

  • 1
    Thanks for you answer but id rather go with the loop i actually found the solution myself but thanks for you help anyway – Freddy789 Jun 27 '19 at 09:22
0

Actually found the answer after looking at https://stackoverflow.com/a/13809834/11565682 after enabling delayed expansion and changing the % to ! heres the working Code for future people with the same Question

    @echo off

    setlocal ENableDelayedExpansion
    for /L %%n in (1,0,10) do (
    tasklist /nh /fi "imagename eq AutoPrinter.exe" | find /i "AutoPrinter.exe" >nul 
    && (
    echo !date! !time:~0,8! AutoPrinter laeuft bereits
    echo !date! !time:~0,8! AutoPrinter laeuft bereits >> autostarterlog.txt
    ) || (
    start AutoPrinter.exe
    echo !date! !time:~0,8! AutoPrinter gestartet
    echo !date! !time:~0,8! AutoPrinter gestartet >> autostarterlog.txt
    ) 
    timeout /T 10 
    )
Freddy789
  • 506
  • 4
  • 15