Start batch > start exe + stop exe > batch activation - batch-file

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

Related

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"

How to close specified opened folders using batch file

Three days ago I was trying to close folders using batch file, I've checked multiple sites for the script and none of them worked. For example I open manually without start the folder "CD_Restored" which is located in C: then I open a batch file inside it to close it, so I wrote:
TASKKILL /F /FI "WINDOWTITLE eq CD_Restored" /IM explorer.exe
and I got this message:
"information: no in-service task matches the specified criteria"
Also I've wrote:
#echo off
set shell = createobject("wscript.shell") : if shell.appactivate("CD_Restored") then shell.sendkeys "%{F4}"
and didn't work either
You can try with sendkeys.bat:
call sendKeys.bat "CD_Restored" "%{F4}"
but it will work only if the folder window is NOT minimized.
You can follow these steps:
#echo off
cd /d C:\
start C:\Windows\explorer.exe CD_Restored
pause
rem Terminate process:
taskkill /F /FI "WINDOWTITLE eq CD_Restored" /IM explorer.exe > nul
if NOT %errorlevel% EQU 0 (echo It seems that Window title named "CD_Restored" doesn't exist!) else (echo Process successfuly terminated!)

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

Batch START command just start a blank command window not run my script

So I am working on a project where I need to start another batch file, Task Options.bat from this batch file, Task.bat in the same directory, but not close or pause the original file, so this is the script of Task.bat
#ECHO off
TITLE Task
START /D %~dp0 "Task Options.bat"
:loop
TASKLIST /FI "MEMUSAGE gt 20000"
timeout 10 >nul
cls
goto loop
Other lines are irrelevant but the third line of code: When I run the file, it just opens a blank cmd window that titled "Task Options.bat". There are codes in the Task Options.bat file so if it is being ran it would output something. So where I did wrong?
Try this:
#ECHO off
TITLE Task
START "Task Options" /D "%~dp0" %ComSpec% /k "Task Options.bat"
:loop
TASKLIST /FI "MEMUSAGE gt 20000"
timeout 10 >nul
cls
goto loop
Note that you may want to change the /k to /c if you want the Options window to go away after the options.bat script runs. Use /k until you've got all the bugs worked out.
Your version did not work because you had the command line arguments all mixed up. This sets the console window title to Options, the starting directory path to the same directory the script is in, uses the %COMSPEC% variable to find the correct cmd.exe file and hands it the Options.bat script file name to execute.

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