Computing.Net > Forums > Programming > Testing command line arguments

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Testing command line arguments

Reply to Message Icon

Name: biggandyy
Date: May 15, 2009 at 21:21:53 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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:

SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS
:processcmdline
::  
:: 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
:getwallport
::  
:: This input collects the wall port 
::  
	set choice=
	set /p choice=Enter the Port Number   
	set wallport=%choice%
	ECHO Wall Port:                 %wallport% >>holding.txt
:getbuilding
::  
:: This input collects the building info 
::  
	set choice=
	set /p choice=Enter the building name 
	set building=%choice%
	ECHO Building Name:             %building% >>holding.txt
:getroomnum
::  
:: 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



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: May 15, 2009 at 22:06:58 Pacific
Reply:

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


0

Response Number 2
Name: biggandyy
Date: May 16, 2009 at 10:56:31 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Testing command line arguments

Command line arguments in VisualC++ www.computing.net/answers/programming/command-line-arguments-in-visualc/6012.html

command line argument in C ?? www.computing.net/answers/programming/command-line-argument-in-c-/4446.html

C++ Command-Line Arguments www.computing.net/answers/programming/c-commandline-arguments/4864.html