finding parent dir and current dir names in a branch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 117
Joined: 26 Aug 2017 06:11

finding parent dir and current dir names in a branch

#1 Post by nnnmmm » 14 Jan 2022 08:29

AA=

Code: Select all

@ECHO OFF
set K1=N:\ABC
FOR /F "Tokens=*" %%U IN ('DIR "%K1%\!!*.BAT" /A-D /B /ON /S') DO (
   FOR %%V IN ("%%~fU") DO (
      FOR %%W IN ("%%~pV.") DO (
         FOR %%X IN ("%%~pV..") DO (
          ECHO %%~nxX \ %%~nxW \ %%~nxV
         )
      )
   )
)
AA is n-file x n-file operations and not n-file operations
is it possible to find parent directory names and current directory names in an ok way?

m:\abc\123\xyz\qwe.txt
xyz is the current dir name
123 is the parent dir name

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: finding parent dir and current dir names in a branch

#2 Post by aGerman » 14 Jan 2022 11:22

First of all, it works for me.
Output:
123 \ xyz \ !!foo.bat
Consider to use "%%~dpV." and "%%~dpV.." though. Just to ensure that the cmd doesn't try to work with a path relatively to your current drive.
AA is n-file x n-file operations and not n-file operations
??? If you read this twice (or three times or even four times) would you get any sensible information out of it?

Steffen

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: finding parent dir and current dir names in a branch

#3 Post by penpen » 15 Jan 2022 04:51

aGerman wrote:
14 Jan 2022 11:22
AA is n-file x n-file operations and not n-file operations
??? If you read this twice (or three times or even four times) would you get any sensible information out of it?
Just guessing that nnnmmm tries to make a claim about quadratic run time requirement of that command.
The input seem to be the n different file names (n in ℕ) produced by the dir-command in the outer for-loop.
The 'x' seems to be used as a multiplication sign.
In that case, the op expects the loops to process n² file names in total, which is simply wrong, since it processes
only 3 additional file names (the file name itself again in the second for-loop and the two directories in the inner
2 loops) per processed batch file name (so that script ends up with 4n processed file names in total).

penpen

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: finding parent dir and current dir names in a branch

#4 Post by aGerman » 15 Jan 2022 07:08

Right. This is not quadratic, at least not from Batch perspective. There is no iteration necessary to get to the file name, folder name, and parent name. (There might be an iteration over the elements of the paths behind the scenes. However, this is opaque for a Batch developer.)

Steffen

nnnmmm
Posts: 117
Joined: 26 Aug 2017 06:11

Re: finding parent dir and current dir names in a branch

#5 Post by nnnmmm » 15 Jan 2022 12:40

>> n-file x n-file operations and not n-file operations
n number of files multiplied by n number of files ... sort of like n by m matrix and not 1 by n matrix as (1 times n number of files)
>>The 'x' seems to be used as a multiplication sign
correct

>> cmd doesn't try to work with a path relatively to your current drive. ok i wont
you mean like this? "set K1=N:" instead of "set K1=N:\ABC"

when i did something like this below... all within the nested for loops, it seemed to run endlessly and halted, it could be my mistake somewhere, i will try again.
if %%~nxX equals ABC goto :A
if %%~nxX equals GGG goto :B
if %%~nxX equals hhh goto :C
i wanted to use parent directory names for filtering and sorting files.

>>so that script ends up with 4n processed file names in total
if so, i will maintain the nested four FOR LOOPs

thanks you all for help

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: finding parent dir and current dir names in a branch

#6 Post by aGerman » 16 Jan 2022 06:16

you mean like this?
No I mean what I already proposed:
Consider to use "%%~dpV." and "%%~dpV.."
In other words, involve the ~d modifier along with the ~p modifier.

Steffen

Post Reply