Testing command line arguments

May 15, 2009 at 21:21:53
Specs: Windows XP
A follow up to my question about extracting delimiters from a variable... I am afraid it is a bit more basic but I am continually getting either syntax errors or logic errors (i.e. the code does not evaluate the logical operations properly). Here is the code with the syntax error:
:: Checks to see if the wall port was entered 
:: as a command line arguments and appends   
:: it to the OUTPUT file if they exist     
	ECHO OFF >holding.txt
	IF NOT DEFINED %1 GOTO :getwallport
	ECHO Wall Port:                 %1 >>holding.txt
	IF NOT DEFINED %2 GOTO :getbuilding
	ECHO Building Name:             %2 >>holding.txt
	IF NOT DEFINED %3 GOTO :getroomnum
	ECHO Room Number:               %3 >>holding.txt
	Exit /B 0
:: This input collects the wall port 
	set choice=
	set /p choice=Enter the Port Number   
	set wallport=%choice%
	ECHO Wall Port:                 %wallport% >>holding.txt
:: This input collects the building info 
	set choice=
	set /p choice=Enter the building name 
	set building=%choice%
	ECHO Building Name:             %building% >>holding.txt
:: This input collects the room number
	set choice=
	set /p choice=Enter the room number   
	set roomnum=%choice%
	ECHO Room Number:               %roomnum% >>holding.txt
	Exit /B 0

It is supposed to be simple... if there are existing command line variables (%1 %2 %3) then output those to a text file. If they do not exist then ask the user for them with the SET /P command.

The SET /P portion works great, it is the IF statement initially that is driving me nuts.

Thanks for any guidance. -BA

See More: Testing command line arguments

Report •

May 15, 2009 at 22:06:58
If not defined is testing it the contents of %1 is a defined variable, not it %1 is defined.

Something like this is probably what your after.

if "%~1"=="" GOTO :getwallport

Report •

May 16, 2009 at 10:56:31
That did not do it but it did fix my syntax problems AND it lead me to figure out what the logic problem was. In the program I get to :processcmdline via a CALL and I was not passing the command line arguments to the CALL.

When I modified my statement to read

CALL :processcmdline %1 %2 %3
everything began to work automagically!

Thanks for the code, it cleaned up alot my code and lead me to the solution!

Report •

Related Solutions

Ask Question