function call by referrence is not wokring

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

function call by referrence is not wokring

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: function call by referrence is not wokring

#2 Post by Ed Dyreen » 21 May 2014 19:52

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. . .

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: function call by referrence is not wokring

#3 Post by sambasiva » 21 May 2014 21:16

Thanks a lot for your help.
Will try the same.

-Thanks
SS

Post Reply