I want to use a .bat file that executes the .dir command to create a list of all folders in all subfolders of the current folder.

========================== I am close: I use: dir /s /b /o:n /a:d >list.txt

to get the list of folders along with the full path.

...but I want the list with the folder names only, without the fullpath.

Is that possible?

  • If you don't mind using PowerShell, you can use Get-ChildItem -Directory -Path "C:\temp". – shinjijai Feb 26 at 20:57
  • 1
    @JeffZeitlin Add "tokens=*" after /F to fix the space problem. Use that and convert your comment into an answer :) – DavidPostill Feb 26 at 21:26
  • 1
    @DavidPostill - !!!! – Jeff Zeitlin Feb 26 at 21:30
  • 1
    @JeffZeitlin ??? – DavidPostill Feb 26 at 21:31
  • 1
    @DavidPostill - Never really got the hang of "tokens=..."; had to try it - and was subsequently astonished at how easy the "complicated" turned out to be... :) – Jeff Zeitlin Feb 26 at 21:35

With a little help from David Postill, you can do this with

(for /f "tokens=*" %I in ('dir /b /s /a:d') do @echo %~nxI) | sort > list.txt

to store the sorted list in the file. I guess it wasn't so complicated after all. :)

(I still prefer the PowerShell solution, though...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.