Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top