Giving control to app started from bat file - batch-file

I have a batch script that tests for the existence of a running program (JoyToKey.exe), if it's not running, start it, if it is running, move on.
Once the app is running, I then launch another app (mgalaxy.exe) but although it is maximized and I can see it, it does not have control. That is I need to do an to get control of the running mgalaxy.exe.
How can I do this so that I don't need to do the . It used to work perfectly on Windows 7, but under Windows 8.0 I have this problem. Code in batch file is:
#echo off
tasklist /FI "IMAGENAME eq JoyToKey.exe" 2>NUL | find /I /N "JoyToKey.exe">NUL
if NOT "%ERRORLEVEL%"=="0" (
echo Launching JoyToKey
cd C:\Mame\jtk374en
START /MIN JoyToKey.exe
)
echo Launching mGalaxy
cd c:\Mame
start mgalaxy.exe
exit

Try adding timeout. I'd be interested to hear how it goes, as I also found focus issues when launching programs from a batch file. This worked for me when starting a text editor.
start mgalaxy.exe
timeout 3 /nobreak
exit

Related

How to start a .exe after a specific game closes with a batch file

I have just did this code with a batch file that closes the windows process explorer.exe. It starts some other programs that help with stuttering and closing explorer.exe helps quite a bit.
Thing is to start the game, I need to open a launcher but I want to close the launcher when I start the game. The game is fortnite. In the code it ends explorer.exe but when I close fortnite (Not the launcher), I want explorer.exe to start up.
Code:
START C:\"Program Files (x86)"\"Epic Games"\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe
START C:\Users\Yqmxh\Documents\Optimization(s)\TimerResolution.exe
START C:\Users\Yqmxh\Documents\Optimization(s)\"Mz Game Accelerator.lnk"
Taskkill /F /IM explorer.exe
start /wait "C:\Windows\explorer.exe"
exit
I don't know how much cpu time this consumes, but it would work at least:
:beginning
tasklist | FIND "FortniteClient-Win64"
IF NOT %ERRORLEVEL% == 1 (ECHO process was found
ECHO now waiting for six seconds
PING localhost -n 6 >NUL
GOTO beginning
)
ECHO the EXE is not running anymore
explorer
Since I considered the FOR loop too complex, I settled with a GOTO, which I hope I won't get downvoted for ;)
I think it's pretty self-explaining, but feel free to ask, if I'm wrong.

Checking for running .bat files

I need to have a .bat file running on a PC all the time, which exports data every 8 hours.
The .bat file I would like to build would make hourly checks to see if the other .bat file is running, and if not start it.
I know tasklist can't find .bat files which is why I'm using WINDOWTITLE instead.
I am assuming that ERRORLEVEL = 1 means the test failed.
I have looked around a lot and best I have come up with is this but it doesn't work.
tasklist /FI "WINDOWTITLE eq Export"
if errorlevel=0 START C:\ExportApps\Export.bat
TIMEOUT /T 3600
GOTO LOOP
When it checks for the second time it starts another instance of the .bat file.
If I change the ERRORLEVEL to 1 I get the following:
INFO: No tasks are running which match the specified criteria.
tasklist doesn't set errorlevel to 1 when there is no finding. (Ab)use find to get a correct errorlevel:
tasklist /fi "windowtitle eq Export" | find /c "cmd.exe" 1>nul && exit /b
REM code for restarting
Schedule it to be run every hour (or consider Compo's advice to schedule your primary batch file, when it actually doesn't have to run all the time for some reason).

How to kill a service in a batch file every 20 seconds?

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

CMD Batch command "start /wait" not working in Windows 8.1?

I'm working on a very simple bat file script that worked fine in Windows 7, but seems to be having issues in Windows 8. It relies heavily on "start /wait" to execute 1 file at a time. It seems that the "start" command works just fine. However, given the folder structure, I have referenced each item as such in a command:
start /wait ./folder1/app1.exe
start /wait ./folder2/app2.exe
start /wait ./folder3/app3.exe
While this worked just fine in W7. In Windows 8.1, this results in an error message stating: "Windows cannot find '.\folder1\app1.exe'. Make sure you typed the name correctly, and then try again."
So... Did MS replace the .\ wildcard in Windows 8 with something else? Or change it just slightly?
Yes, I know. This isn't really programming (still studying C/CPP), but any insight would be greatly appreciated.
./ is not a wildcard. * and ? are wildcards - they match any number of any characters /one any-character.
/ is a switch indicator. \ is a directory separator. Dangerous to confuse the two - it confuses cmd.
.\ means 'relative to the current directory`. Perhaps you should display the current directory using
echo %cd%
to make sure that cmd's idea of the current directory and yours agree. Also make sure the targets exist and are not hidden files.
I had the same problem with a cmd file on my usb drive.
In Windows 7 my application started with ...
START /WAIT %usbDrive%\Truecrypt\TrueCrypt.exe /volume %usbDrive%\%Container% /k %usbDrive%\Truecrypt\Truecrypt.key /cache /wipecache /quit
... but in Windows 8.1 it has not waited to put my password in the TrueCrypt screen.
As a workaround for both Windows versions this works for me fine now.
START %usbDrive%\Truecrypt\TrueCrypt.exe /volume %usbDrive%\%Container% /k %usbDrive%\Truecrypt\Truecrypt.key /cache /wipecache /quit
:CHECK
ping -n 2 localhost 1>NUL 2>NUL
TASKLIST /FI "IMAGENAME eq TrueCrypt.exe" > TrueCrypt.txt
FIND /N "TrueCrypt.exe" TrueCrypt.txt 1>NUL 2>NUL
IF %ERRORLEVEL%==0 ( GOTO CHECK ) ELSE ( DEL TrueCrypt.txt )
At first you start the program you want, then you need a mark to go back and check if your program is still running. The next thing is to wait about 2 seconds (for ping -n ... you can also use timeout). Then you can filter the tasklist with the exeutable name and write it in a check text file. With the find command you can grep in the check file... Finally the last line, if the application is not in the tasklist anymore the check text file will be deleted otherwise the cmd is jumping to the check mark.
I hope it will help

How to run multiple programs using batch file

I like to run two programs using batch file, but the condition is, the second program must start only after the first program loaded, so is there any way to control using timer to control when the program starts.
I needed the same thing, and found out that following thing works as hoped:
start D:\Michal\Xming\Xming.exe -multiwindow
start D:\Michal\Xming\putty.exe
start D:\Michal\WinSCP\WinSCP.exe
And it all is saved in file Login.bat.
BTW, I am running Win7 but I doubt that this has any impact.
Basically, you could try this approach (not tested):
Run the first program using the start command.
Check the task list in a loop to see if the program has appeared there.
Impose some time limitation to the said loop.
Run the next program in case of success, exit with notification otherwise.
The scripting might look like this:
#ECHO OFF
START program1.exe
FOR /L %%i IN (1,1,100) DO (
(TASKLIST | FIND /I "program.exe") && GOTO :startnext
:: you might add here some delaying
)
ECHO Timeout waiting for program1.exe to start
GOTO :EOF
:startnext
program2.exe
:: or START program2.exe
Keep in mind that the timing is not precise, especially if you are going to insert delays between the task list checks.
I think this might be irrelevant here but would like share the following:
I 've created the following batch file and run it whenever I open my laptop in the office to open relevant programs at a single click.
Kept this file at Desktop and created a folder where I put all shortcuts for relevant programs.
So, I run these shortcuts in the batch file as follows:
#ECHO off
start C:\Users\User1\Desktop\Softwares\IE
start C:\Users\User1\Desktop\Softwares\Googletalk
start C:\Users\User1\Desktop\Softwares\YahooMessenger
start C:\Users\User1\Desktop\Softwares\Program4
start C:\Users\User1\Desktop\Softwares\Program5
I have also find a small hack to do it, just using a ping command with -n switch as follows:
start /d "C:\Program Files (x86)\Mobile Partner\" MobilePartner.exe
ping 127.0.0.1 -n 8
start /d "F:\Other Applcations\System Tools\OS Tweak\" dragfullwindows.exe
I wrote this answer as I am on Windows 10 and it is 2021.
And this provides a few more ideas for newage programs, that should run in the taskbar(in the background).
Here is my "Work Start.bat" batch file sitting on my desktop:
rem Work Start Batch Job from Desktop
rem Launchs All Work Apps
#echo off
rem start "Start VS Code" "C:\Users\yourname\AppData\Local\Programs\Microsoft VS Code\code.exe"
start "Start OneDrive" "C:\Users\yourname\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
start "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe"
start "Start Clipboard" "C:\Program Files\Beyond Compare 4\BCClipboard.exe"
start "Start Cisco AnyConnect" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
start chrome
start firefox
start skype
start "Start Teams" "C:\Users\yourname\AppData\Local\Microsoft\Teams\current\Teams.exe" /MIN
start Slack
start Zoom
sleep 10
taskkill /IM "explorer.exe"
taskkill /IM "teams.exe"
taskkill /IM "skype.exe"
taskkill /IM "slack.exe"
taskkill /IM "zoom.exe"
taskkill /IM "cmd.exe"
#echo on
Some Apps would not start with a simple "start app" command, so I used the full path.
For some reason some were found in my user appdata folder and not in program files, I do not understand this behaviour of program storage, it makes no sense.
I used a time delay so that the apps could fully start before I sent them to the background using taskkill command
I killed explorer.exe because OneDrive opens explorer
I killed cmd.exe because it opened and stayed opened due to badly behaving apps.
The rest I killed so that they would just move to the background.
Here is my "Work End.bat" batch file to forceably close everything:
rem Work End Batch Job from Desktop
rem Forcibly Closes All Work Apps
#echo off
taskkill /f /IM OneDrive.exe
taskkill /f /IM GoogleDriveSync.exe
taskkill /f /IM BCClipboard.exe
taskkill /f /IM "vpnui.exe"
taskkill /f /IM "chrome.exe"
taskkill /f /IM "firefox.exe"
taskkill /IM "explorer.exe"
taskkill /f /IM "teams.exe"
taskkill /f /IM "skype.exe"
taskkill /f /IM "slack.exe"
taskkill /f /IM "zoom.exe"
#echo on
I do have to ensure I have saved all my work, and that files are no longer syncing.
Possibly I will need a batch file that kills everything except file sync.
The good thing about forceably killing Chrome and firefox is they ask to be restored on next start, so I can continue where I left off, assuming I saved everything.
If I don't forceably kill these they stay in the background, if I close using the Cross they do not offer me to start from where I left off.
I could then start my gaming files, in another batch file but this would be similar to the first file.
Now I can turn off the default "Start Up" Apps and use this "Work Start.bat" , because the Start Up Apps annoy me when I start my pc just to game.

Resources