2

I made a Main batch file with the lines below:

@echo off
color 1e
title  ------ Just a Test ------
start "C:\Users\%USERNAME%\Desktop\Check.bat"
:START
echo Welcome to the Game!
...

And Check.bat contains:

@echo off
if not exist "C:\Users\%USERNAME%\Desktop\Batch_System\importantFile.dll" goto ERROR
if exist "C:\Users\%USERNAME%\Desktop\Batch_System\importantFile.dll" goto CONTINUE
:ERROR
cls
echo        ERROR :
echo   Important file not found. please reinstall the program 
pause
exit /b
:CONTINUE
cls
exit /b

When I use the command start, it starts only a command prompt with the Check.bat directory and the main batch file continues executing the game. I want to force close the main batch file if importantFile.dll doesn't exist.

Okay, let me explain: When the main batch file is executed and runs the command start to start another batch file called Check.bat, the file Check.bat checks if the file importantFile.dll exists, and if not, Check.bat displays an error message.

Does anyone know how to write Check.bat in a manner that when the .dll file does not exist, force the main batch file to exit?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Sheep1Man
  • 31
  • 1
  • 8

3 Answers3

1

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:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

start is asynchronous by default. Use start /wait so that main.bat can test the exit code of check.bat. Make check.bat return an appropriate exit code.

For example...

main.bat

@echo off
start /b /wait check.bat
if not %errorlevel% == 0 exit /b
echo "Welcome to the game!"
...

check.bat

@echo off
if exist "importantfile.dll" exit 0
echo ERROR: Important file not found. Please reinstall the program.
pause
exit 1

notes

Added /b to start to avoid opening another window. Change that per your preference.

You could use call instead of start but call gives the called code access to the variables of main.bat so encapsulation is improved if you use start as you did.

The logic in check.bat is simplified above. Once you identify the success path early in the script and exit, the rest of the script can assume the fail path. This saves you a few if's and labels which you might find simplifies writing and reading of similar scripts. Beware of potentially confusing multiple exit points in longer scripts though!

When choosing exit codes, 0 is a common convention for success.

The above code is just one technique - there are several other options (such as checksomething && dosomethingifok). Some useful information on return codes, and checking them, can be found in http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html

simaos
  • 1
  • 1
  • Your code is right, but when i open the "check.bat" file it's working fine (Shows to me the error if the file doesn't exist), but when i execute the "main.bat" it's just shows me a CMD console with the directory of the "main.bat" and the color that was in his script (color 1e). – Sheep1Man Jun 02 '18 at 13:04
  • Thanks for sharing. The simple examples above are tested, exactly as they appear (except the "..." at the end of main.bat), by calling "main.bat" from the command line and having "main.bat", "test.bat" and "importantfile.dll" all in the same directory. If you are still seeing colours, are you calling "check.bat" from your original "main.bat"? Do you have the "start /wait /b" changes? If you post your full scripts, I would be happy to test them and help. Also, some great advice in the Mofi's answer although I prefer "start" to "call" for the reasons I mentioned in my answer. – simaos Jun 02 '18 at 15:38
0

Thanks to the answer from Mofi. I've my example and exp. on this. To be short, it's about the setting of log date format. You may change the format of time , date and log. you may have the result.

why-batch-file-run-with-failure-in-windows-server

Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
Sam Xiao
  • 21
  • 5