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

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

Related

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

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.

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.

Batch file runs multiple processes simultaneously

I need to run similiar multiple mp3 converting processes at the same time with windows command prompt.
As far as I know, if I start one converting process using batch file, only 1 console window apears and only one process starts. After this process is finished (and if there is a command line for it) starts another process and so on.
As I just tested, if I start multiple batch files at the same time, I have more then 75% time economy!!!!! It's very important form me, because overall time processing counts in months (in my case)!
So, my problem is - how to force start all other multiple batch files at the same time and make the main batch file wait for finished executing all others batches. And only then continiue actions in main batch file.
THANK YOU!

Resources