Write a directory tree to a text file in Windows command line
I found a question “How to write a list of directory trees to a text file?” on Stack Overflow, as I was looking for exactly the same solution. It has two brilliant answers, and since I’m using this blog as my personal remember-that-diary, I decided to share it here, so I shouldn’t look for this solution again.
The simpliest and fastest answer is to execute (in cmd
) following command:
forfiles /s /p "C:\dir" /m * /c "cmd /v:on /c if @isdir==TRUE (set f=@relpath&echo !f:~3,-1!)" >list.txt
Author says that it is slow. Well… generation of 12k result file, with 300+ directories (full paths) took just a few seconds. I think this is reasonable fast.
As per my own comment below answer, it is worth noting that this script puts listing.txt
into user home directory (i.e. C:\Users\[username]
). I was quite surprised, as I was looking at first for listing.txt
in c:
and in folder, which structure I want to dump.
If you, however require more sophisticated answer, then write a *.bat
file as mentioned in second answer. I haven’t tested and from comments bellow I can assume, that it has some problems, when trying to use path with spaces as root, script-entry parameter.