How do I make an exe run automatically when another exe is launched?
Example :
EXE 1 is launched & running
EXE 2 will run automatically once it see that the EXE 1 has already launched.
What is the command do I use in .bat file or autorun.inf?
Here you go
start exe1.exe
:LOOP
tasklist /nh /fi "imagename eq exe1.exe" | find /i "exe1.exe" >nul && start exe2.exe || goto :LOOP
That will start exe1.exe and will keep looking to see if it has started before running exe2.exe.
#ECHO OFF
START /D "path" myexe1.exe
START /D "path" myexe3.exe
Taken from here.
Then save the file as "somename.bat"
Edit
Due to your last comment here's what I found
#echo off
tasklist /nh /fi "imagename eq chrome.exe" | find /i "chrome.exe" >nul && (
echo Windows Media Player is running
) || (
echo Chrome is not running
)
pause>nul
I just copied the code from the link, google can be very helpful.
Related
I am very new to batch files and am trying to create a script that checks if a process is running and alert if the process is/is not running. This is what I have gotten from google, but isn't working the way I am wanting.
tasklist /FI "IMAGENAME eq example.exe" 2>NUL | find /I /N "example.exe">NUL if "%ERRORLEVEL%"=="0" echo Program is running
If this is batch (as the attempt from the question indicates), according to [MS.Docs]: Using multiple commands and conditional processing symbols:
&& [...]
command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.
Example:
C:\>(tasklist /FI "IMAGENAME eq svchost.exe" 2>NUL | findstr /I /N "svchost.exe" >NUL) && (echo Program running)
Program running
C:\>(tasklist /FI "IMAGENAME eq svchost1.exe" 2>NUL | findstr /I /N "svchost1.exe" >NUL) && (echo Program running)
You are trying to over complicate it. Copy the below into a batch file. Also use findstr instead.
tasklist /fi "imagename eq example.exe" | findstr /i "example.exe" >nul
If %errorlevel%==0 echo example.exe running.
If %errorlevel%==1 echo example.exe Not running.
This is even less complicated, there's no need to filter by ImageName when you're using Find to filter too:
TaskList|Find /I "example.exe">Nul&&(Echo Running)||Echo Not running
I would like to copy a file every 10 minutes when Firefox is open.
I am not sure what is the best way to do that but so far I have created a .bat that I wanted to link to a task in Task Scheduler that will run it every 10 minutes.
This is the command in the .bat :
copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
But Task Scheduler doesn't allow me to add a condition (= run only if firefox is open) nor to run a task every 10 minutes.
So here are my questions:
If a .bat is a good way to do this, how can I run a .bat with those two parameters?
If a .bat is not a good way to do this, what method would you recommend?
You could just have a batch file like this:
#echo off
:loop
tasklist /FI "IMAGENAME eq firefox.exe" 2>NUL | find /I /N "firefox.exe">NUL
if "%ERRORLEVEL%"=="0" copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
timeout /t 600 /nobreak
goto loop
This would be a standalone batch-script you would have to start yourself, or put in the startup folder.
You could also just take the
#echo off
tasklist /FI "IMAGENAME eq firefox.exe" 2>NUL | find /I /N "firefox.exe">NUL
if "%ERRORLEVEL%"=="0" copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
and run that every 10 minutes from task scheduler ofcourse.
You could also make this into your firefox shorcut and end it when the program isn't active anymore, like this:
#echo off
set programName=firefox.exe
start %programName%
:loop
tasklist /FI "IMAGENAME eq %programName%" 2>NUL | find /I /N "%programName%">NUL
if "%ERRORLEVEL%"=="0" copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
if "%ERRORLEVEL%"=="1" exit /b
timeout /t 600 /nobreak
goto loop
echo off
:loop
tasklist /fi "imagename eq Notepad.exe" | find "INFO:" > nul
if errorlevel 1 goto loop
wordpad.exe
This is not working in XP. It works fine in Windows 7.
When notepad.exe does not exist in the task list, tasklist /fi "imagename eq Notepad.exe" dumps the "INFO:" line to stderr in Windows XP. You could redirect stderr to stdout with 2>&1, but it's easier just to find /i "notepad" instead.
On a side note, instead of doing if errorlevel 1 you could use conditional execution.
#echo off
setlocal
:loop
rem // Output nothing for the following code block.
>NUL 2>NUL (
rem // Make sure notepad is not running before continuing.
tasklist /fi "imagename eq notepad.exe" | find /i "notepad" && (
rem // Notepad is in tasklist. Sleep 1 second, then check again.
timeout /t 1 /nobreak || ping -n 2 localhost
goto loop
)
)
wordpad.exe
I need a batch (.bat) file that opens a program if it's not open, and stops the program if it is open. I have a game where when the launcher is closed, it stays open in the background. And I have to end it with task manager or else I can't launch it because steam doesn't like it when an app is open two times (it doesn't allow it), so I would like a batch file that does this for me, then bind it to a macro.
To check if your programm is running : (Here an example with notepad.exe)
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && echo %MyProcess% Is running || echo %MyProcess% Is not running
So you can do like that :
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && Taskkill /F/IM "%MyProcess%" || start "%MyProcess%"
This is another way to do it:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find "." > nul && taskkill /f /im "Launcher.exe" & goto :EOF
tasklist /fi "imagename eq Launcher.exe" |find "." > nul || start "" steam://rungameid/243870 & goto :EOF
So I figured this out:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find ":" > nul
if errorlevel 0 taskkill /f /im "Launcher.exe" && exit
start steam://rungameid/243870
exit
The logic behind it is that if you can kill the process, then end the prompt before you can create a new session of the process. But if the process can't be killed, then make a new session of it, then end the prompt.
I have a script while ran from a CMD runs normally, however when I run it from task scheduler although it does everything as intended, the script status hangs on "Running"
Here is the script:
#echo OFF
tasklist /FI "IMAGENAME eq utorrent.exe" 2>NUL | find /I /N "utorrent.exe">NUL
if "%ERRORLEVEL%"=="0" (echo UTorrent is running nothing to do) ELSE (
echo UTorrent is not running, starting Utorrent!
start C:\Users\Adonis\AppData\Roaming\uTorrent\uTorrent.exe
)
tasklist /FI "IMAGENAME eq steam.exe" 2>NUL | find /I /N "steam.exe">NUL
if "%ERRORLEVEL%"=="0" (echo Steam is running nothing to do) ELSE (
echo Steam is not running, starting Steam!
start X:\Games\SteamLibrary\Steam.exe
)
exit
Can anyone advise why this is happening? IE why is the script stuck in running state through scheduler?
The OS in question is windows 8.
It is set to run only when user is logged on and with highest privileges.
Thanks!
Try to launch the application with the /B parameter to don't stop the execution of he script.
Also I've improved the syntax of your code:
#echo OFF
(Tasklist /FI "IMAGENAME eq utorrent.exe" | find /I "utorrent.exe">NUL && (
Echo: UTorrent is running nothing to do)
) || (
Echo: UTorrent is not running, starting Utorrent!
Start /B "" "C:\Users\Adonis\AppData\Roaming\uTorrent\uTorrent.exe"
)
(Tasklist /FI "IMAGENAME eq steam.exe" | find /I "steam.exe">NUL && (
Echo: Steam is running nothing to do)
) || (
Echo: Steam is not running, starting Steam!
Start /B "" "X:\Games\SteamLibrary\Steam.exe"
)
Exit