Has anyone else use this construct ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply

Has anyone else use this construct ?

Yes
0
No votes
No
0
No votes
 
Total votes: 0

Message
Author
carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Has anyone else use this construct ?

#1 Post by carlsomo » 19 Apr 2013 21:38

It works it seems for dirty cmd line parrameters?

Code: Select all

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Has anyone else use this construct ?

#2 Post by Endoro » 20 Apr 2013 00:01

This might be useful if you want to check if 1) %1 is empty AND 2) delayed expansion is on.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Has anyone else use this construct ?

#3 Post by trebor68 » 20 Apr 2013 00:13

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.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Has anyone else use this construct ?

#4 Post by Endoro » 20 Apr 2013 01:10

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Has anyone else use this construct ?

#5 Post by Liviu » 20 Apr 2013 22:04

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

Post Reply