Wait for uninstaller to finish using batch - batch-file

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

Related

How to run multiple EXE's, Open File and XCOPY's in a Batch Script

I'm trying to create a batch script to install a particular program via SCCM (eventually).
Currently I'm just trying to get it to run on my own machine but running into to one or two problems.
The Installation as a manual process is a pain, needs to run 1 prerequisite EXE, then once finished run the main EXE. The process should then wait for that to finish before running the program itself and to be left open for 30 seconds (roughly) then kill that task. Lastly, I need to copy some config files over.
Typing individual lines manually works but struggling for an all in one solution. I've tried using timeouts, PAUSE, && - all seems to be failing to sequence the tasks one after the other.
vstor_redist.exe /q /norestart
&& APP.exe /Silent
&& cd "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\APP" "Shortcut.lnk"
&& taskkill /IM "APP.exe" /F
&& xcopy "config.config" %AppData%\COFIG\*.* xcopy "config.xml" %AppData%\CONFIG\*.*
I have found that I was able to achieve my goal by using the Start command with its /Wait option.
Example:
start /wait app.exe
start /wait app2.exe
And so on

How to handle batch file if need to kill the CMD's

I have to write a batch file which is suppose to kill all the currently running CMD processes, except one which is in Listening mode before regression runs. If there is any opened CMD in the same path as Regression, it ruines the regression. There would be probably 2 ways to achieve this:
1. Close all command prompts and start the listener (which can be done by calling a soft link).
2. Close all command prompts, except the one which would run the Regression.
I tried to kill the command prompts, however it ruined the regression as it killed the cmd which is expected to run the regression.
TASKKILL /F /IM cmd.exe /T
cd %appdata%\Microsoft\Windows\Start Menu\Programs\Startup
"Regression Run.lnk"
If I go ahead with approach 1, tried to kill all cmd instances and then run the link, as soon as TASKKILL is called the cmd running itself is also killed. The other 2 lines were never ever called.
For the other approach, I am not able to get any method how to add any exception while calling TASKKILL.
If any one has better idea around it or suggest a better way to achieve it.
Any guidance would be greatly appreciated.
This can be done by using TASKKILL /FI to filter out the PID of the current cmd.exe shell you are running. I would like to know an easier way of getting the current cmd.exe PID.
#ECHO OFF
SET "TEMPFILE=%TEMP%\regtest.tmp"
powershell -NoProfile -Command ^
"(Get-WmiObject -Class Win32_Process -Filter "processid=`'$pid`'").ParentProcessId" >"%TEMPFILE%"
SET /P "MYPID=" <"%TEMPFILE%"
ECHO MYPID is set to %MYPID%
IF EXIST "%TEMPFILE%" (DEL "%TEMPFILE%")
TASKLIST /FI "IMAGENAME eq cmd.exe"
TASKKILL /F /FI "PID ne %MYPID%" /IM cmd.exe
TASKLIST /FI "IMAGENAME eq cmd.exe"

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.

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

Resources