0

I have a directory with multipe levels of folders. I am completely new to writing batch files and I am writing my first one. Stuck for ages on trying to

  1. find all files in the directory including sub-folder

  2. get parent directory for each file

  3. save as variable like %parent.filename%

I have been searching here: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)

And on Google but unfortunately I am stuck.

So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.

This is the code I have been testing in the command prompt.

For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA

EDIT

I also saw this thread

And tried this code:

FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB

But %~nxB doesn't return any value... I expected it to get the last string of the path.

2 Answers2

1
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB

Note the extra .

The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"

As a one-line command (within a batch, decorated by standard palaver)

@ECHO OFF
SETLOCAL

FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS

set parent.
GOTO :EOF

I used the filemask test* to better suit my system.

I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I think the point being made is with `%~pA`, which contains the fully qualified path minus the drive, e.g. `\path\to\ `, _(when they want `to`)_. To do that you'd need another `For` loop or additional series of `Set` commands. – Compo Jun 15 '19 at 00:24
  • Thank you for your answer but it did not work. I added the point but it only added a point to each output line. Basically I would like my loop to set variables like this for each file found using the dir command. C:\Users\User\Documents\Linkedin\Folder\Subfolder\file1.doc SET Folder.file1.doc=Subfolder C:\Users\User\Documents\Linkedin\Folder\file2.doc SET Folder.file2.doc=Folder – Joachim Brindeau Jun 15 '19 at 00:52
  • OK - I've added-in the second line from your original posting. – Magoo Jun 15 '19 at 05:47
  • Thank you ! It worked perfectly. :) Is there a way to avoid creating a text file ? – Joachim Brindeau Jun 15 '19 at 13:05
0

I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:

For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do @For %B In ("%~pA.")Do @Set "Folder.%~nxA=%~nxB"

From a batch-file, perhaps this would help you out:

@Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="

Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"

Rem View any returned variables
Set Folder. 2>NUL&&Pause
Compo
  • 36,585
  • 5
  • 27
  • 39