@dbenham
I'm using the
RETURN.BAT Version 2.0 you posted in this thread to return a variable with the result from a script doing some string manipulation.
The problem is that sometimes the result end up as an empty string. When it does, RETURN.BAT does not return an empty variable, but rather a variable containing
%=%I made a few changes to your test script from the same post to show the issue:
Code: Select all
@echo off
setlocal disableDelayedExpansion
call return init
for /f %%C in ('copy /z "%~f0" nul') do set "\r=%%C"
set ^"\n=^
^"
set test1="This & that" ^& the other thing
set test2="This & that" ^& the other thing!
set test3="&<>|%%^" ^&^<^>^|%%^^
set test4="&<>|%%^!" ^&^<^>^|%%^^!
setlocal enableDelayedExpansion
set "test5=!\n!Line One!\n!hidden!\r!Line Two"
set "test6="
set "err1=0"
set "err2=1"
set "err3=2"
set "err4="
set "err5="
set "err6=1"
setlocal disableDelayedExpansion
echo Delayed expansion is DISABLED
:loop
echo ------------------------------------------
for /l %%N in (1 1 6) do (
set "result="
call :test test%%N result err%%N
call set "err=%%errorlevel%%"
setlocal enableDelayedExpansion
echo result=!result!
echo errorlevel=!err!
endlocal
)
if "!!" equ "" exit /b
echo(
echo(
setlocal enableDelayedExpansion
echo Delayed expansion is ENABLED
goto :loop
:test
setlocal enableDelayedExpansion
set rtn=!%1!
echo(
echo %1=!rtn!
call return rtn %2 !%3!
The only changes from the version you posted are an added test6 with a blank/undefined variable:
And changed 5 to 6 in this for loop:
I also added a closing
" to this line. It seemed like it was missing one to me. Not sure if you forgot or if there is some reason for it I don't understand, but the result seems unaffected.
Code: Select all
set "test5=!\n!Line One!\n!hidden!\r!Line Two"
Output:
Code: Select all
Delayed expansion is DISABLED
------------------------------------------
test1="This & that" & the other thing
result="This & that" & the other thing
errorlevel=0
test2="This & that" & the other thing!
result="This & that" & the other thing!
errorlevel=1
test3="&<>|%^" &<>|%^
result="&<>|%^" &<>|%^
errorlevel=2
test4="&<>|%^!" &<>|%^!
result="&<>|%^!" &<>|%^!
errorlevel=0
test5=
Line One
Line Two
result=
Line One
Line Two
errorlevel=0
test6=
result=%=%
errorlevel=1
Delayed expansion is ENABLED
------------------------------------------
test1="This & that" & the other thing
result="This & that" & the other thing
errorlevel=0
test2="This & that" & the other thing!
result="This & that" & the other thing!
errorlevel=1
test3="&<>|%^" &<>|%^
result="&<>|%^" &<>|%^
errorlevel=2
test4="&<>|%^!" &<>|%^!
result="&<>|%^!" &<>|%^!
errorlevel=0
test5=
Line One
Line Two
result=
Line One
Line Two
errorlevel=0
test6=
result=%=%
errorlevel=1
As you can see test 6 fails to return an empty string with or without delayed expansion.
I was hoping you would be able to fix that issue so I can skip testing for %=% after each call. Apart from returning an empty string I have not found any problems. I have successfully returned stuff like a single space and unicode strings etc.