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.
Related
I need to open an XML file save it without modifying it and then restart a program, right now I have two batch files to do it.
notepad.exe "C:\Users\Max Singh\AppData\Local\Microsoft\Windows Live\Services\Cache\config.xml"
Then I manually save in notepad then run the second file,
TASKKILL /F /IM sidebar.exe
START sidebar.exe
I want to be able to do this in one file without me needing to manually save but I don't know how in a batch file.
Assuming that the only reason to open and save the file is to change its modified time, you could try:
COPY /y config.xml config.xml.new
RENAME config.xml.new config.xml
TASKKILL /F /IM sidebar.exe
START sidebar.exe
I presume this is the commonly touted FIX
For a Windows 7 Sidebar is "Hung (non updating)"
Thus needs to be killed and restarted !
I am not certain if this is the best method, however you are the client.
but do read the notes in the comments from others, with more experience.
pushd "C:\Users\Max Singh\AppData\Local\Microsoft\Windows Live\Services\Cache"
type config.xml > config.tmp
copy /y config.tmp config.xml
del config.tmp
popd
Rem Try the following line either without the /F or change it to /T
Taskkill /F /IM sidebar.exe
Rem The suggested method is to wait a few seconds so have added 5 seconds
Timeout 5
start sidebar.exe
Note the above is the minimal file for testing and once happy you can add
#echo off as your first line
You should add the full paths as per comments to guard against loose cannons that may have a similar name. So the second half should be.
%SystemRoot%\System32\taskkill.exe /IM sidebar.exe
%SystemRoot%\System32\timeout.exe 5
Start "" "%ProgramFiles%\Windows Sidebar\sidebar.exe"
I've a Intranet with an asmx Page to do some works. I need to execute it, each day. I make a batch file, put it in task manager of my server. It's ok to run... But I add a part to kill this one after severals minutes and the problem is here.
This is my batch file... Where is my error... I use a label (ImportToDo) to create a task and ask it to kill
#echo off
start "ImportToDo" iexplore.exe http://MySrv:MyPort/interne/Import.asmx/Importation
Timeout /T 80 /NoBreak>NUL
taskkill /F /IM "ImportToDo" /T > nul
Thanks for yours helps
I put my batch late in the day to be sure do not close IE of anotherone.
And I take solution odf JosefZ
taskkill /F /IM iexplore.exe /T
It's Ok
Thanks
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.
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
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 😎