FileTree.ps1
📜 is a PowerShell script designed for Windows 11 that generates a tree-like 🌳 view of a directory's structure within the Windows terminal 🖥️. It helps you easily visualize the hierarchy of folders and files within a directory.
Here's an example of what the output generated by FileTree:
Your_Root_Directory
+--- build
| +--- favicon.ico
| +--- manifest.json
| +--- robots.txt
+--- node_modules
+--- public
| +--- favicon.ico
| +--- index.html
| +--- manifest.json
| +--- robots.txt
+--- src
\--- actions
\--- components
| +--- ActionButtons
| | +--- ActionButtons.css
| | +--- ActionButtons.jsx
| +--- C4
| | +--- C4.js
| +--- ConvertedContent
| | +--- ConvertedContent.css
| | +--- ConvertedContent.jsx
| +--- Dropdown
| | +--- Dropdown.css
| | +--- Dropdown.jsx
| +--- Header
| | +--- Header.css
| | +--- Header.jsx
| +--- Settings
| \--- C4Settings.js
| \--- ModSettings.js
| \--- Settings.css
| \--- Settings.js
\--- containers
- 📖 Overview
- [📂 Special Handling of Certain Files/Directories]
- [📝 File Output]
- [🖥️ Error Handling]
- [⏱️ Performance Control]
- 🛠 Prerequisites
- ✨ Features
- 🚀 How to Use
- 👩💻 Options for Accessibility
The FileTree.ps1
is a PowerShell script designed specifically for Windows 11 that provides a visual representation of the directory structure. It displays both directories 📂 and files 📄 in a tree-like 🌳 format, making it easier to understand the layout of your project.
The script handles the following directories and files specially:
- Directories named 'Archive', 'node_modules' and 'build' are skipped and not traversed further.
- Files having names beginning with 'nu', 'Nu', or 'NU', files matching the pattern '_tree.txt$', and files with names 'README.md', 'LICENSE', 'reportWebVitals.js', '.eslintrc.js' are skipped.
- Files with extensions '.png' and '.jpg' are also excluded.
If the script encounters locked or inaccessible files, they are stored in an array $lockedFiles
and their paths are displayed at the end of script execution.
The script exports the generated tree structure to a .txt
file in the same directory where the script is run. The filename includes the date and time of generation in the format yyyyMMddTHHmmss_tree.txt
.
If a file with the same name already exists, its content will be cleared before writing the new tree structure.
The script uses PowerShell's Try-Catch
block to handle errors that may occur when attempting to access certain directories or files. These errors are silently caught and the file or directory path is added to the $lockedFiles
array.
For performance control, a maximum of 5 retries (controlled by $maxRetries
) with a delay of 1 second (controlled by $delayInSeconds
) are implemented when trying to access a file or directory.
- 🪟 Windows 11
- 🖥 PowerShell 5.1 or higher
- 🌳 Generates a tree-like view of directories and files
- 📄 Helps you quickly understand the hierarchy of your project
- 🏁 Simple to set up and use
- 💾 Save
FileTree.ps1
in a directory of your choice.
-
💻 Open the Windows PowerShell terminal.
-
📂 Navigate to the directory you want to visualize.
-
🎬 Run
.\FileTree.ps1
.
The script will display the directory structure in the terminal in a tree-like format, just like the example provided above.
👩💻 To make FileTree.ps1
easily accessible from any directory, you have several options:
-
📁 Move Script: Move
FileTree.ps1
to a directory that is easy to remember and access. For example, you could create aScripts
folder in your user directory or yourC:\
drive. -
🌐 Add to PATH: Add that directory to your system's
PATH
environment variable. -
🔄 Open a New PowerShell Session: After this, you can call
FileTree.ps1
from any directory.- ❗ Note: Incorrectly modifying the
PATH
variable can lead to issues. Make sure you know what you're doing before modifying it.
- ❗ Note: Incorrectly modifying the
-
📝 Open Your PowerShell Profile: Enter
notepad $PROFILE
in a PowerShell window. This will open your PowerShell profile in Notepad. You'll be prompted to create one if the profile doesn't exist. -
👍 Add Alias: Add the following line to your PowerShell profile, replacing
C:\path\to\FileTree.ps1
with the full path to your script:Set-Alias filetree "C:\path\to\FileTree.ps1"
-
💾 Save and Close Notepad: Save the changes and close Notepad.
-
🔄 Reload Profile or Restart PowerShell: For the changes to take effect, either reload your profile with
. $PROFILE
or close and reopen PowerShell.
After doing this, you should be able to use the command filetree
in PowerShell, no matter your current directory, to run your FileTree.ps1
script.
-
📍 This doesn't allow you to run it from "anywhere," but you can always run the script using its fully qualified path:
& "C:\path\to\FileTree.ps1"
Any of these methods will allow you to execute the script from any directory, making it easier to visualize your directory structure. Choose the one that best suits your needs 🌟.