Access complex parameters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Access complex parameters

#16 Post by dbenham » 09 May 2011 03:46

"DOS" is seriously whacked! (as if we didn't already know this)

Extra escapes enable us to read evidence of multiple LFs, but not the LFs themselves.

test1.bat

Code: Select all

@echo off
cls
setlocal enableDelayedExpansion
set lf=^


set "var=Line1^^^^^^!lf!!lf!^^^^^^!lf!!lf!Line3^^^^^^!lf!!lf!^^!lf!!lf!Line5"
call echo var=#%%var%%#
echo on
call myBatch %%var%% nextArg

mybatch.bat

Code: Select all

@setlocal enableDelayedExpansion
@echo -------------------------
(
goto :echoEnd
rem %%*=#%*#
)
:echoEnd
@echo off
echo -------------------------
echo %%*=#%*#
set "var=%*"
echo var=#!var!#
echo -------------------------
set /a n=0
:printArgs
if "%~1" neq "" (
  set /a n+=1
  set "var=%~1"
  echo %%!n!=#%~1#
  echo var=#!var!#
  echo:
  shift /1
  goto :printArgs
)
echo -------------------------
echo on
if #%*#

test1 output

Code: Select all

var=#Line1^
^
Line3^

Line5#

>call myBatch %var% nextArg
-------------------------

>(
goto :echoEnd
 rem %*=#Line1^
 Line3
Line5 nextArg#
)
-------------------------
%*=#Line1^
var=#Line1^#
-------------------------
%1=#Line1#
var=#Line1^#

%2=##
var=#^#

%3=#Line3#
var=#Line3^#

%4=#Line5#
var=#Line5#

%5=#nextArg#
var=#nextArg#

-------------------------
The syntax of the command is incorrect.

>if #Line1^
^
Line3^

Line5 nextArg#

>

All of the LFs are there as evidenced by the broken IF statement. We just haven't been able to read them.

======================================================
amel27 wrote:In my tests about %* substitution all <CR> trimmed and <LF> replace to <CRLF>,
therefore command line splitted to lines with leading and trailing space.

Actually nothing is stripped! (broken IF again)

test2.bat

Code: Select all

@echo off
cls
setlocal enableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') Do set "cr=%%a"
set lf=^


set "var=line1!lf!!lf!xxxxxxxline3a!cr!line3b!cr!!lf!line4!"
echo var=#!var!#
echo on
myBatch! !var! nextArg

test2 output

Code: Select all

var=#line1

line3bxline3a
line4#

>myBatch! !var! nextArg
-------------------------

>(
goto :echoEnd
 rem %*=#line1
 xxxxxxxline3aline3b
 line4 nextArg#
)
-------------------------
%*=#line1
var=#line1#
-------------------------
%1=#line1#
var=#line1#

%2=#xxxxxxxline3a#
var=#xxxxxxxline3a#

%3=#line3b#
var=#line3b#

%4=#line4#
var=#line4#

%5=#nextArg#
var=#nextArg#

-------------------------
The syntax of the command is incorrect.

>if #line1

line3bxline3a
line4 nextArg#

>

======================================================

Quotes preserve all the lines as one argument, but so far seem to do more harm then good.

test3.bat

Code: Select all

@echo off
cls
setlocal enableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') Do set "cr=%%a"
set lf=^


set var="line1!lf!!lf!xxxxxxxline3a!cr!line3b!cr!!lf!line4!"
echo var=#!var!#
echo on
myBatch3! !var! nextArg

mybatch3.bat

Code: Select all

@setlocal enableDelayedExpansion
(
goto :echoEnd
rem %%*=#%*#
)
:echoEnd
@echo off
echo ---
echo %%*=#%*#
set "var=%*"
echo var=#!var!#
echo ---
set /a n=1
:printArgs
set "var=%~1"
if "!var!" neq ""  echo %%!n!=#%~1#
if "!var!" neq "" (
  echo var = #!var!#
  echo:
  set /a n+=1
  shift /1
  goto :printArgs
)
echo ---
echo on
if #%*#

test3 output

Code: Select all

var=#"line1

line3bxline3a
line4"#

>myBatch3! !var! nextArg

>(
goto :echoEnd
 rem %*=#"line1
 xxxxxxxline3aline3b
 line4" nextArg#
)
---
%*=#"line1
var=##
---
%1=#line1
var = #line1#

%2=#nextArg#
var = #nextArg#

---
The syntax of the command is incorrect.

>if #"line1

line3bxline3a
line4" nextArg#

>

All my attempts to better parse %1 have failed

======================================================
Jeb wrote:Your conclusion is not correct, as the space is inserted by the echo on mechanism, but it doesn't exists in the parameter.

To further demonstrate Jeb's point - Added space only happens within parentheses while echo on:

testEchoOn.bat

Code: Select all

@cls
rem normal output
  rem Intentional non-sensical indents throughout
(
    rem 1st line after left paren always left justified
rem subsequent lines indented 1 space
rem ...
(rem 1st line after nested left paren still left justified
rem and subsequent lines indented 1 space
    rem ...
    rem ...
    ) &rem right paren always left justified
rem but lines still indented 1 space until outermost paren closure
rem ...
rem ...
    )
    rem back to normal output
    rem ...

output with : prefix to preserve single leading space (forum quirk)

Code: Select all

:>rem normal output
:
:>rem Intentional non-sensical indents throughout
:
:>(
:rem 1st line after left paren always left justified
: rem subsequent lines indented 1 space
: rem ...
: (
:rem 1st line after nested left paren still left justified
: rem and subsequent lines indented 1 space
: rem ...
: rem ...
:)  & rem right paren always left justified
: rem but lines still indented 1 space until outermost paren closure
: rem ...
: rem ...
:)
:
:>rem back to normal output
:
:>rem ...


Dave Benham

Post Reply