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.
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.
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.
Related
I have a menu in a batch file that is powered by the choice command:
choice /C PNQTF /N /M "Choice: "
echo Errorlevel is %errorlevel%
if %errorlevel%==5 goto ViewFTP
if %errorlevel%==4 goto AutoTransferToggle
if %errorlevel%==3 goto TheEnd
if %errorlevel%==2 goto InternalLink
if %errorlevel%==1 goto NewSub
goto begin
19 times out of 20, there are no problems. The menu works fine. But a small percentage of the time, when I push Q to quit, the code seems to randomly skip all the way to :NewSub even though the Errorlevel is %errorlevel% statement echoes to the screen that Errorlevel is 3... and yet it still follows the instructions as if errorlevel were 1!
I thought of maybe using !errorlevel! just to be safe, but it doesn't matter because this set of choices is not in any block of code - it's not enclosed in any if statements or in any functions. I will say this issue never happens if I just run the batch file and immediately quit... there is something in the dark depths of the batch file that is somehow managing to linger and haunt the menu when the execution returns back to the beginning via goto begin.
I am missing half of the hair on my head because this issue has caused me to pull so much of it out. Chest hair might be next. The only thing that would seem more random than this
It turns out the solution is something I had already checked for before, but not careful enough. I traced my code and found a scenario where the user is returned to the main menu (goto begin) while in the middle of a called function - so the function was never concluded with a goto :eof statement.
Symptoms only occur when the user tries to quit from the main menu. When the user selects Q (which brings them to the very bottom of the file to a label called :TheEnd), it skips right back up to the line after the goto begin statement that interrupted the unconcluded function. The user doesn't see any output other than "after I hit Q, suddenly it's executing this other code."
So, as it turns out, errorlevel was working just fine; the returning of execution back to that line of code only made it appear that errorlevel was messing up, when in fact the batch file execution was treating the non-completed function like a bucket list item before finally dying.
I'm trying to get a .bat file to run a reminder popup do to restrictions on my work PC (Windows 7 OS). I am unable to use windows task scheduler due to these restrictions so I need the time check to run inside the batch file itself.
Currently, I have the following:
#echo off
:check
if "%time%"=="09:51:00.00"
msg user42 Test Reminder
Timeout /t 20 /nobreak
GOTO :Check
The issue seems to be with the "if" in the third line. as the rest of the code works with this line removed.
Any advice would be much appreciated.
We would need to manipulate your time variable to make this work as expected, but we don't really want to modify system or user environment variables. So we create our own.
First we make sure the time hour value always has 2 digits by replacing whitespace with 0 as only single digit time (1,2,3..9) will have a leading space. then we remove the : to make it a single matchable numeric value, then we simply do a match to see if the time is greater than or equal to 0951 (09:51 am) and if it is, we check if it is less than 0952 (09:52 am) meaning time has to be 09:51 any second and not 09:52 or larger.
Now we run the script every 50 seconds which will fall inside of each minute regardless, if we left it at 20 seconds, it would alert 2 or 3 times.
#echo off
:check
set mytime=%time: =0%
set mytime=%mytime::=%
if "%mytime:~0,4%" GEQ "0951" if "%mytime:~0,4%" LSS "0952" msg user42 Test Reminder
Timeout /t 50 /nobreak>nul
GOTO :Check
I am looking to exit my loop at a defined number of loops (10 - Loops). I looked at a few things on Google, but the loop was to do something else, so I was a little lost.
Here is my basic loop script and I hope one of you can educate me and point me in the right direction. If your wondering what I am doing it this way, here's why and if you have a better option, please let me know. I am fairly new to this batch scripting and I am open to suggestions.
This Uninstaller.exe does not pause the batch script. So I am basically looking for the Uninstaller.exe, which will be deleted upon completion of the Uninstaller.exe process. So once the loop detects the deletion of the executable, it will then exit the loop and move on to the next action in the script.
I have had a couple times where the uninstaller.exe crashed before it ended and deleted its self and prevented the batch file from continuing on. So I figured it would be best to only have it loop for a set number of times before exiting.
:: Uninstall App
"C:\Program Files\App Name\uninstall.exe" -quiet
::Validates the uninstall
SET LookForUninstaller="C:\Program Files\App Name\uninstall.exe"
:CheckForUninstaller
IF NOT EXIST %LookForUninstaller% GOTO ExitLoop
TIMEOUT /T 5 >nul
GOTO CheckForUninstaller
:ExitLoop
You may benefit from using the "start" command to run the uninstaller on a different process.
And you may also take advantage of its optional "/WAIT" which will allow the batch to wait until the process is finished to continue.
You may not need the loop after all.
i.e.
start /WAIT "C:\Program Files\App Name\uninstall.exe"
Here is what I did and seems to work ok.
::Set Uninstaller Variable
SET AppToUninstall="C:\Program Files\App Name\uninstall.exe"
:: Uninstall App
"%AppToUninstall%" -quiet
::Loops for 12 times in 10 second intervals (Total 120 seconds) to confirm deletion. Loop will exit after 12 loops and move on if uninstaller is not deleted.
for /l %%i in (1,1,12) do (
TIMEOUT /T 10 >nul
IF NOT EXIST %AppToUninstall% GOTO ExitLoop
)
:ExitLoop
Sorry for my bad english expression ... i'm not native.
I have searched for an answer for a while ... maybe i'm not doing this right but it seems that my (simple) code is not working for a reason.
As soon as I open the batch programm it shuts without me having the time to read the error message :(
could some one please correct this one ??
I am grateful for any kind of help !
#echo off
color 0a
mode 1000
title THE OFFSWITCH
:start
cls
if %time%== "12:00:00.00" goto :time
echo.
echo IT IS NOT TIME YET : %time%
goto start
:time
echo hello
pause>nul
It is pure luck, if your code works. In the most cases, it will just miss the time, because the loop is not fast enough to get the correct centisecond.
Instead of
if %time%== "12:00:00.00" goto :time
you should use
if "%time%" geq "12:00:00.00" goto :time
(if time is "greater or equal" than...)
Also you should consider to put a delay into the loop, because it eats all of the CPU-Time, it can get, and thus slows down your system. For example timeout /t 1>nul waits for one second.
How do I create a batch file that runs all the time, but only performs what I want done at midnight? I know how to do the loop part.. or so I think, I've never done batch code programming before so I don't really know if its right.
:loop
Stuff
goto loop
But I need the stuff to run a python script everyday at midnight. Thanks for any help in advance!
This depends on the system time showing 24 hour time.
The time format could be variable - you'd have to check what echo %time% shows when it's midnight.
#echo off
:loop
if "%time:~0,5%"==" 0:00" (
echo launch python script.
ping -n 60 localhost >nul
)
ping -n 20 localhost >nul
goto :loop
You should use at command to schedule your batch job and leave alone dead cycle.
You should try out Schtasks.exe, there is an example I wrote a while back on the Batch file wiki page, see :shutdown-clock
or you could use the GUI version, which is Task Scheduler