I suggest this code:
@echo off
rem Is there specified more than one argument?
if not "%~2" == "" goto ErrorArgCount
rem Is there specified as second argument just " or just ""?
set SecondArgument=%2
if not defined SecondArgumentgoto CheckFirstArg
set "SecondArgument="
:ErrorArgCount
echo ERROR: Too many parameters used on calling "%~nx0".
echo/
pause
goto :EOF
:CheckFirstArg
setlocal EnableExtensions DisableDelayedExpansion
if not "%~1" == "" set "FileName=%~1" & goto ProcessFile
:Begin
set "FileName="
set /P "FileName=File name: "
rem Has the user entered anything at all?
if not defined FileName goto Begin
rem Remove all double quotes from file name.
set "FileName=%FileName:"=%"
rem Has the user entered anything else than double quotes?
if not defined FileName goto Begin
:ProcessFile
echo Continue "%~nx0" with file "%FileName%" ...
echo/
pause
endlocal
The batch file first checks if it was called with more than one argument. The first IF condition is for typical arguments like Argument2
or "Argument 2"
.
But a user could run the batch file also with the arguments list "File Name" "" "third argument"
or with FileName "
on which first IF condition removing surrounding double quotes from second argument fails to detect the empty second argument string. For that reason the second argument string is assigned to an environment variable which is not defined anymore if there is really no second argument string. Otherwise environment variable SecondArgument
is defined with string value "
or ""
which results in second IF condition being false and therefore running into error code for too many arguments.
Beginners in batch file writing should read carefully How does the Windows Command Interpreter (CMD.EXE) parse scripts?
It is most important to know how cmd.exe
processes environment variable references using syntax %variable%
, especially on using immediately expanded environment variables within a command block starting with (
and ending with )
. Such a command block is parsed by Windows command processor completely with replacing all %variable%
variable references by current values of the referenced environment variables before the command block is used at all. Command blocks are used usually with the commands IF, ELSE and FOR. Every environment variable defined or modified inside a command block must be referenced inside the command block using delayed expansion or the code is not working as expected by the writer of the batch file. The help/documentation output on running in a command prompt window set /?
explains when and how to use delayed environment variable expansion on an IF and a FOR example.
It is good practice on batch file writing to avoid command blocks where it is easy possible by writing the code in a simple top to down manner with execution branches done using command GOTO.
There is the command START as it can be seen on running in a command prompt window help
or help start
or start /?
. For that reason it is not good to use as label the string start
. It is possible, but it is not advisable as it makes it difficult to search for start
being interpreted as command in comparison to start
being used as label. For that reason the code above uses Begin
as label which is not a Windows command as documented by Microsoft on page Windows Commands as well as and even better by SS64 on SS64.com - A-Z index of the Windows CMD command line.
It is necessary to take care on prompting a user for an input with set /P
because the user has the freedom to enter nothing at all which means the environment variable is still not defined and not being defined before or keeps its value on being already defined. The user can also enter by mistake or intentionally a string which could result in executing something for which the batch file is not written at all or Windows command processor exits batch file processing because of a syntax error caused by user input and not good written batch file code. Please read my answer on very detailed How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
See also single line with multiple commands using Windows batch file for an explanation of operator &
used in batch code above to execute two commands specified on one command line.