Enter empty string as parameter from DOS command prompt?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

Enter empty string as parameter from DOS command prompt?

#1 Post by Clueless in Seattle » 22 Dec 2013 09:28

I'm still running MS-DOS 6.21 and have a lot of fun writing batch files for it.

I'm wondering if there's a way to enter an empty string as a parameter when running a batch file.

The batch file would be run from a macro executed by my text editor.

And the parameter would sometimes contain text, and sometimes be empty.

What I'm trying to figure out is how to preserve the order of the parameters if one of them in the middle of the sequence of parameters should turn up empty. So I guess you could say what I'm looking for is a way to pass a "place holder" parameter to a batch file in cases where that parameter string comes up empty.

Here's a simplified example: Suppose the three parameters are:

%1 DIRECTORY_NAME
%2 [empty string]*
%3 FILE_NAME

Is there a way to write a command line to run the batch file that would pass parameter %2 to the batch file as an empty string?

Will in Seattle
a.k.a. "Clueless"

*In case you're wondering, parameter %2 is used for the name of a sub-directory (if any). Sometimes there will be one, and sometimes not.

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Enter empty string as parameter from DOS command prompt?

#2 Post by aGerman » 22 Dec 2013 10:31

I fear you can't pass an empty string but you could check if %3 is present.

Code: Select all

IF _%3==_ (your command here)

... where you should assume that %2 contains the file name.

Regards
aGerman

EDIT:
Maybe this could work for you

Code: Select all

FOR %%I IN (DIRECTORY_NAME SUBDIR_NAME FILE_NAME) DO SET %%I=
SET DIRECTORY_NAME=%1
SET FILE_NAME=%3
IF DEFINED FILE_NAME (SET SUBDIR_NAME=%2) ELSE SET FILE_NAME=%2

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Enter empty string as parameter from DOS command prompt?

#3 Post by Squashman » 22 Dec 2013 11:04

Pass it as an empty quoted string

Code: Select all

@echo off
echo parm1 is %~1
echo parm2 is %~2
echo parm3 is %~3
pause

Code: Select all

C:\Batch>parms.bat "Foo1" "" "Foo3"
parm1 is Foo1
parm2 is
parm3 is Foo3

Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

Re: Enter empty string as parameter from DOS command prompt?

#4 Post by Clueless in Seattle » 22 Dec 2013 12:15

Squashman wrote:Pass it as an empty quoted string

Code: Select all

C:\Batch>parms.bat "Foo1" "" "Foo3"


Thanks, Squashman.

So, does that mean that all the other parameters have to be enclosed in quotes as well, for this to work?

Will in Seattle
a.k.a. "Clueless"

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Enter empty string as parameter from DOS command prompt?

#5 Post by Ed Dyreen » 22 Dec 2013 12:52

Clueless in Seattle wrote:
Squashman wrote:Pass it as an empty quoted string

Code: Select all

C:\Batch>parms.bat "Foo1" "" "Foo3"


Thanks, Squashman.

So, does that mean that all the other parameters have to be enclosed in quotes as well, for this to work?

Will in Seattle
a.k.a. "Clueless"
The parameters don't have to be enclosed in quotes, but if an empty argument isn't enclosed in quotes, there is no argument at all.
Spaces and tabs are delimiters thus not valid arguments. This is why an empty argument must be quoted so the receiver has a way of knowing the argument has value empty.

Computer languages and programs often use;

Code: Select all

myProgram /argName: value /argName: "v a l u e" /argName: "" /aSwitch /anotherSwitch
which is bulletproof.

Post Reply