/FileTree

🌳 FileTree 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.

Primary LanguagePowerShellMIT LicenseMIT

🌳 FileTree

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.

📄 Example Output

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

📑 Table of Contents


🌳 FileTree PowerShell Script

📖 Overview

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.

📂 Special Handling of Certain Files/Directories

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.

📝 File Output

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.

🖥️ Error Handling

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.

⏱️ Performance Control

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.

🛠 Prerequisites

  • 🪟 Windows 11
  • 🖥 PowerShell 5.1 or higher

✨ Features

  • 🌳 Generates a tree-like view of directories and files
  • 📄 Helps you quickly understand the hierarchy of your project
  • 🏁 Simple to set up and use

🚀 How to Use

🏁 Initial Setup

  1. 💾 Save FileTree.ps1 in a directory of your choice.

🏃‍♂️ Running the Script

  1. 💻 Open the Windows PowerShell terminal.

  2. 📂 Navigate to the directory you want to visualize.

  3. 🎬 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:

🛠 Option 1: Add Script Directory to the PATH

  1. 📁 Move Script: Move FileTree.ps1 to a directory that is easy to remember and access. For example, you could create a Scripts folder in your user directory or your C:\ drive.

  2. 🌐 Add to PATH: Add that directory to your system's PATH environment variable.

  3. 🔄 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.

🛠 Option 2: Create a PowerShell Alias

  1. 📝 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.

  2. 👍 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"
  3. 💾 Save and Close Notepad: Save the changes and close Notepad.

  4. 🔄 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.

🛠 Option 3: Use a Fully Qualified Path

  • 📍 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 🌟.