# TreeAndDir A lightweight Python utility for Windows that captures a complete directory listing and file-tree snapshot of the current working directory and saves them to plain-text files. --- ## Table of Contents - [Overview](#overview) - [Features](#features) - [Requirements](#requirements) - [Installation](#installation) - [Usage](#usage) - [Output Files](#output-files) - [Code Changes & Improvements](#code-changes--improvements) - [Troubleshooting](#troubleshooting) - [License](#license) --- ## Overview `TreeAndDir.py` automates two common Windows diagnostic tasks: 1. **`dir /s`** — Recursively lists every file and folder in the current directory, including file sizes, dates, and totals. 2. **`tree /F /A`** — Renders the directory structure as an ASCII tree with all file names shown (`/F`) using ASCII characters instead of extended line-drawing characters (`/A`), making the output portable across all editors and terminals. Both outputs are written to text files in the same directory where the script is run. --- ## Features - **Zero external dependencies** — uses only the Python standard library. - **Timing & file-size feedback** — reports how long each command took and the size of the output file. - **Robust error handling** — non-zero exit codes and I/O errors are caught and reported without crashing the script. - **Smart interactive detection** — the "Press Enter to exit" pause only appears when the script is run interactively; it is suppressed automatically in automated/piped contexts. - **UTF-8 output with fallback** — files are written as UTF-8; any unencodable characters are replaced rather than raising an exception. - **Clean exit codes** — exits `0` on full success, `1` if any command produced a warning, making it easy to use in batch/CI pipelines. --- ## Requirements | Requirement | Version | |---|---| | Operating System | Windows 10 / 11 (requires `cmd.exe`) | | Python | 3.8 or later | No third-party packages are needed. All modules used (`os`, `subprocess`, `sys`, `time`, `pathlib`) are part of the Python standard library. --- ## Installation 1. **Clone or download** this repository (or just copy `TreeAndDir.py` to any folder). 2. **Verify Python is installed:** ```cmd python --version ``` If Python is not installed, download it from [python.org](https://www.python.org/downloads/). 3. No `pip install` step is required. --- ## Usage ### Interactive (double-click or Command Prompt) Navigate to the directory you want to capture, then run: ```cmd cd C:\path\to\target\directory python TreeAndDir.py ``` Example session output: ``` Running 'dir /s'... Saved to C:\MyProject\DirOutput.txt (42.3 KB, 0.18s) Running 'tree /F /A'... Saved to C:\MyProject\TreeOutput.txt (8.1 KB, 0.05s) Done. Press Enter to exit... ``` ### Automated / Scripted The script integrates cleanly into batch files or CI pipelines: ```cmd cd C:\MyProject python TreeAndDir.py IF ERRORLEVEL 1 ( echo One or more commands produced warnings. ) ``` The "Press Enter" prompt is suppressed automatically when stdin is not a terminal (e.g., when output is redirected or the script is called from another process). --- ## Output Files Both files are created in the **current working directory** at the time the script is run. ### `DirOutput.txt` Contains the full output of `dir /s`, including: - File names, sizes (bytes), and last-modified timestamps for every file. - Subfolder names and their individual file counts. - A grand total of files and bytes at the end. Example excerpt: ``` Volume in drive C is Windows Volume Serial Number is XXXX-XXXX Directory of C:\MyProject 02/20/2026 10:00 AM