Can you put goto and if then into one line? - performance

I am trying to put the goto and the if then command in the same line because so I don't have to make a another BAT files to get the job done.
Here an example of the code that I am makeing so far:
#echo off
SET _cliportaskkill=0
SET _startffox=0
If "%_cliportaskkill%"=="2" goto everything
If "%_cliportaskkill%"=="1" goto cliponly
If "%_cliportaskkill%"=="0" goto taskkillonly
:cliponly
echo|set /p=username#email.com|clip
EXIT
:taskkillonly
TASKKILL /F /T /IM Greenshot.exe & TASKKILL /F /T /IM iprntctl.exe & TASKKILL /F /T /IM iprntlgn.exe & TASKKILL /F /T /IM smax4pnp.exe & TASKKILL /F /T /IM ZenNotifyIcon.exe & TASKKILL /F /T /IM ZenUserDaemon.exe & TASKKILL /F /T /IM RAVBg64.exe & TASKKILL /F /T /IM nwtray.exe & TASKKILL /F /T /IM RtkNGUI64.exe & TASKKILL /F /T /IM BingSvc.exe & TASKKILL /F /T /IM Spotify.exe & TASKKILL /F /T /IM SpotifyWebHelper.exe & TASKKILL /F /T /IM FacebookGameroom.exe & TASKKILL /F /T /IM "Facebook Gameroom Browser.exe" & TASKKILL /F /T /IM zapp.exe & TASKKILL /F /T /IM steamwebhelper.exe & TASKKILL /F /T /IM Steam.exe & TASKKILL /F /T /IM lync.exe
EXIT
:everything
echo|set /p=username#email.com|clip
TASKKILL /F /T /IM Greenshot.exe & TASKKILL /F /T /IM iprntctl.exe & TASKKILL /F /T /IM iprntlgn.exe & TASKKILL /F /T /IM smax4pnp.exe & TASKKILL /F /T /IM ZenNotifyIcon.exe & TASKKILL /F /T /IM ZenUserDaemon.exe & TASKKILL /F /T /IM RAVBg64.exe & TASKKILL /F /T /IM nwtray.exe & TASKKILL /F /T /IM RtkNGUI64.exe & TASKKILL /F /T /IM BingSvc.exe & TASKKILL /F /T /IM Spotify.exe & TASKKILL /F /T /IM SpotifyWebHelper.exe & TASKKILL /F /T /IM FacebookGameroom.exe & TASKKILL /F /T /IM "Facebook Gameroom Browser.exe" & TASKKILL /F /T /IM zapp.exe & TASKKILL /F /T /IM steamwebhelper.exe & TASKKILL /F /T /IM Steam.exe & TASKKILL /F /T /IM lync.exe
EXIT
===================================================================================================================
#echo off
If "%_startffox%"=="1" goto newwindow
If "%_startffox%"=="0" goto newtab
:newwindow
CD /D "C:\Program Files (x86)\Mozilla Firefox"
firefox.exe "https://www.outlook.com/" "http://everybodyedits.com/" "http://www.kongregate.com/accounts/username/recently-played"
EXIT
:newtab
CD /D "C:\Program Files (x86)\Mozilla Firefox"
firefox.exe -new-tab "https://www.outlook.com/" -new-tab "http://everybodyedits.com/" -new-tab "http://www.kongregate.com/accounts/username/recently-played"
EXIT
Whcih is used for this BAT file:
Old verson
#echo on
START "taskkilling and cliping" CMD /c "#echo off & echo|set /p=username#email.com|clip & TASKKILL /F /T /IM Greenshot.exe & TASKKILL /F /T /IM iprntctl.exe & TASKKILL /F /T /IM iprntlgn.exe & TASKKILL /F /T /IM smax4pnp.exe & TASKKILL /F /T /IM ZenNotifyIcon.exe & TASKKILL /F /T /IM ZenUserDaemon.exe & TASKKILL /F /T /IM RAVBg64.exe & TASKKILL /F /T /IM nwtray.exe & TASKKILL /F /T /IM RtkNGUI64.exe & TASKKILL /F /T /IM BingSvc.exe & TASKKILL /F /T /IM Spotify.exe & TASKKILL /F /T /IM SpotifyWebHelper.exe & TASKKILL /F /T /IM FacebookGameroom.exe & TASKKILL /F /T /IM "Facebook Gameroom Browser.exe" & TASKKILL /F /T /IM zapp.exe & TASKKILL /F /T /IM steamwebhelper.exe & TASKKILL /F /T /IM Steam.exe & TASKKILL /F /T /IM lync.exe"
START "start new firefox in three new tabs" CMD /c "#echo off & CD /D "C:\Program Files (x86)\Mozilla Firefox" & firefox.exe "https://www.outlook.com/" "http://everybodyedits.com/" "http://www.kongregate.com/accounts/username/recently-played""
So I am trying make a BAT file that have options that I can use for my new verson that I use when I log on to the computer.
So far I try testing the one of code through a different BAT file so here what I have so far:
SET _startffox=0
CHOICE /C wt /m "w open new window, t open new tab"
IF errorlevel 2 set _startffox=0 & goto line
IF errorlevel 1 set _startffox=1 & goto line
:line
pause & echo %_startffox%
(echo on & (If "%_startffox%"=="1" goto newwindow); (If "%_startffox%"=="0" goto newtab); (:newwindow & ECHO "go to new window firefox" & ECHO "Hi windows" & pause & EXIT); (:newtab & ECHO "go to new tabs firefox" & ECHO "Hi tabs" & pause & EXIT))
pause
so far it does not work the way I wahntd to work.

#echo off
setlocal
SET "_cliportaskkill=0"
SET "_startffox=0"
If "%_cliportaskkill%"=="2" call :everything
If "%_cliportaskkill%"=="1" call :cliponly
If "%_cliportaskkill%"=="0" call :taskkillonly
If "%_startffox%"=="1" call :newwindow
If "%_startffox%"=="0" call :newtab
EXIT /B
:cliponly
echo|set /p=username#email.com|clip
EXIT /B
:taskkillonly
TASKKILL /F /T^
/IM Greenshot.exe^
/IM iprntctl.exe^
/IM iprntlgn.exe^
/IM smax4pnp.exe^
/IM ZenNotifyIcon.exe^
/IM ZenUserDaemon.exe^
/IM RAVBg64.exe^
/IM nwtray.exe^
/IM RtkNGUI64.exe^
/IM BingSvc.exe^
/IM Spotify.exe^
/IM SpotifyWebHelper.exe^
/IM FacebookGameroom.exe^
/IM "Facebook Gameroom Browser.exe"^
/IM zapp.exe^
/IM steamwebhelper.exe^
/IM Steam.exe^
/IM lync.exe
EXIT /B
:everything
echo|set /p=username#email.com|clip
TASKKILL /F /T^
/IM Greenshot.exe^
/IM iprntctl.exe^
/IM iprntlgn.exe^
/IM smax4pnp.exe^
/IM ZenNotifyIcon.exe^
/IM ZenUserDaemon.exe^
/IM RAVBg64.exe^
/IM nwtray.exe^
/IM RtkNGUI64.exe^
/IM BingSvc.exe^
/IM Spotify.exe^
/IM SpotifyWebHelper.exe^
/IM FacebookGameroom.exe^
/IM "Facebook Gameroom Browser.exe"^
/IM zapp.exe^
/IM steamwebhelper.exe^
/IM Steam.exe^
/IM lync.exe
EXIT /B
:newwindow
CD /D "C:\Program Files (x86)\Mozilla Firefox"
firefox.exe^
"https://www.outlook.com/"^
"http://everybodyedits.com/"^
"http://www.kongregate.com/accounts/username/recently-played"
EXIT /B
:newtab
CD /D "C:\Program Files (x86)\Mozilla Firefox"
firefox.exe^
-new-tab "https://www.outlook.com/"^
-new-tab "http://everybodyedits.com/"^
-new-tab "http://www.kongregate.com/accounts/username/recently-played"
EXIT /B
Can you put goto and if then into one line?
Yes.
Labels though need to be at the start of a line and can be preceded
by spaces used for indenting.
Advise you use call instead of goto in many instances as it will
give good program flow as call goes to the label and then returns
back to the line of the caller. Using goto goes to the label and
then you may need to use goto again to go somewhere else and thus
you go all over and may eventually may become lost. So using call
allows all the main code to be grouped together and call labels as
needed.
taskkill can handle multiple images in the one process thus may be
more efficient.
Use ^ to continue the command over to the next line to line up the
arguments vertically. Can improve readability and maintenance.
Use of EXIT /B can exit a label or the script.
Use of EXIT closes the interpreter which is usually undesirable
for a script.
Be free to update the code using CHOICE or any other new ideas
you may have.
Example of indented label.
#echo off
if not "%~1" == "" call :level0 %*
exit /b
:level0
echo test indented label
:level1
echo "%~1"
shift
if not "%~1" == "" goto :level1
echo done
exit /b
Call the example script with arguments i.e. main.cmd 1 2 3. The interpreter will find :level1 even though it is indented.

Still haven't figure out how to put in one line and updated michael_heath code with a litte bit of mine:
#echo on
setlocal
CHOICE /C ectfx /m "e do everything, c clip only, t taskkill only, f skip this, x close this"
If errorlevel 5 EXIT /B
If errorlevel 4 goto :choiceff
If errorlevel 3 call :taskkillonly
If errorlevel 2 call :cliponly
If errorlevel 1 call :everything
:choiceff
CHOICE /C wtx /m "w open new window, t open new tab, x close this"
If errorlevel 3 EXIT /B
If errorlevel 2 call :newtab
If errorlevel 1 call :newwindow
EXIT /B
:cliponly
echo|set /p=username#email.com|clip
EXIT /B
:taskkillonly
TASKKILL /F /T^
/IM Greenshot.exe^
/IM iprntctl.exe^
/IM iprntlgn.exe^
/IM smax4pnp.exe^
/IM ZenNotifyIcon.exe^
/IM ZenUserDaemon.exe^
/IM RAVBg64.exe^
/IM nwtray.exe^
/IM RtkNGUI64.exe^
/IM BingSvc.exe^
/IM Spotify.exe^
/IM SpotifyWebHelper.exe^
/IM FacebookGameroom.exe^
/IM "Facebook Gameroom Browser.exe"^
/IM zapp.exe^
/IM steamwebhelper.exe^
/IM Steam.exe^
/IM lync.exe
EXIT /B
:everything
echo|set /p=username#email.com|clip
TASKKILL /F /T^
/IM Greenshot.exe^
/IM iprntctl.exe^
/IM iprntlgn.exe^
/IM smax4pnp.exe^
/IM ZenNotifyIcon.exe^
/IM ZenUserDaemon.exe^
/IM RAVBg64.exe^
/IM nwtray.exe^
/IM RtkNGUI64.exe^
/IM BingSvc.exe^
/IM Spotify.exe^
/IM SpotifyWebHelper.exe^
/IM FacebookGameroom.exe^
/IM "Facebook Gameroom Browser.exe"^
/IM zapp.exe^
/IM steamwebhelper.exe^
/IM Steam.exe^
/IM lync.exe
EXIT /B
:newwindow
CD /D "C:\Program Files (x86)\Mozilla Firefox"
firefox.exe^
"https://www.outlook.com/"^
"http://everybodyedits.com/"^
"http://www.kongregate.com/accounts/username/recently-played"
EXIT /B
:newtab
CD /D "C:\Program Files (x86)\Mozilla Firefox"
firefox.exe^
-new-tab "https://www.outlook.com/"^
-new-tab "http://everybodyedits.com/"^
-new-tab "http://www.kongregate.com/accounts/username/recently-played"
EXIT /B
Remove the VAR cliportaskkill and startffox from the code and a litte bug where pressing t to taskkill only after it finish the code it go to the next errorlevel and call the :cliponly code before it move on to the next choice command (this where I put an & goto :label to stop this bug but may break this BAT file when put into one line.)
P.S. In the near future I am thinking I could put the cliportaskkill and startffox VAR back in so that I can use it for parallel codes or two instances without waiting for the other codes to finish (without doing in another BAT files as well.)
I'ev set echo to on and here the output of the codes.
>setlocal
>CHOICE /C ectfx /m "e do everything, c clip only, t taskkill only, f skip this, x close this"
e do everything, c clip only, t taskkill only, f skip this, x close this [E,C,T,F,X]?T
>If errorlevel 5 EXIT /B
>If errorlevel 4 goto :choiceff
>If errorlevel 3 call :taskkillonly
>TASKKILL /F /T /IM Greenshot.exe /IM iprntctl.exe /IM iprntlgn.exe /IM smax4pnp.exe /IM ZenNotifyIcon.ex
e /IM ZenUserDaemon.exe /IM RAVBg64.exe /IM nwtray.exe /IM RtkNGUI64.exe /IM BingSvc.exe /IM Spotify.exe /IM SpotifyWebHelper.exe /IM F
acebookGameroom.exe /IM "Facebook Gameroom Browser.exe" /IM zapp.exe /IM steamwebhelper.exe /IM Steam.exe /IM lync.exe
ERROR: The process "Greenshot.exe" not found.
ERROR: The process "iprntctl.exe" not found.
ERROR: The process "iprntlgn.exe" not found.
ERROR: The process "smax4pnp.exe" not found.
ERROR: The process "ZenNotifyIcon.exe" not found.
ERROR: The process "ZenUserDaemon.exe" not found.
ERROR: The process "RAVBg64.exe" not found.
ERROR: The process "nwtray.exe" not found.
ERROR: The process "RtkNGUI64.exe" not found.
ERROR: The process "BingSvc.exe" not found.
ERROR: The process "Spotify.exe" not found.
ERROR: The process "SpotifyWebHelper.exe" not found.
ERROR: The process "FacebookGameroom.exe" not found.
ERROR: The process "Facebook Gameroom Browser.exe" not found.
ERROR: The process "zapp.exe" not found.
ERROR: The process "steamwebhelper.exe" not found.
ERROR: The process "Steam.exe" not found.
ERROR: The process "lync.exe" not found.
>EXIT /B
>If errorlevel 2 call :cliponly
>echo | set /p=username#email.com | clip
>EXIT /B
>If errorlevel 1 call :everything
>CHOICE /C wtx /m "w open new window, t open new tab, x close this"
w open new window, t open new tab, x close this [W,T,X]?
As you can see after the taskkill block it go to the next block which is the clip code before going to the next CHOICE commmand (for some reason it does not go to the next block of codes.)
I've press T for this one
>If errorlevel 3 EXIT /B
>If errorlevel 2 call :newtab
>CD /D "C:\Program Files (x86)\Mozilla Firefox"
>firefox.exe -new-tab "https://www.outlook.com/" -new-tab "http://everybodyedits.com/" -new-tab "
http://www.kongregate.com/accounts/username/recently-played"
>ECHO newtab!
newtab!
>pause
Press any key to continue . . .
>EXIT /B
>If errorlevel 1 call :newwindow
>CD /D "C:\Program Files (x86)\Mozilla Firefox"
>firefox.exe "https://www.outlook.com/" "http://everybodyedits.com/" "http://www.kongregate.com/a
ccounts/username/recently-played"
>ECHO newwindow!
newwindow!
>pause
Press any key to continue . . .
I've add an echo and pause to test if it do the same for this CHOICE command and it start the next block of code after it finished the the previous code. I've tested this in a test file where I used some CHOICE, If errorlevel, calling and goto commands and same bugs happened in the test file.

Related

Batch script - checking and killing process

I want write batch script:
Open cmd.exe and then run command which run minimalized Matlab (MATLAB.exe) and then matlab app (MATLABWindow.exe)
After that it must check if Matlab app is working
if yes, do nothing (still checking)
if not, it will kill minimalized MATLAB.exe
What I wrote but didnt work except running matlab and matlab app:
#ECHO OFF
START cmd.exe /min /k "matlab -nosplash -nodesktop -minimize -r "matlab.apputil.run('ThorlabsLC100APP')""
taskkill /F /IM cmd.exe
SLEEP 30
:search
TASKLIST|FIND "MATLABWindow.exe"
IF %ERRORLEVEL% equ 0 (
GOTO search)
ELSE (GOTO found)
TIMEOUT /T 5
GOTO search
:found
taskkill /im MATLAB.exe
EXIT
Thanks
Solved:
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
#ECHO OFF
START /min cmd.exe /k "matlab -nosplash -nodesktop -minimize -r "matlab.apputil.run('ThorlabsLC100APP')""
timeout /t 30 /nobreak
:loop
tasklist /FI "IMAGENAME eq MATLABWindow.exe" | findstr "MATLABWindow.exe" >nul
if %ERRORLEVEL% == 1 goto mycode
goto loop
:mycode
taskkill /F /IM MATLAB.exe
taskkill /F /IM cmd.exe
end

CMD prompt - bat file - Taskkill command ignores start /wait

Here's my script in my bat file:
cd "C:\Program Files (x86)\Steam\Steamapps\common\F13game\"
Start F13theGameRemap.exe
cd "C:\Program Files (x86)\Steam\steamapps\common\F13Game\SummerCamp\Binaries\Win64\"
Start /WAIT SummerCamp.exe
Start /WAIT Taskkill /f /im F13theGameRemap.exe
exit
All I want is to run the first program, then the 2nd. When the 2nd program "summercamp.exe" exits, i want the first program "F13thegameremap.exe" to exit.
The way this script works now is that Taskkill immediately ignores the start /wait command of summercamp.exe and closes F13theGameRemap.exe immediately.
How do I fix this?
Something like this:
#Echo off
Cd /D "C:\Program Files (x86)\Steam\Steamapps\common\F13game\"
Start F13theGameRemap.exe
Cd /D "C:\Program Files (x86)\Steam\steamapps\common\F13Game\SummerCamp\Binaries\Win64\"
Set "App2=SummerCamp.exe"
Start /WAIT %App2%
:loop
Timeout /t 1 >NUL
tasklist.exe /FI "ImageName eq %App2%" /NH |find /i "%App2%" >NUL && Goto :loop
Start /WAIT Taskkill /f /im F13theGameRemap.exe
The :loop will check with 1 second delay if %App2% aka SummerCamp.exe is still running.

How to set RANDOM in a batch file?

So i wrote this batch file.
I need to use some kind of random function to choose between 9 .exe fles.
One of those .exe files is "OPTION1.exe", so i'd like the batch to select from "OPTION2.exe, OPTION3.exe..." etc, which are all located in the same folder as the first one.
I'd like to know if it's possible and how to do it.
Thanks for your time.
#echo off
:loop
TASKKILL /F /IM "Software.exe"
TASKKILL /F /IM "Chrome.exe"
TIMEOUT /T 5
cd C:\Users\admin\Documents\Software
start Software
TIMEOUT /T 15
start OPTION1.exe
TIMEOUT /T 10
start connect.exe
TIMEOUT /T 15
cd C:\Program Files (x86)\Google\Chrome\Application
start Chrome.exe
TIMEOUT /T 400
TASKKILL /F /IM "Chrome.exe"
TIMEOUT /T 10
cd C:\Users\admin\Documents\Software
start disconnect.exe
TIMEOUT /T 15
cls
GOTO loop
set /a executable=(%random% %% 9) + 1
set "executablename=OPTION%executable%.exe"
echo %executablename%
should show you a random executable name OPTION1.exe .. OPTION9.exe
Well there are a few improvements that you can make to the code you provided. Here is the modified code:
echo off
cls
:loop
TASKKILL /F /IM Software.exe
TASKKILL /F /IM Chrome.exe
TIMEOUT /T 5
cd C:\Users\admin\Documents\Software
start Software.exe
TIMEOUT /T 15
start OPTION1.exe
start OPTION2.exe
start OPTION3.exe
TIMEOUT /T 10
start connect.exe
TIMEOUT /T 15
cd C:\Program Files (x86)\Google\Chrome\Application
start Chrome.exe
TIMEOUT /T 400
TASKKILL /F /IM Chrome.exe
TIMEOUT /T 10
cd C:\Users\admin\Documents\Software
start disconnect.exe
TIMEOUT /T 15
cls
GOTO :loop

Killing two process automatically after closing one program manually

I have a batch file that starts two programs :
#echo off
start "" "C:\Program Files (x86)\Kodi\Kodi.exe"
start "" "C:\Program Files\OpenVPN\bin\openvpn-gui.lnk"
Now when i close program "Kodi", i want that it close automatically first the task "openvpn-gui.exe" and then "openvpn.exe"
Thank you !
What about this:
start "" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe"
start "" "C:\Program Files\OpenVPN\bin\openvpn.exe"
start "" /WAIT "C:\Program Files (x86)\Kodi\Kodi.exe"
taskkill /IM "openvpn.exe"
taskkill /IM "openvpn-gui.exe"
Not tested, but give a try :
#echo off
Title Killing two process automatically after closing one program manually
set "RunningProcess=Kodi.exe"
set "Process2Kill=openvpn-gui.exe openvpn.exe"
tasklist /fi "imagename eq %RunningProcess%" /nh |find /i /c "%RunningProcess%"|findstr "^1$" >nul
If "%Errorlevel%" EQU "1" (
For %%a in (%Process2Kill%) do Call :KillProcess "%%a"
) else (
echo %RunningProcess% is still running
)
pause & exit
:KillProcess
Taskkill /f /im "%~1">nul 2>&1
goto :eof

Killing a process from a new command prompt in a batch file

I have this sitting in a batch file, and I'm wondering why it's throwing
"The system cannot find the file TASKLIST"
FOR /F "usebackq tokens=2 skip=2" %%i IN (TASKLIST /FI "IMAGENAME eq explorer.exe") DO taskkill /F /PID %%i
More importantly, the problem is that that command - killing explorer.exe - has to run in the cmd spawned by the command
at xx:xx /interactive "cmd.exe"
How would I pipe that taskkill command into the new command prompt that would be spawned by the at command?
Thank you for your time.
if you want to process a exit of a command use single qutoes or back quotes with "backq" parameter:
FOR /F "tokens=2 skip=2" %%i IN ('TASKLIST /FI ^"IMAGENAME eq explorer.exe^"') DO taskkill /F /PID %%i
(im not sure if your backquotes are displayed because they meta-symbol for code block in stackoverflow)
Taskkill also can kill a process by image name
taskkill /im explorer* /f

Resources