First, help on every command can be get by running in a command prompt window the command with /?
as parameter. start /?
outputs the help of command START. call /?
outputs the help of command CALL usually used to run a batch file from within a batch file. Those two commands can be used to run a batch file as explained in detail in answer on How to call a batch file that is one level up from the current directory?
Second, the command line
start "C:\Users\%USERNAME%\Desktop\Check.bat"
starts a new command process in foreground with a console window with full qualified batch file name as window title displayed in title bar at top of the console window. That is obviously not wanted by you.
Third, the Wikipedia article Windows Environment Variables lists the predefined environment variables on Windows and their default values depending on version of Windows.
In general it is better to use "%USERPROFILE%\Desktop"
instead of "C:\Users\%USERNAME%\Desktop"
.
There is no C:\Users
on Windows prior Windows Vista and Windows Server 2008 by default at all.
The users profile directory can be on a different drive than drive C:
.
It is also possible that just the current user's profile directory is not in C:\Users
, for example on a Windows server on which many users can logon directly and for which the server administrator decided to have the users' profile directories on a different drive than system drive making backup and cleaning operations on server much easier and is also better for security.
Well, it is also possible to have the user's desktop folder not in the user's profile directory. But that is really, really uncommon.
Fourth, on shipping a set of batch files, it is recommended to use %~dp0
to call other batch files from within a batch file because of this string referencing drive and path of argument 0 expands to full path of currently executed batch file.
The batch file path referenced with %~dp0
always ends with a backslash. Therefore concatenate %~dp0
always without an additional backslash with another batch file name, folder or file name.
See also What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?
Fifth, I suggest following for your two batch files:
Main.bat:
@echo off
color 1e
title ------ Just a Test ------
call "%~dp0Check.bat" || color && exit /B
echo Welcome to the Game!
Check.bat:
@echo off
cls
if exist "%~dp0Batch_System\importantFile.dll" exit /B 0
echo ERROR:
echo Important file not found. Please reinstall the program.
echo/
pause
exit /B 1
The batch file Check.bat
is exited explicitly on important file existing with returning exit code 0
to the parent batch file Main.bat
. For that reason Windows command processor continues execution of Main.bat
on the command line below the command line calling the batch file Check.bat
.
Otherwise Check.bat
outputs an error message, waits for a pressed key by the user and exits explicitly with non zero exit code 1
. The non zero exit code results in Main.bat
in executing the next command after ||
which is COLOR to restore initial colors and next executing also EXIT with option /B
to exit the execution of Main.bat
.
See also:
The CALL command line in Main.bat
could be also written as:
call "%~dp0Check.bat" || ( color & exit /B )
And Main.bat
could be also written as:
@echo off
color 1e
title ------ Just a Test ------
call "%~dp0Check.bat"
if errorlevel 1 (
color
goto :EOF
)
echo Welcome to the Game!
I do not recommend using in Main.bat
just EXIT instead of exit /B
or goto :EOF
. Just EXIT would result in exiting the current command process independent on calling hierarchy and independent on how the command process was started: with option /K
to keep it running to see error messages like on opening a command prompt window and next running a batch file from within command prompt window, or with /C
to close the command process after application/command/script execution finished like on double clicking on a batch file.
It is advisable to test batch files by running them from within an opened command prompt window instead of double clicking on them to see error messages on syntax errors output by cmd.exe
. For that reason usage of just EXIT is counter-productive for a batch file in development. Run cmd /?
in a command prompt window for help on Windows command processor itself.
Last but not least see: