How to close specified opened folders using batch file - 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!)

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"

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.

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

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.

How to stop process from .BAT file?

So I have process I started from one bat file. How to stop it from another?
To terminate a process you know the name of, try:
taskkill /IM notepad.exe
This will ask it to close, but it may refuse, offer to "save changes", etc. If you want to forcibly kill it, try:
taskkill /F /IM notepad.exe
As TASKKILL might be unavailable on some Home/basic editions of windows here some alternatives:
TSKILL processName
or
TSKILL PID
Have on mind that processName should not have the .exe suffix and is limited to 18 characters.
Another option is WMIC :
wmic Path win32_process Where "Caption Like 'MyProcess.exe'" Call Terminate
wmic offer even more flexibility than taskkill .With wmic Path win32_process get you can see the available fileds you can filter.
When you start a process from a batch file, it starts as a separate process with no hint towards the batch file that started it (since this would have finished running in the meantime, things like the parent process ID won't help you).
If you know the process name, and it is unique among all running processes, you can use taskkill, like #IVlad suggests in a comment.
If it is not unique, you might want to look into jobs. These terminate all spawned child processes when they are terminated.
I just wanted to kill all instances of Chrome when the browser won't open (recent Chrome annoying bug). You can end up with a bunch of chrome.exe processes running from 2 to ?.
I just did a clean and quick check to see if it's running and kill it if it is. The pause at the end is only so I can view the results, it isn't needed.
#echo off
:again
taskkill /F /IM "chrome.exe"
if errorlevel=0 goto end
if errorlevel=1 goto again
:end
pause
it works well for me
taskkill /F /IM notepad.exe this is the best way to kill the task from task manager.
Edit:
call runntaskkill.bat is changed to call taskkillapp.bat or else it will end up with the same file.
You can make two batch files. One named runtaskkill.bat and another named taskkillapp.bat. To the file runtaskkill.bat add the following code:
call taskkillapp.bat [filenamehere].
And to taskkill.bat:
taskkill /f /im %1.
Just make sure that the two files are in the same directory (folder).
Why don't you use PowerShell?
Stop-Process -Name notepad
And if you are in a batch file:
powershell -Command "Stop-Process -Name notepad"
powershell -Command "Stop-Process -Id 4232"
Here is how to kill one or more processes from a .bat file.
Step 1. Open a preferred text editor and create a new file.
step 2. To kill one process use the 'taskkill' command, with the '/im' parameter that specifies the image name of the process to be terminated. Example:
taskkill /im examplename.exe
To 'force' kill a process, use the '/f' parameter which specifies that processes be forcefully terminated. Example:
taskkill /f /im somecorporateprocess.exe
To kill more than one process you repeat the first part of step 2 with the appropriate processes for the process name. Example:
taskkill /im examplename.exe
taskkill /im examplename1.exe
taskkill /im examplename2.exe
or
taskkill /f /im examplename.exe
taskkill /f /im examplename1.exe
taskkill /f /im examplename2.exe
Step 3. Save your file to your desired location with the .bat extension.
Step 4. Click the newly created bat file to run it.
To just click file and run with no message "press any key to continue"
#echo off
call taskkill /f /im adb.exe /im OfficeClickToRun.exe
pause>nul
exit /b 0
add any amount of processes:
/im the_process_name.exe
TIP:
Create a shortcut of the .bat file somewhere (you can drag it to start menu/all programs) , go to the shortcut file properties and add a shortcut key 😎

Resources