2

I have been working on getting the parent directory for a file using a technique from one of the other questions asked in a for loop. I have gotten it to work for the most part except i think that since my script already takes in 1 parameter which is needed in order to run for multiple file locations, I need to keep it. My getparentdir goto needs some way of manipulating the parameter. I have to do it for multiple directories' files in the same parent directory. Any help would be amazing.

When i run it, it seems to prefer the parameter for the script and not whats being sent into the GOTO so nothing is getting done.

setlocal ENABLEDELAYEDEXPANSION

for /D %%f in (F:\Falcon\Inbound\%1\*) do (

    for %%g in ("%%f\*.PDF") do (
      set ParentDir="%%g"
      echo !ParentDir!
      set ParentDir=%ParentDir: =:%
      set ParentDir=%ParentDir:\= %


      :getparentdir
      if "%~1" EQU "" goto :cont
      Set ParentDir=%~1
      shift
      goto :getparentdir
      call :getparentdir


      :cont 
      set ParentDir=%ParentDir::= %
      echo ParentDir is !ParentDir!

    )
)
Echilon
  • 10,064
  • 33
  • 131
  • 217

2 Answers2

1

I cannot figure out what you are trying to do. But I see a number of problems with the code in your question.

1) You set ParentDir within the DO() block, and properly use delayed expansion to ECHO the value that was just set. But later on in the same block of code you mistakenly revert to normal expansion to perform search and replace. You need to use delayed expansion throughout the block of code. For example:
set ParentDir=!ParentDir: =:!.

2) You have CALL following GOTO. Of course the CALL can never execute because the GOTO bypasses it. That probably is just as well.

3) You attempt to GOTO within a FOR loop. That will immediately break the loop, and the logic will most likely not be what you intended - The loop will never process more than 1 PDF file. Also, you are attempting to GOTO a label within a parenthesized DO block of code. Generally, you should not put labels within blocks of code. See (Windows batch) Goto within if block behaves very strangely for a discussion of a similar problem with an IF statement block of code.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • thanks! i'm slightly new to batch and still learning syntax and the rules of the language. This definitely helps for future projects. I was trying to get the parent directory's name and it was staring me in the face. – user1783992 Oct 30 '12 at 18:50
0

Just realized if you just give a value for the outer for loop

   for /D %%f in (F:\Falcon\Inbound\%1\*) do (

      SET ParentDir = %%~nf

      for %%g in ("%%f\*.PDF") do (

         rem - Do Code here
      )
   )

That will work just the same in the need of a for loop. Sudden clarity.

  • Perhaps clear to you, but it doesn't look right to me. ParentDir will contain the name of the folders found within "F:\Falcon\Inbound\%1". It does not give a parent folder, (at least not from my perspective). – dbenham Oct 30 '12 at 16:19