Looking to grab the names of all folders in a directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Looking to grab the names of all folders in a directory

#1 Post by pditty8811 » 30 Mar 2013 00:27

I have part of the code here:

Code: Select all

for /f "delims=" %%f in ('dir /A:D /b "C:\Users\P Ditty\Documents\SH3\data\cfg\Careers"') do (
set fullpath3=%%f

for /D %%J in ("!fullpath3!\..\..") do set careerdir3=%%~nxJ

)


But this will only make the variable one folder name in that directory at a time, or per loop. I'm looking to create a multiple choice user input question using all these folder names, one choice per folder.

The multiple choice will look something similar to this:

echo Select which career you'd like to play:
echo =============
echo.
echo 1) Folder Name 1
echo 2) Folder Name 2
echo 3) Folder Name 3
echo 4) Folder Name 4
etc...
echo 5) Exit
set /p web=Type option:
if "%web%"=="1" goto dynamiccampaign
if "%web%"=="2" goto list
if "%web%"=="3" start ipconfig.exe
if "%web%"=="4" Call cleanup.bat
if "%web%"=="5" exit
goto home


So how do I assign each folder its own variable, not knowing if there will only be 2 folders or it may be even up to 10 folders in that directory?

Thanks.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Looking to grab the names of all folders in a directory

#2 Post by pditty8811 » 30 Mar 2013 00:28

Wait, I think I might be able to figure this one out.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Looking to grab the names of all folders in a directory

#3 Post by pditty8811 » 30 Mar 2013 01:40

Actually I think I need help.

This is what I got:

for /f "delims=" %%A in ('dir /a-d /b "C:\Users\P Ditty\Documents\SH3\data\cfg\Careers"') do (
echo %%A
set fullpath1=%%A
set fullpath2=%%B
set fullpath3=%%C
set fullpath4=%%D
set fullpath5=%%E
set fullpath6=%%F
set fullpath7=%%G
set fullpath8=%%H
set fullpath9=%%I
set fullpath10=%%J
set fullpath11=%%K
set fullpath12=%%L
set fullpath13=%%M
set fullpath14=%%N
set fullpath15=%%O

echo !fullpath1!
pause

for /D %%L in ("!fullpath1!\..\..") do set careerdir1=%%~nxL

for /D %%M in ("!fullpath2!\..\..") do set careerdir2=%%~nxM

for /D %%N in ("!fullpath3!\..\..") do set careerdir3=%%~nxN

for /D %%O in ("!fullpath4!\..\..") do set careerdir4=%%~nxO

for /D %%P in ("!fullpath5!\..\..") do set careerdir5=%%~nxP

for /D %%Q in ("!fullpath6!\..\..") do set careerdir6=%%~nxQ

for /D %%R in ("!fullpath7!\..\..") do set careerdir7=%%~nxR

for /D %%S in ("!fullpath8!\..\..") do set careerdir8=%%~nxS

for /D %%T in ("!fullpath9!\..\..") do set careerdir9=%%~nxT

for /D %%U in ("!fullpath10!\..\..") do set careerdir10=%%~nxU

for /D %%V in ("!fullpath11!\..\..") do set careerdir11=%%~nxV

for /D %%W in ("!fullpath12!\..\..") do set careerdir12=%%~nxW

for /D %%X in ("!fullpath13!\..\..") do set careerdir13=%%~nxX

for /D %%Y in ("!fullpath14!\..\..") do set careerdir14=%%~nxY

for /D %%Z in ("!fullpath15!\..\..") do set careerdir15=%%~nxZ
)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Looking to grab the names of all folders in a directory

#4 Post by Endoro » 30 Mar 2013 03:09

Why not do the output in the for loop:

Code: Select all

@echo off&setlocal enabledelayedexpansion
set /a count=0
for /f "delims=" %%i in ('dir /ad /b') do (
   set /a count+=1
   echo !count!^) %%~i
)

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Looking to grab the names of all folders in a directory

#5 Post by pditty8811 » 30 Mar 2013 11:08

Endoro wrote:Why not do the output in the for loop:

Code: Select all

@echo off&setlocal enabledelayedexpansion
set /a count=0
for /f "delims=" %%i in ('dir /ad /b') do (
   set /a count+=1
   echo !count!^) %%~i
)


Hi,

I did achieve the result I wanted but it's about 20 lines of code.

Could you explain to me your code so I can figure out this easier way? What is the last echo line doing?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Looking to grab the names of all folders in a directory

#6 Post by Endoro » 30 Mar 2013 11:26

It is doing this:

Code: Select all

 1) Folder Name 1
 2) Folder Name 2
 3) Folder Name 3
 4) Folder Name 4
etc...


Post Reply