Checking for running .bat files - batch-file

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).

Related

Multiple tasks that do the same

I'm working on a project that create a x number of tasks that do the same thing, open the same Excel file, but in different intervals. The problem comes when two of the tasks run at the same time, a read only file errors with macros. I tried to put a limit in the bat file that the tasks run, first stopping open other excel files and before do the same with other tasks, but It doesn't work. There is the last code that I wrote.
#ECHO OFF
TASKLIST /FI "IMAGENAME eq TASKENG.EXE" 2>NUL | FIND /I "TASKENG">NUL && (
START %~dp0myExcelFile.xlsm
EXIT
)

Start batch > start exe + stop exe > batch activation

Batch:
#echo off
tasklist /FI "IMAGENAME eq custom.bin" 2>NUL | find /I /N "custom.bin">NUL
if "%ERRORLEVEL%"=="0" goto mycode
goto quite
:mycode
#RD /S /Q "customfolder"
:quite
exit
now I need to make it work as in the header, step by step:
Run batch
Run custom.bin
Close custom.bin
And now our batch understand that we ve closed custom.bin and
execute custom folder and custom files deletion. Can someone give a
hand? Is it even possible that kind of stuff?
I think you're trying to do something like this:
#echo off
start /wait custom.bin
echo custom.bin has finished running
pause
This starts custom.bin, and waits until it closes, and after that continues to run the rest of the batch-file

Wait for uninstaller to finish using batch

I would like to uninstall a few programs using batch and wait for each program to finish uninstalling before doing the other. The problem is that some of these uninstallers don't call other programs and aren't in the task manager, so START /WAIT wouldn't work. Any ideas would be helpful.
Assuming you are using Windows and that the programs were installed with .msi packages,
Uninstalling a program in a batch file is as easy as using MSIEXEC. Full documentation on using MSIEXEC can be found on Microsoft Technet
To find the GUID of the installation that you would like to Uninstall, just use wmic in Command prompt:
wmic product get > C:\InstalledProgramsList.txt
Then navigate to C:\InstalledProgramsList.txt and use ctrl + F to find the desired product and associated GUID
Ok. I think I got it. This is the code I'm using, where I created a subroutine that waits for the task to finish.
:waitforuninstallation
timeout /T 1 /NOBREAK >nul
tasklist /FI "IMAGENAME eq %1" 2>NUL | find /I /N "%1">NUL
if %ERRORLEVEL% EQU 0 goto waitforuninstallation
exit /b
This can be called using:
call :waitforuninstallation PROGRAMNAME.exe
Where PROGRAMNAME is the task running in the task manager.
Edit: made the subroutine carry an argument
There's a really annoying trend with free installers made by NSIS: Cisco Amp, Folding#Home, PsychoPy, Anaconda, PyCharm. The uninstaller exe quits, and something called Un_A.exe takes over and does the rest. In this case, you'd have to wait for that process to finish instead.
powershell while (! (get-process Un_a -ea 0)) { sleep 1 }; 'waiting'; wait-process Un_a
Or if you want the exit code of un_a:
powershell while (! ($proc = get-process Un_A -ea 0)) { sleep 1 }; $handle = $proc.handle; 'waiting'; wait-process Un_A; exit $proc.exitcode

Giving control to app started from bat 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

Create batch file to block specific programs

How to create an autorun batch file to block/close portable programs?
For example:
if (xxx.exe is running)
then (close it immediately OR block access to internet)
Thank you.
I have tested this batch command:
#echo off
:TOP
tasklist /FI "IMAGENAME eq taskmgr.exe" /FO | grep taskmgr.exe
if ERRORLEVEL == 0 taskkill /f /im taskmgr.exe
GOTO TOP
but it's failed to run, what is the problem?
I'm pretty sure this isn't possible, in batch anyways.
You could edit registry keys for certain files to stop them from being run but this would be system wide, not just for portable programs.
There are too many variables to consider, and this really isn't something you can do in batch.

Resources