Kenneth Lui
Kenneth Lui

Reputation: 1

How to deal with windows batch variable?

I have written the following batch but I'm afraid it doesn't work close to what I'm expecting:

@echo off

set filename=%1
for /f "tokens=* USEBACKQ" %%F in (`forfiles /m "%filename%" /c "cmd /c echo @ftime"`) do (
set R1=%%F
)

:process
ping -n 60 127.0.0.1 > nul

for /f "tokens=* USEBACKQ" %%F in (`forfiles /m "%filename%" /c "cmd /c echo @ftime"`) do (
set R2=%%F
)

setlocal enabledelayedexpansion

if !R1! == !R2! (
   echo no change
) else (
   echo kill process
   set R1=!R2!
   echo !R1!
   taskkill /im schedule2.exe
   schedule2.exe
)

endlocal

goto process

This batch is to check the modification time of a specified file (filename pass as parameter when calling this batch), the time was record first and after 60 seconds the modification time is checked again. if the file was modified, kill a particular process (which called schedule2.exe in my case) and then the time obtained at the very beginning was reset and change to the latest time observed. since using enabledelayedexpansion the value for R1 also gone as well, what can I do to achieve the result i want? many thanks

Upvotes: 0

Views: 45

Answers (1)

Magoo
Magoo

Reputation: 80033

Remove the selocal/endlocal bracket. There is nothing within that bracket that requires delayedexpansion.

You'd also need to change !var! to %var% throughout since delayedexpansion is then not invoked.

Your problem is that you are stiing r1 within the subroutine, but since that is currently within a setlocal/endlocal bracket, the change is backed out when the endlocal is encountered.

Upvotes: 1

Related Questions