As I said in your other question: Batch: return value from SETLOCAL EnableDelayedExpansion
Just follow the link for the โperfectโ solution.
Or I can paste the code here
rem ** Preparing CR and LF for later use for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a" set LF=^ rem TWO Empty lines are neccessary
Then at the beginning of your function, to detect if delayedExpansion is turned off or on
setlocal set "NotDelayedFlag=!" setlocal EnableDelayedExpansion
And at the end of your function, to return the value
rem ** Prepare for return set "var=!var:%%=%%~2!" set "var=!var:"=%%~3!" for %%a in ("!LF!") do set "var=!var:%%~a=%%~L!" for %%a in ("!CR!") do set "var=!var:%%~a=%%~4!" rem ** It is neccessary to use two IF's, else the %var% expansion doesn't work as expected if not defined NotDelayedFlag set "var=!var:^=^^^^!" if not defined NotDelayedFlag set "var=%var:!=^^^!%" ! set "replace=%% """ !CR!!CR!" for %%L in ("!LF!") do ( for /F "tokens=1,2,3" %%2 in ("!replace!") DO ( ENDLOCAL ENDLOCAL set "%~1=%var%" ! @echo off goto :eof ) )
EDIT: a slightly shorter change
Well, that looks a little more complicated, and in many cases it can be solved with another trick.
If you know that you will switch from EnableDelayed to DisableDelayed, and you are sure that you are not using LF, FOR-RETURN will work too.
@echo off setlocal call :myTest result set result goto :eof :myTest setlocal EnableDelayedExpansion rem ... do something here set "value=^!_&_%%_|_>" echo -- for /f ^"eol^=^ ^ delims^=^" %%a in ("!value!") do ( endlocal set "%~1=%%a" goto :eof )
The separation of for /f ^"eol^=^....
is only to disable the eol character.
jeb Feb 23 '12 at 18:46 2012-02-23 18:46
source share