function variable passing problem (whitespace or quotes)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rynait
Posts: 7
Joined: 28 Nov 2010 22:29

function variable passing problem (whitespace or quotes)

#1 Post by rynait » 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

rynait
Posts: 7
Joined: 28 Nov 2010 22:29

Re: function variable passing problem (whitespace or quotes)

#2 Post by rynait » 01 Dec 2010 01:45

the table is mashed up... responding attempt with html quote

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])



Rynait

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: function variable passing problem (whitespace or quotes)

#3 Post by orange_batch » 01 Dec 2010 06:16

We did solve your problem in the previous thread (more syntax errors). You should study DOS logic more, the only problem is your code again.

If you have:

Code: Select all

set var=This Has Spaces
call :function %var%

It is identical to:

Code: Select all

call :function This Has Spaces

Same as passing that many arguments (%1=This %2=Has %3=Spaces). Just surround it with quotes and strip using %~1 syntax, otherwise you will have to use shift inefficiently.

For example:

Code: Select all

call :function This Has Spaces

:function
echo:%1
shift
echo:%1
shift
echo:%1

Echoes:

This
Has
Spaces

rynait
Posts: 7
Joined: 28 Nov 2010 22:29

Re: function variable passing problem (whitespace or quotes)

#4 Post by rynait » 01 Dec 2010 12:48

Hello

the issue here is not in relation to the previous post, I want to be able to pass the variable (no return) to function that does not include quotes or end up chopped.

[pre]
Previous post was about returning back the variable from function. and i never change subject within the post, because other people will get confused when trying to learn or seek information.

By the way in the test.bat script, already illustrated your suggested code, and i tried to show in table; the result is failed.
[/pre]

Rynait

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: function variable passing problem (whitespace or quotes)

#5 Post by orange_batch » 01 Dec 2010 19:21

Ah sorry, but it's the same problem with your syntax.

I don't get entirely why you're trying to do whatever it is you're doing. You either pass the variable name, or you pass the variable's value.

Here is every possible scenario and the outcome:

Code: Select all

set var=This Has Spaces
:: First:
call :function %var%
:: Same as call :function This Has Spaces

:: Second:
call :function "%var%"
:: Same as call :function "This Has Spaces"

:: Third:
call :function var

:: Fourth:
call :function "var"

:function
echo:%1
echo:%~1

First:
%1: This
%~1: This

(%2: Has, %3: Spaces)

Second:
%1: "This Has Spaces"
%~1: This Has Spaces

Third:
%1: var
%~1: var

Fourth:
%1: "var"
%~1: var

Code: Select all

set var=This Has Spaces
:: Fifth:
call :function var

:: Sixth:
call :function "var"

:function
:: With delayed expansion:
echo:!%1!
echo:!%~1!

:: Without delayed expansion:
call echo:%%%1%%
call echo:%%%~1%%

Fifth:
!%1! or %%%1%%: !var! or %var%: This Has Spaces
!%~1! %%%~1%%: !var! or %var%: This Has Spaces

Sixth:
!%1! or %%%1%%: !"var"! or %"var"%: (broken)
!%~1! %%%~1%%: !var! or %var%: This Has Spaces

Post Reply