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
Related
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
Title description as stated. I'm very novice at this, so please go easy. I've searched articles here, and have tried several iterations of how to do this but my attempts have failed.
I need to run Netstat -anbo every 30 minutes to a text file and keep them rolling for at least 5 files.
Thanks.
Here's how you can run something every few secs in batch script, without the 'sleep' utility.. I will leave the implementation up to you :)
# :loop
REM "do something every 10 secs"
# ping localhost -n 11 > nul
# goto loop
The ping acts as a sleep here.. you can even ping to any non-existent host.
If you want a more accurate time interval, you are better off using
# :loop
REM Execute the MS-DOS dir command ever 20 seconds.
# SLEEP 20
# GOTO loop
For this to work, you will need to have a sleep MS-DOS utility on the computer. You can find it here
You would have to use a simple loop and timeout
A sample implementation
#echo off
:loop
netstat -anbo >> file.txt # >> Will append to file instead of erasing file content
timeout /t 1800 /nobreak # /t timeout in seconds /nobreak Ignore key presses and wait for specified time
goto :loop #Simple loop to keep batch running indefinitely
Remember to run this batch as administrator (For netstat)
I am creating a batch file program where if you open a mspaint.exe, if its idle for 10 seconds the program will close automatically. I have created this:
#echo off
:Start
tasklist /FI "IMAGENAME eq mspaint.exe" | findstr "mspaint.exe" >nul
IF "%ERRORLEVEL%" == "0" GOTO Running
IF "%ERRORLEVEL%" == "1" GOTO NotRunning
:Running
***::Check if idle***
GOTO Terminate
:NotRunning
GOTO EOF
:Terminate
timeout 5
taskkill /im mspaint.exe /f
ECHO Paint has been terminated due to inactivity
PAUSE
:EOF
EXIT
I have trouble finding the idle syntax(seems it doesn't exist), is there another way to make the idle time work? And since this is my first time creating a batch file, I really need a helping hand here.
Not the answer you want to hear, but the one that you need. The only way to monitor another process for activity is to install a window hook, but that's going to require a much lower level language (C or C++).
If that doesn't put you off then start with the Microsoft Hooks Overview and post further questions. Good luck (you'll probably need it).
Thanks for the help but that seems complicated. I used SCHTASKS instead and it worked :)
I'm using a network management tool to apply updates to software and I have an issue where if a users is already using the program you want to update the update will fail as you would expect.
I have been trying to put together a batch script that will detect whether the the program is running and if it is the script will wait until the user closes down the program and the apply the msi update.
I've pretty much scoured google but can only really find previous scripts that kill the program first before proceeding and I don't want that to happen as the user may lose work.
Hope someone can help!
#echo off
start "" notepad.exe
:loop
(tasklist | find /i "notepad.exe" && ( ping -n 2 localhost & goto loop)) >nul
echo Notepad closed
This just starts notepad.exe (the running program) and waits until it is closed. Adapt according to your needs
`tasklist |find "programname"`
will tell you if the programm is running:
if %errorlevel%==0 echo running
put just a little loop around it:
:loop
tasklist |find "programname" >nul
if %errorlevel%==0 (
echo prgram running
timeout 2>nul
goto loop
)
echo program not running
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.