Windows task scheduler - Non-repeating only run during window of time - windows

I've set up a non-repeating task which is triggered by workstation unlock. How can I condition it so that only runs within a specific period of time during the day? For example just between 8-10 A.M?
There's a similar question answered before but that solution can only be used for a repeating task.

I was hoping for an answer, came back and there was nothing but since I needed one badly enough I found it myself. This process always amazes me.
You need to do your task via a batch file using IF command. Here's the code:
#ECHO OFF
:: For using just the hour digit of 'time' variable
SET hour=%time:~0,2%
:: Let's say I want whatever it is to run only when I'm unlocking the station between 8-10 A.M.
IF %hour% GEQ 8 IF %hour% LEQ 10 (GOTO RUNIT)
GOTO END
:RUNIT
:: Put your commands here, I want to open a bunch of text files I'm working on.
start notepad "D:\file1.txt"
start notepad "D:\file2.txt"
:END
Works like charm. Now those file won't open every time I'm back to my laptop and log in.

Related

Trying to wrap CALL function fails [duplicate]

I am using the tool 'HTML Match' to compare two HTML files. As I have to compare many files, I create a batch file like the followion. For example, I give only five sets of files.
cd "C:\Program Files\HTML Match"
HTMLMATCH.EXE "D:\Raj\compare1\a1.html" "D:\Raj\compare2\a1.html" "D:\Raj\compare_res\a1.html"
HTMLMATCH.EXE "D:\Raj\compare1\a2.html" "D:\Raj\compare2\a2.html" "D:\Raj\compare_res\a2.html"
HTMLMATCH.EXE "D:\Raj\compare1\a3.html" "D:\Raj\compare2\a3.html" "D:\Raj\compare_res\a3.html"
HTMLMATCH.EXE "D:\Raj\compare1\a4.html" "D:\Raj\compare2\a4.html" "D:\Raj\compare_res\a4.html"
HTMLMATCH.EXE "D:\Raj\compare1\a5.html" "D:\Raj\compare2\a5.html" "D:\Raj\compare_res\a5.html"
When I execute this batch file in a cmd prompt, only the first line, that is, only 'a1.html', gets compared and produces a result. Then execution stops.
Add call in front of the commands you're running.
You can also change this to a for loop, so:
FOR /L %%i in (1,1,5) DO CALL HTMLMATCH.EXE D:\Raj\compare%%i%%\a%%i%%.html D:\Raj\compare%%i%%\a%%i%%.html D:\Raj\compare_res\a%%i%%.html
The answer to your problem is to write CALL HTMLMATCH.EXE (and the rest of the parameters).
Just use CALL in front of every executable command in the batch file.
I was looking for something really similar and tried, I think, all the replies left here but I finally found the solution to my problem!!
In my script I want to check if one process is running, if not, start it (a .exe) and then check if another process is running, if not, start it too (but leave all the programs opened) and the problem is that the first .exe was started but then not moving to the second one because it was waiting until the process ended.
It´s finally working for me with start and the magic comes with...
/separate
it works for me as:
start "program1" /separate program1.exe
other commands
Before it stopped after starting program1 because it was waiting until it was closed, I think, but this was not going to happen because I wanted to leave it opened.
Now with the start /separate it continues with the other commands.
I found it in another forum but the thing is that it´s the manual, /separate is used to start in another memory space.
You don't have to insert quotation marks where there isn't any space mark between.
Try that:
HTMLMATCH.EXE D:\Raj\compare1\a1.html D:\Raj\compare2\a1.html D:\Raj\compare_res\a1.html
Maybe it will solve your issue.

Opening multiple calculators with a single batch file

So i essentially need to create a batch file, that opens multiple windows of an application, example a calculator here. My actual project is to make it open over a few thousand times, so that the system crashes. I've looked over for loops and learnt a little batch scripting syntax and etc. But i don't really know much, and concluded on a block of code to open a trail bat file to open 20 calculators, multiple environments without the first one exiting.
#echo off
set count=0
:loop
set /a count=%count%+1
start /MIN /DC:\Windows\System32 calc.exe
if %count% neq 20 goto loop
But i don't understand, the behavior is erratic. Sometimes, my system crashes. I've tried using the "for" syntax as well
#echo off
for /L %%a IN (1,1,20) DO start "" "C:\Windows\system32\calc.exe"
And this one crashes my system or opens only a single calculator, which might mean that the previous instances are closing and only the last one opens.
So how do i modify my code?.. or perhaps is there a better script in batch files to open multiple environments of an application?

Force autonomous .bat files to run sequentially

I have a reasonably large number of .bat files that are launched by the Windows Task Scheduler. And, subsequently, or by an app that's called in the process. In the latter case, the app launches a .bat file to log that it has started and another .bat file to log that it has completed. They all trigger another single logging .bat file that writes to a log file. There a multiple situations that cause them to overlap:
all of the Task Scheduler tasks are manually Run at once
one of the app tasks is still running when another related Task
Scheduler runs on schedule.
So, we sometimes see:
the process cannot access the file because it is being used by another
process.
And, the result of this is that log entries are missed.
Just to be clear:
Task Scheduler tasks:
go1 >>> launches bat_name1.bat
go2 >>> launches bat_name2.bat
etc.
bat_name1.bat, bat_name2.bat,....
CALL log.bat %bat_nameN%
app.exe %bat_nameN%
EXIT
app.exe task:nameN
launches STARTnameN.bat
(runs the core of the app)
launches ENDnameN.bat
STARTnameN.bat and ENDnameN.bat
log.bat %nameN%
log.bat
#ECHO OFF
SET fileloc=C:\Users\Public\BackupLogs
echo %time% %date% %2 %3 %~1>%fileloc%\temp.txt
type %fileloc%\temp.txt>>%fileloc%\backuplog.txt
So the objective would be to allow all these programs to run autonomously but to sequentialize the result so the log files can be completely written without interference.
One thought would be to separate the temp.txt into tempN.txt,... and to append the result to the single backuplog.txt as a part of the ending process. That would likely make it better but doesn't appear to be a 100% solution as there could still be overlaps?
You could test if the append fails and retry via something like:
:try_append
copy /b %fileloc%\backuplog.txt+%fileloc%\temp.txt %fileloc%\backuplog.txt
if errorlevel 1 goto try_append
(copy must be used as internal commands such as echo and type won't set the error level.)
That would improve things, however you'd still have the problem of collision on the %fileloc%\temp.txt file. Perhaps you have a way to easily resolve this using unique temp names in your various .bat files.
If not, better random temp filenames can be created using %time::=% (millisecond randomness), but even that could conceivably collide.
When I want a truly random filename I involve the value of the RDTSC opcode which changes every processor clock cycle making collisions impossible. There are open source tools available to help with this, eg: capture RDTSC opcode. But perhaps that is a topic for another question.

If batch file is closed, other program is closed as well

I have a batch file that runs an old game, fixing a lot of issues that the game had. Long story short, the batch file stays open while the game is running, and if the batch file gets closed before the game gets closed, then it screws up a bunch of stuff in the game. Is there any way to set it so if the batch file is closed, it ends another process (the game's process)?
It is fine if I have to keep another batch file running to start the original batch, starting the game. Here's a more visual representation of what I expect to achieve:
Batch1 - starts batch2, ends Process1 if batch2 gets closed
Batch2 - starts Process1
Process1 - (game)
I already have batch2 coded to my liking, I just need help with batch1. Any ideas of what it would look like?
You can try this approach [NOT TESTED] :
From batch1.bat, call batch2.bat and wait for it to terminate. Once it is terminated, use TASKKILL to kill the process associated with your game.
start /wait cmd /k CALL "path to batch2.bat"
TASKKILL /F /IM processName.exe

(MS-DOS) Time Delays

I came past a few ways to cause a time delay such as pings and dirs. Though none of them are really precise, is there anny proper way to cause a time delay?
I heard about a few things though they don't work on all computers, not on my Windows XP nor the Windows NT at college.
It takes ages going through all files on Google finding a good answer, and since I didn't yet find the question on Stack Overflow I thought it might be good to just create the question myself ;)
Sleep
It will allow you to do this.
<warning>This is a hack</warning>
Use your favorite programming language (other than MS-DOS batch) and create an application which takes one argument, the number of milliseconds to wait, then, simply call this program from a batch file to sleep the required amount.
As far as I know, this is the only reliable way to do it in DOS.
If you don't have the ability to send another program along with the batch file, use DEBUG to write a sleep command on the fly, and execute it.
EDIT:
The above answer was kind of toungue-in-cheek. I wouldn't run a batch file that had some DEBUG trickery in it. I believe the traditional way to use a delay in a batch file is the CHOICE commad.
type nul|choice /c:y /t:y,nn > nul
Which of course, doesn't work in XP, since that would be WAAYY too convenient.
"...proper way...."
you can't do that in DOS.
It is possible to achieve a precision of a few miliseconds, depending on your machine's speed.
I have just finished creating such a batch, and though I won't share with you the actual code, I'll give you some pointers:
Use %time% variable, and devide into substrings (without ":" and ".") - one substring will get the seconds, the other - minutes (you may add hours, for delays of over an hour, and even incorporate the date)
Use set /A to transform the time variables into 1 integer representing the amount of seconds which have passed since a rounded hour (X:00:00.00). Add the inputed delay (in seconds) to that.
Create a short loop, comparing the value of the combined var (explained in section 2) to the current time (need to recalc the curent combined min+sec value, inside this loop), and exiting the loop when the match is true.
One major bugfix here - you'll need to truncate any preceeding zeros to variables which are about to get evaluated in a "set /A" command. I've noticed that the console's evaluator (for this command) returns an error when an integer with a preceeding 08 or 09 (or simply 08 or 09) is used.
Remark:
This will only work with command extensions enabled. To test it, use the following at the beginning of the routine:
verify other 2>nul
setlocal enableextensions
if errorlevel 1 goto err
And then add an error handler subroutine named "err".
If you'd want to continue your batch in the same file, use "endlocal" after the looping subroutine.
BTW, this applies ONLY to Windows XP service pack 2 or 3.

Resources