3

Good morrow, all.

My first question here, but I've been keeping an eye on this site for a long time. In fact, it's helped me create about three dozen (albeit simpler) batch files so far! I'm finally having a difficult time searching for an answer. Forgive me if it's covered but nothing I've located is quite right for my application.

Here is the code I am working with. The process is as follows. If file.zip exists, goto an unzip command; else, wait five minutes and check again. This will loop continuously until it finds a file.

:checkexist
IF EXIST "\\server\folder\subfolder\file.zip" (
GOTO zipexist
) ELSE (
ECHO.
ECHO File not found. Lets wait, say, 5 minutes ...
ECHO.
TIMEOUT /t 300 /nobreak
GOTO checkexist
)

:zipexist
ECHO.
ECHO Unzipping will begin in 30 seconds.

And the code continues on.

It works beautifully, actually. The issue I am having is if a file never exists - if it never got uploaded, for example or there was no file for the day. I have tried using some options with the SLEEP command and using something I found on MS TechNet about SET DELAY=nn with no avail. I am trying to find a wrapper(?) for this simple if/else statement that will only allow it to run for nn minutes and if no file.zip is found, terminate the batch file. This will, ultimately, run on a server-side process so no user will be available to make a judgement call.

Are there any suggestions on how to accomplish this in a similarly simple way? I realise there are a lot of options but sometimes the syntax confuses me; I'm still learning.

Please forgive any dumb questions that follow this initial post! :) I'm getting there, I'm sorry I'm a little slow.

UserUnknown
  • 172
  • 2
  • 15
  • Does it have to be nn minutes? Can you set a counter that is incremented each iteration. If the counter is greater than your max allowable attempt, then exit? – Johnie Karr Mar 21 '14 at 17:17
  • Why didn't the `SLEEP` command work? Did you get error messages? Tell us what happened when ran compared to what you expected. – aphoria Mar 21 '14 at 17:19
  • @aporia I did not actually receive an error message of any type. Using `SLEEP` along with a `PING > nul` string as seen in a [SO Post](http://stackoverflow.com/questions/735285/how-to-wait-in-a-batch-script). It seemd to just hang even with a short timer. I had to forcibly close the command window. @Johnie Karr I was able to search for [this SO Post](http://stackoverflow.com/questions/10453804/counter-not-incrementing-with-if-statement-inside-for-loop-in-windows-batch-file) and will use that as a basis to try my hand at it. It's a bit advanced for my skill at the moment but worth a shot! :) – UserUnknown Mar 21 '14 at 17:38
  • You were trying to use `SLEEP` with `PING`? You can just do `SLEEP 300` to sleep for 300 seconds (5 minutes). – aphoria Mar 21 '14 at 18:29

2 Answers2

2

This implements the suggestion but illustrates a different way to check the file and to continue when the loop has been executed 24 times = 2 hours

set num=0
:checkexist
IF NOT EXIST "\\server\folder\subfolder\file.zip" if %num% LEQ 24 (
ECHO.
ECHO File not found. waiting 5 minutes ... times (%num%^)
ECHO.
TIMEOUT /t 300 /nobreak
set /a num+=1
GOTO checkexist
)

:zipexist
ECHO.
ECHO Unzipping will begin in 30 seconds.
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Thank you for the code example! After Johnie's suggestion and hunting SO, I came up with just this statement. Upon examination though, I left out the slash-a from `SET /a num+=1`. Silly error. That was the missing piece. Works perfectly now. Thank you, both. – UserUnknown Mar 24 '14 at 16:07
1

Try placing a counter outside your IF statement, and incrementing it inside your loop. Once the counter becomes your limit, then exit.

Sorry for not providing a code sample, batch scripting was a long time ago for me.

Johnie Karr
  • 2,744
  • 2
  • 35
  • 44
  • Thank you. I will try that. I found a couple of posts here on SO about it so I think I can work backwards from there. I will try that out and let all know how it works. Thank you for the direction, I never thought about that option! – UserUnknown Mar 21 '14 at 17:45