@echo off setlocal EnableExtensions if "%~1"=="" ( echo Usage: %~nx0 "D:\Folder" [output.txt] exit /b 2 ) set "TARGET=%~1" if not exist "%TARGET%\" ( echo Folder not found. exit /b 2 ) set "OUTPUT=%~2" if "%OUTPUT%"=="" ( tree /f /a "%TARGET%" exit /b 0 ) if exist "%OUTPUT%" ( choice /c YN /n /m "Overwrite existing file? [Y/N] " if errorlevel 2 ( echo Cancelled. exit /b 1 ) ) tree /f /a "%TARGET%" > "%OUTPUT%" if errorlevel 1 ( echo Failed to write output file. exit /b 1 ) echo Saved: %OUTPUT% exit /b 0