293

I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever %1 is surrounded by quotes such as the case when %1 is "c:\some path with spaces".

IF NOT %1 GOTO MyLabel // This is invalid syntax
IF "%1" == "" GOTO MyLabel // Works unless %1 has double quotes which fatally kills bat execution
IF %1 == GOTO MyLabel // Gives an unexpected GOTO error.

According to this site, these are the supported IF syntax types. So, I don't see a way to do it.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

UPDATE: on 2020-10-25, I updated the accepted answer from using brackets to using a tilde. Everyone says the tilde is better as it's more secure. I'm a little torn cause the tilde looks more complicated and is less clear as to what it's purpose is but nevertheless, I changed it.

blak3r
  • 16,066
  • 16
  • 78
  • 98
  • 2
    On my systems (Windows 2003 as well as Windows 7), `if "%1" == "" GOTO MyLabel` doesn't fatally kill the execution of the script as long as `%1` has an even number of double-quotes. I see that an odd number of double-quotes in `%1` kills the execution of the script with this error: `The syntax of the command is incorrect.` The solution below that uses square brackets to solve the problem has been marked as the correct answer but it doesn't seem to be doing any better. That solution also fails with the same error when `%1` has an odd number of double-quotes. – Susam Pal Jan 07 '13 at 14:31
  • @SusamPal Interesting. Try the parenthesis version under it and see if that works. That one I tested more. I just updated the accepted answer a couple days ago. – blak3r Jan 07 '13 at 18:25
  • [Dan Story's answer](http://stackoverflow.com/a/2541820/303363) seems to work fine indeed. I used the version using square brackets. – Susam Pal Jun 07 '14 at 05:04
  • a good "catch all" example: http://stackoverflow.com/questions/830565/how-do-i-check-that-a-parameter-is-defined-when-calling-a-batch-file/34552964#34552964 covering both file/directory and generic string/number mix in argument. –  Jan 01 '16 at 01:48
  • 1
    So frustrating -- `IF DEFINED` only working on environment variables instead of script variables is such a waste of potential! – kayleeFrye_onDeck Aug 16 '16 at 23:04
  • I think it's only possible to test the LAST argument. If you leave a middle arg empty, shell shifts the subsequent args to fill its spot. in this call: `CMD.bat arg1, , arg3` the following it true : `%2 == %arg3` – johny why Nov 23 '21 at 22:25
  • @kayleeFrye_onDeck Maybe if you use `SETX` then you can use `DEFINED`. Then they will be permanent vars. – johny why Nov 23 '21 at 22:27
  • It might work, but then you'd in most cases need to run a clean-up script to undefine all those variables that are meant to only be temporary and constrained to the script. `IF /i NOT "%var%"==""` is pretty much the only safe way to check if a script-var is defined. And that's so much more ugly than `IF DEFINED %var%` :( – kayleeFrye_onDeck Dec 01 '21 at 01:37

18 Answers18

334

Use square brackets instead of quotation marks:

IF [%1] == [] GOTO MyLabel

Parentheses are insecure: only use square brackets.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Dan Story
  • 9,985
  • 1
  • 23
  • 27
  • 14
    parenthesis aren't secure! Content like `&` in `%1`will break the line – jeb Dec 09 '11 at 16:45
  • 20
    Using other characters like `if #%1#==##` or `if [%1]==[]` is good so long as there are no spaces in the argument, otherwise you will get a `…was unexpected at this time` error. – Synetech Feb 04 '13 at 19:41
  • @kungfujam Spaces separate each argument, for example: test.bat = ( echo %1 test %2 ) - Execute test.bat in CMD with: "test hello world" - Output: "hello test world" - Arguments can go on forever, ex: %1 %2 %3, and so on. (I came across this question for my own use, and felt like replying to the above comment)... Enjoy... – NYCBilly May 04 '15 at 06:26
  • 3
    @NYCBilly Huh? If you run `test.bat "hello world"`, then `%1` will have spaces in it. (That doesn't matter if we just want to check if `%1` is empty, but kungufjam's point is that `if [%1] == [hello world]` would not work if `%1` is `"hello world"`. – jamesdlin May 31 '15 at 21:04
  • 20
    And using `"%~1"` is really a better approach, as I mentioned in my answer. – jamesdlin May 31 '15 at 21:08
  • 11
    Guys, @jamesdlin is correct. This approach will not work if the variable text has spaces or is `==`. Use his answer instead. – James Ko Apr 03 '16 at 15:36
  • 9
    You do not make it clear that `it DOES NOT work with spaces`. Use @jamesdlin answer instead. – JB. Jun 12 '17 at 11:56
  • I cant make this work with loop on all args `for %%A in (%*)` or if `if not %1 = []` – johny why Nov 23 '21 at 18:20
  • @jamesdlin in a loop on all args, the `~` doesn't help. Can't make it work. – johny why Nov 23 '21 at 18:32
  • The issue with spaces ONLY happens if %1 isn't blank. That's exactly what we're testing here: `Is %1 blank?` In other words, if we get an error due to blanks in %1, then WE KNOW %1 ISN'T EMPTY. Therefor, can we just code this to ignore the error with a double-pipe, and assume if we get the error, then we know %1 isn't empty. – johny why Nov 23 '21 at 18:42
  • @johnywhy No, we can't fix this with `==""`, because that will also return true if an empty string was passed. An empty string isn't an empty argument, it's a string. But i think the double-pipe has potential. – johny why Nov 23 '21 at 18:45
  • `IF [%1] == [] GOTO MyLabel || GOTO MyLabel` Doesn't this fix the issue with spaces? – johny why Nov 23 '21 at 18:46
234

You can use:

IF "%~1" == "" GOTO MyLabel

to strip the outer set of quotes. In general, this is a more reliable method than using square brackets because it will work even if the variable has spaces in it.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 12
    This is indeed a good way to do it. By using `~`, you strip the outer quotes if they are present, but then add them back manually (ensuring only one set). Moreover, you *have* to use quotes if the argument contains spaces (I learned this the hard way when my previous version of using `#` or brackets instead of quotes caused arguments with spaces to throw a `…was unexpected at this time` error.) – Synetech Feb 04 '13 at 19:39
  • 6
    is this also applicable for general variables like %MYVAR% rather than the arguments like %1 ? – simpleuser Aug 17 '17 at 19:52
  • 6
    @simpleuser Oops, you're right, it does not work with general environment variables. – jamesdlin Aug 17 '17 at 20:34
  • 2
    watch out for strings that use double '"' as an escape sequence -- the script will crash when attempting the comparison. E.g. `""this string will crash the comparison""`. Double '"' is the proper escape sequence, and doing a substitution on double '"' will fail if the string is empty. – Tydaeus Oct 26 '17 at 02:46
  • @Algoman It works fine with blocks. If it doesn't work for you, then you're probably doing something else wrong, and I suggest you post a separate question about it. – jamesdlin Mar 05 '19 at 16:10
  • That's good for a param but how do I do this on a variable name? – Alkanshel Jul 11 '19 at 00:03
  • 2
    @Amalgovinus You probably could pass the variable as an argument to a subroutine and let the subroutine use `"%~1"`. – jamesdlin Jul 11 '19 at 02:09
  • 2
    Should I change the accepted answer to this? Is this always better than [] – blak3r Sep 05 '20 at 23:31
  • 1
    @blak3r after reading through the comments, looks like this one is indeed a better answer. – Thariq Nugrohotomo Sep 24 '20 at 04:41
48

One of the best semi solutions is to copy %1 into a variable and then use delayed expansion, as delayedExp. is always safe against any content.

set "param1=%~1"
setlocal EnableDelayedExpansion
if "!param1!"=="" ( echo it is empty )
rem ... or use the DEFINED keyword now
if defined param1 echo There is something

The advantage of this is that dealing with param1 is absolutly safe.

And the setting of param1 will work in many cases, like

test.bat hello"this is"a"test
test.bat you^&me

But it still fails with strange contents like

test.bat ^&"&

To be able to get a 100% correct answer for the existence

It detects if %1 is empty, but for some content it can't fetch the content.
This can be also be useful to distinguish between an empty %1 and one with "".
It uses the ability of the CALL command to fail without aborting the batch file.

@echo off
setlocal EnableDelayedExpansion
set "arg1="
call set "arg1=%%1"

if defined arg1 goto :arg_exists

set "arg1=#"
call set "arg1=%%1"
if "!arg1!" EQU "#" (
    echo arg1 exists, but can't assigned to a variable
    REM Try to fetch it a second time without quotes
    (call set arg1=%%1)
    goto :arg_exists
)

echo arg1 is missing
exit /b

:arg_exists
echo arg1 exists, perhaps the content is '!arg1!'

If you want to be 100% bullet proof to fetch the content, you could read How to receive even the strangest command line parameters?

jeb
  • 78,592
  • 17
  • 171
  • 225
21

From IF /?:

If Command Extensions are enabled IF changes as follows:

IF [/I] string1 compare-op string2 command
IF CMDEXTVERSION number command
IF DEFINED variable command

......

The DEFINED conditional works just like EXISTS except it takes an environment variable name and returns true if the environment variable is defined.

Andy Morris
  • 3,393
  • 1
  • 21
  • 20
  • 2
    Yes, I actually tried this approach and from what I could tell this only works with ENVIRONMENT variables. Therefore, didn't work with %1 or a variable defined inside the batch file. – blak3r Mar 31 '10 at 14:23
  • 2
    That is only true for `%1` and the likes. "Variables defined inside the batch file" actually **are** environment variables while the batch file is running, and you can use `IF DEFINED` on them. – zb226 Mar 01 '13 at 13:20
19

Unfortunately I don't have enough reputation to comment or vote on the current answers to I've had to write my own.

Originally the OP's question said "variable" rather than "parameter", which got very confusing, especially as this was the number one link in google for searching how to test for blank variables. Since my original answer, Stephan has edited the original question to use the correct terminology, but rather than deleting my answer I decided to leave it to help clear up any confusion, especially in case google is still sending people here for variables too:

%1 IS NOT A VARABLE! IT IS A COMMAND LINE PARAMETER.

Very important distinction. A single percent sign with a number after it refers to a command line parameter not a variable.

Variables are set using the set command, and are recalled using 2 percent signs - one before and one after. For example %myvar%

To test for an empty variable you use the "if not defined" syntax (commands explicitly for variables do not require any percent signs), for example:

set myvar1=foo  
if not defined myvar1 echo You won't see this because %myvar1% is defined.  
if not defined myvar2 echo You will see this because %myvar2% isn't defined.

(If you want to test command line parameters then I recommend referring to jamesdlin's answer.)

AutoMattTick
  • 453
  • 4
  • 6
  • 1
    you are right. But the correct way would have been to just correct the title from `if variable is empty` to `if parameter is empty`. I just did it. (at the risk to invalidate some of the answers, which did check variables instead of parameters and so are invalid anyway, as they didn't answer the question) – Stephan Jul 24 '18 at 16:02
  • 1
    OK thanks Stephan, didn't realise you could do that. I've edited my answer to reflect the update. – AutoMattTick Jul 27 '18 at 14:20
7

You can use

if defined (variable) echo That's defined!
if not defined (variable) echo Nope. Undefined.
noname
  • 79
  • 1
  • 1
  • 1
    Welcome to SO! Your reply does seem a good answer to the question. Once you have sufficient [reputation] (http://stackoverflow.com/help/whats-reputation) you will be able to [comment](http://stackoverflow.com/help/privileges/comment) on any post. Also check this [what can I do instead](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – thewaywewere Apr 26 '17 at 01:18
6

Use "IF DEFINED variable command" to test variable in batch file.

But if you want to test batch parameters, try below codes to avoid tricky input (such as "1 2" or ab^>cd)

set tmp="%1"
if "%tmp:"=.%"==".." (
    echo empty
) else (
    echo not empty
)
zackz
  • 69
  • 1
  • 1
5

I created this small batch script based on the answers here, as there are many valid ones. Feel free to add to this so long as you follow the same format:

REM Parameter-testing

Setlocal EnableDelayedExpansion EnableExtensions

IF NOT "%~1"=="" (echo Percent Tilde 1 failed with quotes) ELSE (echo SUCCESS)
IF NOT [%~1]==[] (echo Percent Tilde 1 failed with brackets) ELSE (echo SUCCESS)
IF NOT  "%1"=="" (echo Quotes one failed) ELSE (echo SUCCESS)
IF NOT [%1]==[] (echo Brackets one failed) ELSE (echo SUCCESS)
IF NOT "%1."=="." (echo Appended dot quotes one failed) ELSE (echo SUCCESS)
IF NOT [%1.]==[.] (echo Appended dot brackets one failed) ELSE (echo SUCCESS)

pause
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
5

I test with below code and it is fine.

@echo off

set varEmpty=
if not "%varEmpty%"=="" (
    echo varEmpty is not empty
) else (
    echo varEmpty is empty
)
set varNotEmpty=hasValue
if not "%varNotEmpty%"=="" (
    echo varNotEmpty is not empty
) else (
    echo varNotEmpty is empty
)
Kate
  • 1,107
  • 8
  • 7
  • Your solution fails when the variable contains a quote. Btw there exists the syntax `IF DEFINED var` for a reason – jeb Mar 06 '18 at 15:51
4

I usually use this:

IF "%1."=="." GOTO MyLabel

If %1 is empty, the IF will compare "." to "." which will evaluate to true.

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • 4
    This is incorrect. This is the same thing as: IF "%1" == "" the reason for this post was if %1 itself has quotes this fails. – blak3r Mar 31 '10 at 14:21
  • Are you trying to test if %1 is empty or if it has quotes? The question is unclear. My answer works if nothing is specified on the command line for %1. – aphoria Mar 31 '10 at 18:41
  • *> Are you trying to test if %1 is empty or if it has quotes?*   @aphoria, it has to be able to handle both. – Synetech Oct 21 '12 at 16:17
  • I used this to work out if %1% had been set, worked nicely for me. Thanks, good answer! – Charlie A Jan 17 '14 at 16:33
  • 1
    aphoria - forgive the 10-years-later response but I just read this today. To expand on what @blak3r said... For example, your batch file is named `batch.cmd`. In the simplest terms, you execute: `batch.cmd "`. That is, only one parameter consisting of one ***double-quote*** mark. Or `batch.cmd ab"cd`, or anything more complex, generally described as having an odd number of [`"`] marks. Your statement: `IF "%1."=="." GOTO MyLabel` will fail and cause the batch execution to end (crash), usually with error: `The syntax of the command is incorrect.`, because the [`"`] marks can't be matched. – Kevin Fegan Sep 03 '20 at 22:59
4

Empty string is a pair of double-quotes/"", we can just test the length:

set ARG=%1
if not defined ARG goto nomore

set CHAR=%ARG:~2,1%
if defined CHAR goto goon

then test it's 2 characters against double-quotes:

if ^%ARG:~1,1% == ^" if ^%ARG:~0,1% == ^" goto blank
::else
goto goon

Here's a batch script you can play with. I think it properly catches the empty string.

This is just an example, you just need to customize 2 (or 3?) steps above according to your script.

@echo off
if not "%OS%"=="Windows_NT" goto EOF
:: I guess we need enableExtensions, CMIIW
setLocal enableExtensions
set i=0
set script=%0

:LOOP
set /a i=%i%+1

set A1=%1
if not defined A1 goto nomore

:: Assumption:
:: Empty string is (exactly) a pair of double-quotes ("")

:: Step out if str length is more than 2
set C3=%A1:~2,1%
if defined C3 goto goon

:: Check the first and second char for double-quotes
:: Any characters will do fine since we test it *literally*
if ^%A1:~1,1% == ^" if ^%A1:~0,1% == ^" goto blank
goto goon

:goon
echo.args[%i%]: [%1]
shift
goto LOOP

:blank
echo.args[%i%]: [%1] is empty string
shift
goto LOOP

:nomore
echo.
echo.command line:
echo.%script% %*

:EOF

This torture test result:

.test.bat :: ""  ">"""bl" " "< "">"  (")(") "" :: ""-"  " "( )"">\>" ""
args[1]: [::]
args[2]: [""] is empty string
args[3]: [">"""bl" "]
args[4]: ["< "">"]
args[5]: [(")(")]
args[6]: [""] is empty string
args[7]: [::]
args[8]: [""-"  "]
args[9]: ["( )"">\>"]
args[10]: [""] is empty string

command line:
.test.bat :: ""  ">"""bl" " "< "">"  (")(") "" :: ""-"  " "( )"">\>" ""
  • It's wrong, an empty string isn't a string with two double quotes `""` an empty string is an empty string `` and in batch an empty variable is an undefined variable. You can define, that for your arguments the outer quotes will be removed, but then the string is still empty with a length of 0 – jeb Oct 04 '16 at 07:51
  • In this context (win cmd arguments), you can not denote an empty string with any other way, not even with double single-quote which equally empty under unix shell. – user6801759 Oct 05 '16 at 14:55
  • Yes, `""` are used for empty strings too, but it's quite easy to use `%~1` to remove the outer quotes. there is no need to use such a complex solution – jeb Oct 06 '16 at 12:56
  • You right, I might improperly stated, it not just for empty string but a safe way to enumerate arguments. Other ways such as `"%~1"==""` will fail with argument like `"Long File Name".txt` which is a valid argument. **edit**: And it's definitely not complex as you said. Only 2 steps above for test, the rest are verbose example. – user6801759 Oct 07 '16 at 04:28
  • "if not defined ARG" seems to work perfectly (my variable doesn't come from command line) – GrayFace Jan 11 '17 at 08:43
4

To sum things up:

set str=%~1
if not defined str ( echo Empty string )

This code will output "Empty string" if %1 is either "" or " or empty. Added it to the accepted answer that's currently incorrect.

GrayFace
  • 1,224
  • 10
  • 13
3

Script 1:

Input ("Remove Quotes.cmd" "This is a Test")

@ECHO OFF

REM Set "string" variable to "first" command line parameter
SET STRING=%1

REM Remove Quotes [Only Remove Quotes if NOT Null]
IF DEFINED STRING SET STRING=%STRING:"=%

REM IF %1 [or String] is NULL GOTO MyLabel
IF NOT DEFINED STRING GOTO MyLabel


REM   OR   IF "." equals "." GOTO MyLabel
IF "%STRING%." == "." GOTO MyLabel

REM GOTO End of File
GOTO :EOF

:MyLabel
ECHO Welcome!

PAUSE

Output (There is none, %1 was NOT blank, empty, or NULL):


Run ("Remove Quotes.cmd") without any parameters with the above script 1

Output (%1 is blank, empty, or NULL):

Welcome!

Press any key to continue . . .

Note: If you set a variable inside an IF ( ) ELSE ( ) statement, it will not be available to DEFINED until after it exits the "IF" statement (unless "Delayed Variable Expansion" is enabled; once enabled use an exclamation mark "!" in place of the percent "%" symbol}.

For example:

Script 2:

Input ("Remove Quotes.cmd" "This is a Test")

@ECHO OFF

SETLOCAL EnableDelayedExpansion

SET STRING=%0
IF 1==1 (
  SET STRING=%1
  ECHO String in IF Statement='%STRING%'
  ECHO String in IF Statement [delayed expansion]='!STRING!'
) 

ECHO String out of IF Statement='%STRING%'

REM Remove Quotes [Only Remove Quotes if NOT Null]
IF DEFINED STRING SET STRING=%STRING:"=%

ECHO String without Quotes=%STRING% 

REM IF %1 is NULL GOTO MyLabel
IF NOT DEFINED STRING GOTO MyLabel

REM GOTO End of File
GOTO :EOF

:MyLabel
ECHO Welcome!

ENDLOCAL
PAUSE

Output:

C:\Users\Test>"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd" "This is a Test"  
String in IF Statement='"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"'  
String in IF Statement [delayed expansion]='"This is a Test"'  
String out of IF Statement='"This is a Test"'  
String without Quotes=This is a Test  

C:\Users\Test>  

Note: It will also remove quotes from inside the string.

For Example (using script 1 or 2): C:\Users\Test\Documents\Batch Files>"Remove Quotes.cmd" "This is "a" Test"

Output (Script 2):

String in IF Statement='"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"'  
String in IF Statement [delayed expansion]='"This is "a" Test"'  
String out of IF Statement='"This is "a" Test"'  
String without Quotes=This is a Test  

Execute ("Remove Quotes.cmd") without any parameters in Script 2:

Output:

Welcome!

Press any key to continue . . .
sjngm
  • 12,423
  • 14
  • 84
  • 114
Mr. Rick
  • 2,456
  • 1
  • 15
  • 3
0

I've had a lot of issues with a lot of answers on the net. Most work for most things, but there's always a corner case that breaks each one.
Maybe it doesn't work if it's got quotes, maybe it breaks if it doesn't have quotes, syntax error if the var has a space, some will only work on parameters (as opposed to environment variables), other techniques allow an empty set of quotes to pass as 'defined', and some trickier ones won't let you chain an else afterward.

Here's a solution I'm happy with, please let me know if you find a corner case it won't work for.

:ifSet
if "%~1"=="" (Exit /B 1) else (Exit /B 0)

Having that subroutine either in a your script, or in it's own .bat, should work.
So if you wanted to write (in pseudo):

if (var)
then something
else somethingElse

You can write:

(Call :ifSet %var% && (
    Echo something
)) || (
    Echo something else
)

It worked for all my tests:

(Call :ifSet && ECHO y) || ECHO n
(Call :ifSet a && ECHO y) || ECHO n
(Call :ifSet "" && ECHO y) || ECHO n
(Call :ifSet "a" && ECHO y) || ECHO n
(Call :ifSet "a a" && ECHO y) || ECHO n

Echo'd n, y, n, y, y


More examples:

  • Wanna just check if? Call :ifSet %var% && Echo set
  • Just if not (only the else)? Call :ifSet %var% || Echo set
  • Checking a passed argument; works fine. Call :ifSet %1 && Echo set
  • Didn't want to clog up your scripts/dupe code, so you put it in it's own ifSet.bat? No problem: ((Call ifSet.bat %var%) && Echo set) || (Echo not set)
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
  • Found a case; If you try the if with else syntax, and the last command in the then block fails, the else will execute: `(Call ifSet.bat YES && (Echo true & Echo x | findstr y)) || Echo`, echos `true` *and* `false` – Hashbrown Aug 04 '16 at 01:31
  • Actually, if this is an issue, and you can deal with an extra line, you can mitigate this by calling `ifSet` on its own, then using `If %errorlevel% EQU 0 (x) Else (y)` – Hashbrown Aug 04 '16 at 07:45
  • What cases does the accepted answer (use square brackets) not work on? And can you explain what the Exit /B does – blak3r Aug 07 '16 at 18:17
  • for some reason when I tested it broke when using `set` variables (like `[%bob%]`, only working for arguments like `[%2]`) but now rechecking it works. I guess my only gripe with `[]` now is it lets empty quotes pass, whereas this wont (so it's up to your use case, really) – Hashbrown Aug 12 '16 at 03:39
  • The `/B` flag stops the whole batch from exiting, only exiting the subroutine or `call`'d batch. Unless you're asking what the [whole command](http://ss64.com/nt/exit.html) is doing; it's returning an error code to let the calling script know the result of the subroutine (`0` pass, `1` fail), usable via boolean logic in the answer, or via `%errorlevel%` in the above comments – Hashbrown Aug 12 '16 at 03:43
0

Using ! instead of " for empty string checking

@echo off
SET a=
SET b=Hello
IF !%a%! == !! echo String a is empty
IF !%b%! == !! echo String b is empty
slfan
  • 8,950
  • 115
  • 65
  • 78
Lin W
  • 289
  • 4
  • 4
  • 1
    The tip is really bad. Quotes can handle spaces and special characters at least, but exclamation marks can't. And exclamation marks get really nasty here when delayed expansion is enabled – jeb Mar 06 '18 at 17:35
0
This way looks correct:

if "%~1" == "" if [%1] == [] echo Argument 1 is really empty.

First filter (if "%~1" == "") из safe way to detect argument is empty or "".
Second filter (if [%1] == []) will skip "" argument.
Remember we have two kind of empty arguments : really empty (nothing) and empty quotes "".
We have to detect both.

Next code takes all arguments "as is" in variable args:

:GetArgs
set args=%1
:ParseArgs
shift /1
if "%~1" == "" if [%1] == [] goto :DoneArgs
set args=%args% %1
goto :ParseArgs
:DoneArgs
if not defined args echo No arguments
if defined args echo Aguments are: %args%

This code correctly process "normal" arguments (simple words, "multiword phrases" or "") with balanced quotes.
Quoted arguments can even contain special chars like "a & b".
But it is still can fail with "abnormal" expressions with unbalanced quotes (do not use them).
  • But it fails with a syntax error for `myBatch ^&`. Btw. Using `[]` is useless, they have no special meaning and can't escape anything. – jeb Oct 07 '21 at 15:45
0

The simplest solution, made of two lines of code:

SET /A var02 = %var01% / 1
IF %ERRORLEVEL% NEQ 0 (ECHO Variable var01 is NOT EXIST)
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
alive-one
  • 33
  • 5
  • Simple but wrong. test it with `set "var01=User&Doc"`, `set var01=---` or `set var01=1 0 0` – jeb Feb 25 '22 at 15:44
  • You're right. My example only check if variable exist, not if it is empty. I misunderstood topic task. – alive-one Mar 03 '22 at 05:13
-1

I got in in just under a month old (even though it was asked 8 years ago)... I hope s/he's moved beyond batch files by now. ;-) I used to do this all the time. I'm not sure what the ultimate goal is, though. If s/he's lazy like me, my go.bat works for stuff like that. (See below) But, 1, the command in the OP could be invalid if you are directly using the input as a command. i.e.,

"C:/Users/Me"

is an invalid command (or used to be if you were on a different drive). You need to break it in two parts.

C:
cd /Users/Me

And, 2, what does 'defined' or 'undefined' mean? GIGO. I use the default to catch errors. If the input doesn't get caught, it drops to help (or a default command). So, no input is not an error. You can try to cd to the input and catch the error if there is one. (Ok, using go "downloads (only one paren) is caught by DOS. (Harsh!))

cd "%1"
if %errorlevel% neq 0 goto :error

And, 3, quotes are needed only around the path, not the command. i.e.,

"cd C:\Users" 

was bad (or used to in the old days) unless you were on a different drive.

cd "\Users" 

is functional.

cd "\Users\Dr Whos infinite storage space"

works if you have spaces in your path.

@REM go.bat
@REM The @ sigh prevents echo on the current command
@REM The echo on/off turns on/off the echo command. Turn on for debugging
@REM You can't see this.
@echo off
if "help" == "%1" goto :help

if "c" == "%1" C:
if "c" == "%1" goto :done

if "d" == "%1" D:
if "d" == "%1" goto :done

if "home"=="%1" %homedrive%
if "home"=="%1" cd %homepath%
if "home"=="%1" if %errorlevel% neq 0 goto :error
if "home"=="%1" goto :done

if "docs" == "%1" goto :docs

@REM goto :help
echo Default command
cd %1
if %errorlevel% neq 0 goto :error
goto :done

:help
echo "Type go and a code for a location/directory
echo For example
echo go D
echo will change disks (D:)
echo go home
echo will change directories to the users home directory (%homepath%)
echo go pictures
echo will change directories to %homepath%\pictures
echo Notes
echo @ sigh prevents echo on the current command
echo The echo on/off turns on/off the echo command. Turn on for debugging
echo Paths (only) with folder names with spaces need to be inclosed in         quotes (not the ommand)
goto :done

:docs
echo executing "%homedrive%%homepath%\Documents"
%homedrive%
cd "%homepath%\Documents"\test error\
if %errorlevel% neq 0 goto :error
goto :done

:error
echo Error: Input (%1 %2 %3 %4 %5 %6 %7 %8 %9) or command is invalid
echo go help for help
goto :done

:done
Xorange
  • 11
  • 3
  • there is a reason for `cd`'s `/d` switch: `cd /d "C:\Users\Me" (and the proper path delimeter for Windows is `\`, not `/`). – Stephan Jul 24 '18 at 15:58
  • It's a long post, but doesn't even try to answer the question. It's about how to detect empty arguments, not `what could be a default behaviour for an empty argument` – jeb Oct 08 '21 at 06:25