Page 1 of 1

function call by referrence is not wokring

Posted: 21 May 2014 12:12
by sambasiva
@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

Re: function call by referrence is not wokring

Posted: 21 May 2014 19:52
by Ed Dyreen
Your get the same return after 1 cycle due to the last char '\' which is a valid fullPathName. Thus asking the path '%%~dp' from a path, the path itself.

Code: Select all

@echo off &prompt $g


SETLOCAL ENABLEDELAYEDEXPANSION
set temppath=C:\AT\instance\domains\ADC06b~1.COM\COMMON~1
set "temp="

:parseDomain
if "!temppath!" NEQ "C:\" (
set temp
call :getParentDir temp !temppath!
set temppath=!temp!
set temp
goto :parseDomain
) else (
echo done
)

pause
exit

:getParentDir
for %%B in ("%~2\..") do set "%~1=%%~fB"
goto :eof

Code: Select all

temppath=C:\AT\instance\domains\ADC06b~1.COM\COMMON~1
temp=C:\AT\instance\domains\ADC06b~1.COM
temppath=C:\AT\instance\domains\ADC06b~1.COM
temp=C:\AT\instance\domains\ADC06b~1.COM
temppath=C:\AT\instance\domains\ADC06b~1.COM
temp=C:\AT\instance\domains
temppath=C:\AT\instance\domains
temp=C:\AT\instance\domains
temppath=C:\AT\instance\domains
temp=C:\AT\instance
temppath=C:\AT\instance
temp=C:\AT\instance
temppath=C:\AT\instance
temp=C:\AT
temppath=C:\AT
temp=C:\AT
temppath=C:\AT
temp=C:\
temppath=C:\
done
Druk op een toets om door te gaan. . .
The good news is that 'for' supports relative pathNames

Code: Select all

   @echo off &setlocal disableDelayedExpansion

   :: removes the trailing slash
   for %%a in ("C:\This\wor^k s!\.") do (
      echo "%%~fa_"
   )

   :: go up one dir AND remove the trailing slash
   for %%a in ("C:\This\wor^k s!\\..") do (
      echo "%%~fa_"
   )

   :: move up and get extension
   for %%a in ("C:\This.tmp\wor^k s!\\..") do (
      echo "%%~xa_"
   )

   pause
   exit

   --output--
   "C:\This\wor^k s!_"
   "C:\This_"
   ".tmp_"
   Druk op een toets om door te gaan. . .

Re: function call by referrence is not wokring

Posted: 21 May 2014 21:16
by sambasiva
Thanks a lot for your help.
Will try the same.

-Thanks
SS