Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: how to list all folders in sub-directories  (Read 3817 times)

0 Members and 1 Guest are viewing this topic.

mrwonker

    Topic Starter


    Rookie

    how to list all folders in sub-directories
    « 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.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: how to list all folders in sub-directories
    « Reply #1 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.  :)

    Salmon Trout

    • Guest
    Re: how to list all folders in sub-directories
    « Reply #2 on: December 07, 2018, 12:15:15 PM »


    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




    Salmon Trout

    • Guest
    Re: how to list all folders in sub-directories
    « Reply #3 on: December 07, 2018, 12:35:47 PM »
    Geek, he or she explained just fine.

    Salmon Trout

    • Guest
    Re: how to list all folders in sub-directories
    « Reply #4 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


    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: how to list all folders in sub-directories
    « Reply #5 on: December 07, 2018, 03:55:24 PM »
    Geek, he or she explained just fine.
    I'm sorry .  :'(

    Salmon Trout

    • Guest
    Re: how to list all folders in sub-directories
    « Reply #6 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.

    mrwonker

      Topic Starter


      Rookie

      Re: how to list all folders in sub-directories
      « Reply #7 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 👍

      mrwonker

        Topic Starter


        Rookie

        Re: how to list all folders in sub-directories
        « Reply #8 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.  :)

        Salmon Trout

        • Guest
        Re: how to list all folders in sub-directories
        « Reply #9 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




        mrwonker

          Topic Starter


          Rookie

          Re: how to list all folders in sub-directories
          « Reply #10 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.