Generate ASCII folder structures on Windows with Tree

I was reading through Adron Hall's post yesterday about generating ASCII trees on Linux and Mac OSx. This is a handy feature if you want to include a project/folder structure to your documentation and don't wish to copy paste screenshots. What is an ASCII tree you may ask? There you go:

Say hello to the tree command. This MS-DOS command has been around since Windows XP (YES really) but it's not well advertised. I only found out today and that's 14 years late! This is how to use it:

Open your favorite command line editor (I jump between Powershell and MS-DOS), navigate to the directory you wish to enumerate and type tree:

Hit enter and see the magic happen right in front of your eyes. You should get an output similar to this:

The command also takes 2 options:

  • /F Display the names of the files in each folder.
  • /A Use ASCII instead of extended characters.

The /F option will definitely generate a more verbose output so you may want to skip it. The /A option will alter the way the output looks. If we run the command again, we'll get something similar to this:

You'll notice the difference in the tree output format. And finally, you may wonder how you can get this onto a file in order to copy paste it into a document or check it in source control. You could pipe it straight into a file but you'll come across formatting issues due to differences in the encodings. The easiest way to achieve what you need is:

tree /a |clip

This will run the command and pipe the result to the clipboard. Open Notepad and paste the result. When you save the file, choose Save As and in the Encoding dropdown make sure you select the Unicode option:

I hope you find this useful and, as always, let me know if you have any questions in the comments below.

  • Share this post on