Page 1 of 1

Has anyone else use this construct ?

Posted: 19 Apr 2013 21:38
by carlsomo
It works it seems for dirty cmd line parrameters?

Code: Select all

if !"%~1" equ ""! goto :whatever

Re: Has anyone else use this construct ?

Posted: 20 Apr 2013 00:01
by Endoro
This might be useful if you want to check if 1) %1 is empty AND 2) delayed expansion is on.

Re: Has anyone else use this construct ?

Posted: 20 Apr 2013 00:13
by trebor68
I have tested the following codes.

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
echo.
echo  SETLOCAL with ENABLEDELAYEDEXPANSION
if !"%~1" equ ""! goto :whatever
echo.
echo First parameter: %1
goto :eof

:whatever
echo.
echo No parameter.

Code: Select all

@echo off
setlocal enableextensions disabledelayedexpansion
echo.
echo  SETLOCAL with DISABLEDELAYEDEXPANSION
if !"%~1" equ ""! goto :whatever
echo.
echo First parameter: %1
goto :eof

:whatever
echo.
echo No parameter.


Here the result of this codes:

Code: Select all

C:\Test2>test40

 SETLOCAL with ENABLEDELAYEDEXPANSION

No parameter.

C:\Test2>test40 Hallo Peggy Sue

 SETLOCAL with ENABLEDELAYEDEXPANSION

First parameter: Hallo

C:\Test2>test40

 SETLOCAL with DISABLEDELAYEDEXPANSION

First parameter:

C:\Test2>test40 Hallo Peggy Sue

 SETLOCAL with DISABLEDELAYEDEXPANSION

First parameter: Hallo

C:\Test2>


When use SETLOCAL DISABLEDELAYEDEXPANSION is the IF construct: IF !"" equ ""! goto :whatever when the parameter ist empty.

Re: Has anyone else use this construct ?

Posted: 20 Apr 2013 01:10
by Endoro

Code: Select all

@ECHO OFF & setlocal disabledelayedexpansion
if !"%~1" equ ""! (echo equal) else echo not equal
setlocal enabledelayedexpansion
if !"%~1" equ ""! (echo equal) else echo not equal


Output without parameter is:

Code: Select all

not equal
equal


.. and with parameter:

Code: Select all

not equal
not equal

Re: Has anyone else use this construct ?

Posted: 20 Apr 2013 22:04
by Liviu
carlsomo wrote:It works it seems for dirty cmd line parrameters?
Define "dirty" and "work" ;-) Maybe you should clarify what exactly the code is attempting to do.

Endoro wrote:Output .. with parameter:
Except if the parameter is ^" or ^^ or other "dirty" variations.

Liviu