I'm trying to make a simple batch file that will let me pick from a list of programs and run them based on my choice. For reference this is what I have so far:
#echo off
:menu
echo 1. zsnes
echo 2. Project64
echo 3. MAME
echo 4. PCSX2
echo 5. VBA
echo 6. DOSBox
set /p emu=Pick your emulator [1-6]:
if %emu%=1 goto zsnes
if %emu%=2 goto project64
if %emu%=3 goto mame
if %emu%=4 goto pcsx2
if %emu%=5 goto vba
if %emu%=6 goto dosbox
:zsnes
start /d "C:\Users\*username*\Documents\zsnes\" zsnesw.exe
I've just typed out through the zsnes program to test it. The command prompt launches and will ask for me to pick a choice. When I select 1, cmd.exe instantly closes but the program is not run. I made a script that contained only the start line and it worked fine. When I open cmd.exe manually and type that line in it also works fine. It just doesn't work in the context of my script. What could be causing this? Any advice would be greatly appreciated!
because your if is not correct .
Try with
if %emu% == 1 goto zsnes
or
if %emu% equ 1 goto zsnes
and so on.
Some syntax errors in IF and FOR commands leads force exist of the script.
probably you'll want also
:zsnes
start /d "C:\Users\*username*\Documents\zsnes\" zsnesw.exe
goto :eof
to avoid executuion of the code under the other labels.
write the following line at the end of the code or where you want to stop / pause your cmd.exe
timeout \t -1
this will pause your cmd screen and give you an option to close it by asking Yes/No ?.
Exactly like the one before me said, "if" should be:
if %emu% == 1 goto zsnes
Additionally, add "pause" after your batch, so you can read the error-messages when something fails.
Related
Windows batch file, I want the window close itself after user hit a key.
For example
#echo off
echo ---------------------
echo hit a key will close
echo ---------------------
pause
exit /b
but it won't close
You dont need to add echo hit a key will close; basically the pause
command already says that for you.
But if you want to add your own custom message you can add
pause>nul
and add an echo above that for a custom exit message,
then type
exit
after the pause >nul.
In the RPG I'm working on, I use a quitgame.bat as I also need to close other processes when the batch exits.
CHOICE is the command I use for most input, and I present the option to 'Q'uit at all times (single keypress to exit the game). when the user selects this Choice, the errorlevel check has the Batch go to a label to Call my quitgame.bat
Set ask=CHOICE /N /C:
Set then= /M ""
Set do=IF ERRORLEVEL ==
%ask%nqfmie%then%
%do%6 GOTO enterSL
%do%5 GOTO viewinv
%do%4 GOTO market
%do%3 GOTO forcereset
%do%2 GOTO quit
%do%1 GOTO nextpage
:quit
CALL "%Quitgame%"
In my quitprogram:
#ECHO OFF
REM program to taskkill vbs scripts and child processes
CALL "%killmusic%"
REM deletes all vbs scripts created during programs execution
DEL /Q %sounds%\*.vbs
REM closes the command console
EXIT
If you dont have other processes or multiple bat files to warrant calling another program, just use a label you can goto using choice.
:quit
Exit
I'd like a batch that will check if the process firefox.exe exists (after it has been started by the start command).
If the process exists, it will go to the label :fullscreen,
else the batch will go the the label :timeout. Then, it will check again if the process firefox.exe exists and if not, it will go again to the label :fullscreen until the process exists.
Here is my batch:
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
:timeout
timeout /t 5
:fullscreen
nircmd sendkeypress F11
exit
How can I do this check ?
You can also use QUERY PROCESS:
#Echo Off
If Not Exist "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe" Exit/B
Start "" "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe"
:Loop
Timeout 5 /NoBreak>Nul
QProcess firefox.exe>Nul 2>&1||GoTo :Loop
NirCmd SendKeyPress F11
I suggest for this task the batch file:
#echo off
start "" /max firefox.exe
if errorlevel 1 goto :EOF
set LoopCount=0
:WaitLoop
%SystemRoot%\System32\timeout.exe /T 5
%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq firefox.exe" 2>nul | %SystemRoot%\System32\find.exe /I "firefox.exe" >nul
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
set /A LoopCount+=1
if not %LoopCount% == 6 goto WaitLoop
Let me explain the few command lines used here.
1. Starting Firefox
The command START being an internal command of cmd.exe interprets the first double quoted string as optional title for the console window. Therefore the command line
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
results just in opening a new console window with the window title:
C:\Program Files (x86)\Mozilla Firefox\firefox.exe
For that reason "" is specified as first START argument to define an empty title. Firefox is a GUI application. So no console window is opened which means an empty window title is really enough.
The parameter /max would not be really necessary, but the goal is to get Firefox into full screen mode after starting. So why not starting it already maximized?
32-bit version of Firefox is by default installed in directory %ProgramFiles% on 32-bit Windows and in %ProgramFiles(x86)% on 64-bit Windows. But it is possible during the installation to install Firefox into any other folder. But Firefox installer is well coded and registers firefox.exe in Windows registry under key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
This is recommended by Microsoft as written in MSDN article Application Registration.
The command START searches also in Windows registry under this key for an executable specified as argument without path as explained in answer on Where is “START” searching for executables?
This is the reason for using just firefox.exe on START command line because that starts an installed Firefox independent on installation location.
START displays an appropriate message box if firefox.exe could not be started and exits in this case with a return code greater 0 (9059 in my test on one computer).
The help output on running if /? in a command prompt window explains how to evaluate the exit code of a previous command or application without usage of immediate or delayed environment variable expansion and therefore working anywhere in a batch file from MS-DOS (really!) to currently latest Windows 10.
The command line if errorlevel 1 goto :EOF means IF start failed to start firefox.exe indicated by an exit code greater or equal 1 THEN exit execution of this batch file. For details on exiting batch file execution see answer on Where does GOTO :EOF return to?
2. Checking for running Firefox
The command TASKLIST being an external command, i.e. a console application in system directory of Windows, outputs a list of running processes. This list can be already filtered by TASKLIST itself for a specific process as done in batch file with /FI "IMAGENAME eq firefox.exe".
But TASKLIST is designed for just printing a list of processes. It is not designed for checking if a specific process is running and returning the result to the calling process via exit code. TASKLIST always exits with 0.
But an error message is output to handle STDERR on using a filter and no process can be found in process list matching this filter. For that reason 2>nul is used to suppress this error message by redirecting it to device NUL. Read the Microsoft article about Using Command Redirection Operators for more information about redirection.
A simple method to get a simple false/true respectively 0/1 result on checking for running Firefox is filtering output of TASKLIST with external command FIND which exits with 0 if the string to find was indeed found or with 1 if the searched string could not be found in the text read in this case from STDIN. The output of FIND is of no interest and therefore suppressed with redirection to device NUL using >nul.
Instead of using TASKLIST and FIND it is also possible to use QPROCESS:
%SystemRoot%\System32\qprocess.exe firefox.exe >nul 2>&1
QPROCESS exits with exit code 1 if firefox.exe could not be found in list of running processes. Otherwise the exit code is 0 on firefox.exe is running.
3. Evaluating Firefox process checking result
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
The IF command checks if exit code of FIND is NOT greater or equal 1 which means if exit code is lower than 1. Command FIND exits never with a negative value. So if this condition is true then it is time to execute nircmd.exe to send key press F11 to application in foreground hopefully being Firefox (not guaranteed by this code) and exit batch file processing.
Otherwise the batch file should wait once again 5 seconds and then do the check again. This can very easily result in an endless running batch file in case of started Firefox is immediately closed by the user before the 5 seconds wait timed out. For that reason it is counted how often the wait loop is already executed. After 6 loop runs, or 30 seconds, it is really time to no longer wait for Firefox and exit the batch file.
4. Getting more information about used commands
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
find /?
if /?
qprocess /?
set /?
start /?
tasklist /?
timeout /?
And Single line with multiple commands using Windows batch file should be also read explaining operator & in addition to all other web pages referenced already above.
You can show a list of opened programs like this:
tasklist
To check if firefox exists:
EDIT: Code edited to show a fully working example
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
goto :checkloop
:checkloop
tasklist|find "firefox.exe" > NUL
if %ERRORLEVEL% == 0 (
call :fullscreen
exit
) else (
call :timeout
goto :checkloop
)
:fullscreen
nircmd sendkeypress F11
goto :EOF
:timeout
timeout /t 5
goto :EOF
So I have a batch file that I am trying to convert but i'm no success. The converter that I am using is
Bat To Exe Converter. The problem that I am encountering is that after converting the batch file it does not execute properly and immediately says "Press any key to continue . . ." and then closes. The batch file works fine on its own and when I converted it using the websites online converter it also worked (I would use the online but has little functions and is not exactly what I need).
Below is the batch code that I am using:
#ECHO OFF
TITLE ADB Over Network Running...
COLOR 17
CLS
IF "%ANDROID_PLATFORM_TOOLS%" == "" GOTO NOPATH
ADB tcpip 5555
IF ERRORLEVEL 1 GOTO END
IF ERRORLEVEL 0 GOTO NEXT
GOTO END
:NEXT
set /P ip=Enter Devices IP: %=%
ADB connect %ip%
GOTO END
:NOPATH
ECHO "ANDROID_PLATFORM_TOOLS" not found. Please add this environment variable
GOTO END
:END
PAUSE
EXIT
I hope that you can help me. Thank you for any help and your time :D
As all that program does is extract the batch file into a subfolder of temp and execute it, Windows has the exact same feature.
Type
iexpress
in Start - Run and follow the wizard and set your bat to run as the last step.
So for starters, I'm kind of a scrub coder and I'm enjoying playing around with batch scripting.
I don't like to have multiple command line windows open, so I'm trying to script everything into this 1 batch file, including the ability to run standard command line commands.
Here's the section of my code that I'm having issues with.
:three
echo.
echo ***************************
echo **Enhanced Command Prompt**
echo ***************************
:three.b
echo.
set /p Return=Enter Command:
if '%Return%'=='Return' goto home
cmd /c %Return%
goto three.b
NOTE: I've also removed cmd /c, and %Return% excutes with no issue, but for the sake personal preference, I've kept the cmd /c present.
The issue with this line of text, is executing a command such as "Ping Google.com" crashes the window. So does ipconfig /all, and anything else that doesn't seem to execute right away.
If I remove the "IF=Return", then I have no problems running ping google.com or anything else, but I'm ultimately trapped in this section of code without that IF statement.
The IF statement itself functions, but "Ping X" and other commands do not without crashing the batch file.
Any help would be appreciated.
Thanks!
You will need to use double quotes in if as this is the proper way of quoting/comparing strings in batch file.
:three
echo.
echo ***************************
echo **Enhanced Command Prompt**
echo ***************************
:three.b
echo.
set /p Return=Enter Command:
if "%Return%" == "Return" goto home
cmd /c %Return%
goto three.b
The last line in my batch file is pause. Is there any way to add a if condition to see if the script is run within command prompt or by double clicking to execute? I want to skip pause if it's running in command prompt.
...
...
if not RUN_IN_COMMAND_PROMPT (
pause
)
EDIT:
Hope to find a solution works in Windows Server 2003/2008, WinXP, Win7.
CALL :GETMYSWITCH %CMDCMDLINE%
IF /I "%MYSWITCH%" == "/C" ECHO I WAS STARTED IN THE EXPLORER & PAUSE
IF /I NOT "%MYSWITCH%" == "/C" ECHO I WAS STARTED IN A DOS SESSION
:GETMYSWITCH
SET MYSWITCH=%2
I know this is a year later but for future people searching you can use
If /I "%COMSPEC%" == %CMDCMDLINE% Goto SkipPause
pause
:SkipPause
It will skip the pause block if running from the command line and pause if running from batch file.
By definition, a shell script is always going to be run in a "command prompt". But try using the SESSIONNAME env var - it seems to NOT be present if the script was started by double-clicking instead of manually running it from a prompt.
Use the tty command.
Use the -s option and check the return value.