function call by referrence is not wokring
Posted: 21 May 2014 12:12
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set domain_home=C:\AT\instance\domains\ADC06b~1.COM\COMMON~1
set temppath=%domain_home%
set "temp="
goto :parseDomain
:getParentDir
for /f "tokens=*" %%A in ('dir /b %~2.?' ) do (echo domainname=%%A)
for /f "tokens=*" %%B in ("%~2") do (set %~1=%%~dpB)
goto :eof
:parseDomain
if "!temppath!" NEQ "" (
call :getParentDir temp !temppath!
echo temp=!temp!
set temppath=!temp!
goto :parseDomain
) else (
echo done
)
output
------------
domainname=CommonDomain
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
.........
..........
goes on in a loop
the problem here is that "temp" being passed by reference to getParentDir function is not getting the updated value to parseDomain (function caller) ?
it gets updated only for the first iteration...later on it is retaining the same value for all iterations.
Please suggest what is going wrong here....
Thanks
SS
SETLOCAL ENABLEDELAYEDEXPANSION
set domain_home=C:\AT\instance\domains\ADC06b~1.COM\COMMON~1
set temppath=%domain_home%
set "temp="
goto :parseDomain
:getParentDir
for /f "tokens=*" %%A in ('dir /b %~2.?' ) do (echo domainname=%%A)
for /f "tokens=*" %%B in ("%~2") do (set %~1=%%~dpB)
goto :eof
:parseDomain
if "!temppath!" NEQ "" (
call :getParentDir temp !temppath!
echo temp=!temp!
set temppath=!temp!
goto :parseDomain
) else (
echo done
)
output
------------
domainname=CommonDomain
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
temp=C:\AT\instance\domains\ADC06B~1.COM\
.........
..........
goes on in a loop
the problem here is that "temp" being passed by reference to getParentDir function is not getting the updated value to parseDomain (function caller) ?
it gets updated only for the first iteration...later on it is retaining the same value for all iterations.
Please suggest what is going wrong here....
Thanks
SS