Link to home
Start Free TrialLog in
Avatar of Steve_Brady
Steve_BradyFlag for United States of America

asked on

Create a list of all folders and subfolders in a directory

Hello,

In Windows 7, I am trying to figure out how to create a list of all folders and subfolders in my

        E:\Libraries\My Documents\

directory. The following instructions were labeled "Best Answer" to a similar question I found here:

http://www.tomshardware.com/forum/16772-45-display-explorer-folders-tree-structure-export-excel
_____________________________________________________
By pratapani

In Windows:

1.      Start – Programs – Accessories – Command Prompt [This will open the DOS screen]
2.      Type: D: ENTER [where D is the drive you want to go to and ENTER is the enter key]
3.      Type CD\directory1\directory2\directory3 ENTER [where directory1, 2, 3 are the directories you want to “tree”. Add more directory names as appropriate]
4.      Type Tree ENTER [This will display the tree on the screen – just a test to see if you are in the correct directory, otherwise you can type: Dir and ENTER]
5.      Type Tree /a /f > list ENTER [Tree*/a*/f*>*list ENTER – note “*” is a space. You can also type: tree/? To see an explanation of the codes. The “>” redirects the output from the screen to a file called “list” in the same directory]

You should now close the command prompt [click on X or type: Exit ENTER]

PS if you are not sure of the directory name, you can type dir /p ENTER after step 2.

You can now open the file in Notepad or in MSWord [It will be saved in the last directory that you tree-ed]

Praveen
_____________________________________________________

Unfortunately, I don't understand DOS well enough to have even made it through step three.  :P

I would appreciate it if someone could help me with a bit more specific explanation (e.g. are DOS commands case-sensitive? And in step three, should I type "CD\E:\Libraries\My Documents\" or just "CD\My Documents\" or something altogether different?, etc.).  Also, I assume the above instructions result in a text file with the names of all folders and files.  While that would be very helpful, is there also a way to display only folders and subfolders (without all the filenames)?

Finally, is there a simpler/easier/quicker way to do this?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of lee555J5
lee555J5
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DOS commands are not case sensitive.

To see only the folders, leave out the /f so your command becomes

tree /a > list.txt [Enter]

Try the tree /? and try the tree command with and without the /a and the /f.
Use Powershell comes with Windows 7

copy and paste the code below into notepad

$MyDocs = [System.Environment]::GetFolderPath("MyDocuments")
dir $MyDocs -recurse | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName }

save as yourname.ps1
open powershell
at the powershell prompt call your script
i.e.
PS C:\>./yourname.ps1
from a command prompt you could also
dir c:\directorytoenumerate /b /ad /s >output.txt
Avatar of Steve_Brady

ASKER

Thanks for the responses.

>>>lee555J5
You're wanting E:\Libraries\Documents, so...


Thanks Lee.  Excellent answer:  clear, concise, complete.
The powershell script appears to assume the desired folder is the current user's My Documents folder, but I don't see that stated in the original post (e.g. it could be a backup location).
It also doesn't redirect the output into a file as requested.
Also, since it doesn't filter everything except what comes after the final "\" it's no different from the output of the dir command.

e.g.
dir /AD /S /B E:\Libraries\Documents > %userprofile%\desktop\FolderList.txt

For help with most DOS commands you run
command /?
so,
dir /?
will show you that
/AD displays files (a subdirectory or 'folder' is really a special file) with the attribute of directory (misnamed, as there is only 1 directory on each drive - the 'root' - all others are subdirectories)
/S makes dir 'recursive', searching the specified subdir and all subdirs below it
/B means 'bare' format, with no sizes, date/times or other extraneous info  besides names
E:\Libraries\Documents is the path you want listed
The next parts aren't covered by the dir command's help, but are often helpful when using the command line, batches and/or scripts.
> redirects the output, in this case to a file; NOTE: if the file exists, the output will overwrite it! If >> is used, the output will instead be appended to the file rather than overwriting.
%userprofile% is an "environment variable" (to see all environment variables, run set from the command line) that expands to the current user's home path, so that dir command will end up making (or overwriting) a file named FolderList.txt on the current user's desktop.

"DIR" is an internal function of CMD.EXE, by the way... i.e. there is no "dir.exe" - it's all contained in the CMD program.
DOS is for luddites. Learn Powershell.

This script will list the folders and subfolders of a given root folder sorted by size. If you really want to port it to a text file add this pipe | out-file c:\whateverpath\whateverfile.txt     after format-table -auto

copy and paste the code below into notepad

Function list($strPath)
{
$list = $null
$list = @{}
$objLoadfolders = dir $strPath -recurse -ea SilentlyContinue | Where-Object { $_.PSisContainer }
      ForEach ($Folder in $objLoadfolders)
      {
           
            $strSize = 0
            $strCount = 0
            $objLoadfiles = (Dir $Folder.FullName -recurse -ea SilentlyContinue | Where-Object { !$_.PSisContainer })
                  ForEach ($File in $objLoadfiles)
                  {
                        $strSize = ($strSize + $File.length)                        
                  }
      $list.add($Folder.FullName,$strSize)
           
      }

      $list.GetEnumerator() | Sort-Object value -descending | format-table -auto      
}

$strPath = read-host "Enter root folder"
List $strPath

save as yourname.ps1
open powershell
at the powershell prompt call your script
i.e.
PS C:\>./yourname.ps1