Computer Hope

Microsoft => Microsoft DOS => Topic started by: mrwonker on December 07, 2018, 05:54:22 AM

Title: how to list all folders in sub-directories
Post by: mrwonker on December 07, 2018, 05:54:22 AM
Hello hoping someone can help me with what I should think is a simple batch script I'm trying to create.
What I'm wanting to do is to run said script for it to scan through all sub-folders in the directory run from and create a .txt file listing all sub-folders but not including the current directory or the sub-folders child folders so folder structure could be as follows

>a (from where the script will be run)

>>1
>>>2
>>>3
>>>>4

>>5
>>>6
>>>7
>>>>8

and I want the result to be
2
3
6
7

I've tried
dir /a:d /b /s >".\list.txt"
but this gives me
a/1
a/1/2
a/1/3
a/1/3/4
a/5
a/5/6
a/5/7
a/5/7/8
so is giving me a list but all levels of folders and is creating the full path name.

I don't think I'm explaining what I want very clearly but to reiterate I want a list of subfolders but to not include the first level folders or the 3rd level folders and I don't want the full path only the 2nd level folders names.

Thanks, James.
Title: Re: how to list all folders in sub-directories
Post by: Geek-9pm on December 07, 2018, 11:35:14 AM
Quote
I don't think I'm explaining what I want very clearly
Yes.
Try this.
Some us a lists of folders and files as input.
Some a new list of items you want as output.
Ans wait. The best experts  are busy doing other stuff.  :)
Title: Re: how to list all folders in sub-directories
Post by: Salmon Trout on December 07, 2018, 12:15:15 PM
(https://images2.imgbox.com/dc/1b/qbXyHJKt_o.jpg)

batch

Run in top folder (called a in this example)

Code: [Select]
@echo off
if exist list.txt del list.txt
for /f "delims=" %%A in ('dir /b /ad') do dir /b /ad %%A >> list.txt

list.txt

Code: [Select]
2
3
6
7



Title: Re: how to list all folders in sub-directories
Post by: Salmon Trout on December 07, 2018, 12:35:47 PM
Geek, he or she explained just fine.
Title: Re: how to list all folders in sub-directories
Post by: Salmon Trout on December 07, 2018, 02:23:33 PM
Use quote marks like this if the folder names might have spaces

@echo off
if exist list.txt del list.txt
for /f "delims=" %%A in ('dir /b /ad') do dir /b /ad "%%A" >> list.txt

Title: Re: how to list all folders in sub-directories
Post by: Geek-9pm on December 07, 2018, 03:55:24 PM
Geek, he or she explained just fine.
I'm sorry .  :'(
Title: Re: how to list all folders in sub-directories
Post by: Salmon Trout on December 07, 2018, 04:32:11 PM
You are forgiven ;) It looked a bit strange at first look, but you can puzzle it out. What you gave was good advice. Sorry for being snippy.
Title: Re: how to list all folders in sub-directories
Post by: mrwonker on December 07, 2018, 05:31:51 PM
Thank you so much for all the replies and help I’ve been out all day and late now so will try your suggestions in the morning 👍
Title: Re: how to list all folders in sub-directories
Post by: mrwonker on December 08, 2018, 05:04:51 AM
Hi, tried the suggestion and works exactly as hoped you guys are great, I'd be interested in a little explanation on what parts of the code do what as to hopefully get a better understanding so can try and work other coding out myself rather than have to ask for help.  :)
Title: Re: how to list all folders in sub-directories
Post by: Salmon Trout on December 09, 2018, 05:12:08 AM
Hi, tried the suggestion and works exactly as hoped you guys are great, I'd be interested in a little explanation on what parts of the code do what as to hopefully get a better understanding so can try and work other coding out myself rather than have to ask for help.  :)

@echo off
Turn off automatic echoing of each command line to the console.

if exist list.txt del list.txt
If list.txt exists, delete it. By implication, if it does not exist, do nothing.

for /f "delims=" %%A in ('dir /b /ad') do dir /b /ad "%%A" >> list.txt
The FOR /F command processes each line of e.g. a command output or text file, in this case the output of dir /b /ad (executed in the same folder that the batch is in) and puts each line, one, by one, into the FOR "metavariable" %%A. Each line is the name of a folder under the folder a. So each time around the loop, the metavariable %%A expands to the name of a folder. We do a dir /b /ad on each of these folders, which gives the names of the sub folders contained within one level below. We echo that output to list.txt, using the >> (append) operator.

You can find full explanations of batch commands on many websites, one of the best is SS64.com.

https://ss64.com/nt/for_f.html



Title: Re: how to list all folders in sub-directories
Post by: mrwonker on December 09, 2018, 06:40:04 AM
thanks for your explanation, what I'm not understanding and would like to get my head round is which part of the script is telling it to scan only the subfolder as I'd be quite interested in modifying this so that it could instead of scanning the subfolders scan the subfolders-subfolders so in my example would result in 4 8 I'd hoped I could have worked this out myself but after some time trying a few different things, I'm not having much luck.