function variable passing problem (whitespace or quotes)
Posted: 01 Dec 2010 01:41
I came across this whitespace breakup or unable to strip quotes problem in function variable passing.
I was working on a script and spotted those weird problems. turns out everytime i fix this call, i find that the problem "moves" onto another call.
so I created test.bat to analyze (track-see) this variable passing problem.
The goal is pass complete path with whitespace, but without quotes (and chop-off) into the function (the inital declared variable can either have or not have quotes).
test.bat
@echo off
::variable declared or notated
set testname="Dwarf Fortress"
set testA=Race the Sun
:newback
echo. inital values
call:eval
echo.
call:notlocal %testname% %testA%
echo.finished with notlocal call
echo.
call:eval
echo.
echo.
echo.
call:returing testname testA
echo.finished returning call (showing after call)
echo.
call:eval
echo.
echo.
call:islocal "%testname%" "%testA%"
echo.
echo. second islocal without quotes
call:islocal %testname% %testA%
echo.
echo. second islocal without %
call:islocal testname testA
echo.
echo. finished with functions, final data is
call:eval
echo.
echo.
pause
EXIT
:notlocal
set v1=%~1
set v2=%~2
echo.inside notlocal
call:eval
echo.inside V1 = [%v1%]
echo.inside V2 = [%v2%]
echo.
goto:eof
:returing
setlocal
endlocal&call set "%~1=%%%~1%%"&call set "%~2=%%%~2%%"
goto:eof
::function sections below
:islocal
SETLOCAL
set v1=%~1
set v2=%~2
echo.inside islocal
call:eval
echo.1st pass = [%v1%]
echo.2nd pass = [%v2%]
echo.
set v1=cloudy
set v2=bad
echo.
echo.trick it out
echo.1st pass = [%v1%]
echo.2nd pass = [%v2%]
echo.
endlocal&set %~1=%v1%&set %~2=%v2%
goto:eof
:eval
echo.testname = [%testname%]
echo.testA = [%testA%]
echo.v1 = [%v1%]
echo.v2 = [%v2%]
goto:eof
in above test.bat that i created "inside function variables" result table below,
function v1 v2 note
notlocal Dwarf Fortress Race (v2 fail)
returning Dwarf Fortress Race (v2 fail)
islocal "Dwarf fortress"" (", chopoff, v2 "")
islocal no "" Dwarf Fortress Race (v2 fail)
islocal no% testname testA (fail, variable [name])
Is there a trick or something to make complete pass, in particular with v2?
Rynait
I was working on a script and spotted those weird problems. turns out everytime i fix this call, i find that the problem "moves" onto another call.
so I created test.bat to analyze (track-see) this variable passing problem.
The goal is pass complete path with whitespace, but without quotes (and chop-off) into the function (the inital declared variable can either have or not have quotes).
test.bat
@echo off
::variable declared or notated
set testname="Dwarf Fortress"
set testA=Race the Sun
:newback
echo. inital values
call:eval
echo.
call:notlocal %testname% %testA%
echo.finished with notlocal call
echo.
call:eval
echo.
echo.
echo.
call:returing testname testA
echo.finished returning call (showing after call)
echo.
call:eval
echo.
echo.
call:islocal "%testname%" "%testA%"
echo.
echo. second islocal without quotes
call:islocal %testname% %testA%
echo.
echo. second islocal without %
call:islocal testname testA
echo.
echo. finished with functions, final data is
call:eval
echo.
echo.
pause
EXIT
:notlocal
set v1=%~1
set v2=%~2
echo.inside notlocal
call:eval
echo.inside V1 = [%v1%]
echo.inside V2 = [%v2%]
echo.
goto:eof
:returing
setlocal
endlocal&call set "%~1=%%%~1%%"&call set "%~2=%%%~2%%"
goto:eof
::function sections below
:islocal
SETLOCAL
set v1=%~1
set v2=%~2
echo.inside islocal
call:eval
echo.1st pass = [%v1%]
echo.2nd pass = [%v2%]
echo.
set v1=cloudy
set v2=bad
echo.
echo.trick it out
echo.1st pass = [%v1%]
echo.2nd pass = [%v2%]
echo.
endlocal&set %~1=%v1%&set %~2=%v2%
goto:eof
:eval
echo.testname = [%testname%]
echo.testA = [%testA%]
echo.v1 = [%v1%]
echo.v2 = [%v2%]
goto:eof
in above test.bat that i created "inside function variables" result table below,
function v1 v2 note
notlocal Dwarf Fortress Race (v2 fail)
returning Dwarf Fortress Race (v2 fail)
islocal "Dwarf fortress"" (", chopoff, v2 "")
islocal no "" Dwarf Fortress Race (v2 fail)
islocal no% testname testA (fail, variable [name])
Is there a trick or something to make complete pass, in particular with v2?
Rynait