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
Related
I am very new to batch files, having mostly coded in Matlab for years. I would simply like to loop some code I have written for a set number of minutes. In pseudocode:
While elapsedTime<timeLimit
Run Code
How do I declare the variables elapsedTime (which would be a time counter that runs throughout the While loop) and timeLimit (just a value in seconds)?
If you are using batch you could do this:
#echo off
set /p min=Set minutes:
set /a sec=%min% * 60
timeout %sec%
Calculating with time is difficult in batch (not impossible, just difficult).
Here is another approach: creating a dummy file, start an additional (independent) process which waits a certain time and then deletes the file.
The original process can loop until the file disappears:
#echo off
break> timer.tmp
start /min cmd /c "timeout 5 & del timer.tmp"
:loop
echo waiting...
timeout 1 >nul
if exist timer.tmp goto :loop
echo done waiting.
Just adapt the timeout to the desired time (5 seconds in my example)
I need to make a batch file that calls a URL (without opening a browser), but only after 20 seconds since previous run. If it's run sooner than 20 seconds, the script does nothing and closes. How would I go about doing this?
If your batchfile waits 20 seconds before closing, and you can only have one instance of it running at a time, then the problem is solved.
Use a technique like this or this to make your batch file exit ,without doing anything else, if it is already running.
Then, if the batchfile does not detect another instance running, Use the timeout command at the end of the batchfile to wait 20 seconds.
timeout /t 20
#montewhizdoh: if you add a timeout at the end of the batch file, it means you have to wait for 20 seconds before the batch actually returns, which may not be desirable, especially if the operations it performs are supposed to occur quickly.
#seventy70: by recording the current time in a separate file, you can ensure that the batch will exit without doing anything you don't want it to, unless a certain number of seconds has elapsed. The following code achieves this:
#echo off
Setlocal EnableDelayedExpansion
set lastTime=86500
if NOT EXIST lasttime.txt goto :nextStep
for /f "delims=;" %%i in (lasttime.txt) do (
set lastTime=%%i
)
set /A lastTime=(%lastTime:~0,2%*3600) + (%lastTime:~3,2%*60) + (%lastTime:~6,2%)
:nextStep
set currTime=%TIME%
set /A currTime=(%currTime:~0,2%*3600) + (%currTime:~3,2%*60) + (%currTime:~6,2%)
:: required check in case we run the batch file right before and right after midnight
if %currTime% LSS %lastTime% (
set /A spanTime=%lastTime%-%currTime%
) else (
set /A spanTime=%currTime%-%lastTime%
)
if %spanTime% LSS 20 (
echo Only %spantime% have passed since the last run
goto :eof
)
:: ************************************
:: DO ACTUAL STUFF HERE
:: ************************************
if exist lasttime.txt del lasttime.txt
echo %time%>>lasttime.txt
endlocal
So every time the batch is allowed to run, it records the current time in a file. The next time around, it reads the file, extracts the time from it and compares it with the current time. If less than 20 seconds have elapsed, the batch exits. If more than 20 seconds have passed, the actual operations can be executed and at the end, the current time is recorded again in the control file for use next time around.
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)
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.
I want to perform the following operations.
Read 1 word at a time from an input file consisting of many words.
Pass this word as an argument to another command line based application.
Run that application for a fixed amount of time, say 10 seconds.
Abort the execution of the application if it is still running after 10 seconds and go back, pick the next word from the input file and repeat steps 1 to 3.
Here is what I have written though it does not achieve exactly what I want it to:
#echo off
for /f %%i in ('type input.txt') do call:Routine %%i
:Routine
set app="myApp.exe"
set limit=60
%app% %1
goto Delay
:Delay
ping localhost -n %limit% > nul
The above script will introduce the delay after the execution of myApp has completed. However, I want it to run myApp.exe for not more than 10 seconds, if it does, then abort the application using taskkill and move on to the next word from the input file.
I searched for a solution online and came across this:
http://www.tech-archive.net/Archive/WinXP/microsoft.public.windowsxp.general/2006-04/msg03609.html
Though it does not answer my query exactly, I would like to make my code do something similar.
Thanks.
The logic in the linked code looks flawed: It either launches 3 download commands, or it delays ~59 seconds and attempts to kill all download commands, but it never does both. The TASKKILL command arguments are not correct - the imagename belongs after the /IM parameter.
In your code, you are not going to kill your task without the TASKKILL command!
You must GOTO :EOF or EXIT /B after your loop finishes, otherwise the code will fall through and execute the subroutine without using CALL. But there really is no need to use a subroutine at all.
You only need to initialize your variables once.
No need to execute a command in your IN() clause. FOR /F has a variation that can read the text file directly. Type HELP FOR from the command line and read the documentation carefully.
PING has roughly a 1 second delay between each echo request. So a count of 11 will yield a delay of roughly 10 seconds.
EDIT - originally forgot the critical START command to start the app in its own process
#echo off
set app="myApp.exe"
set limit=11
for /f %%i in (input.txt) do (
start "" %app% %%i
ping localhost -n %limit% > nul
taskkill /im %app% /f
)