Directory Tree to JS Object Converter
Convert any folder structure into a nested JavaScript object or JSON tree. Filter by extensions, exclude paths, limit depth, and export results.
About
Representing a file system hierarchy as a structured JavaScript object is a foundational task in build tools, documentation generators, static site scaffolding, and asset management pipelines. Manual construction of these trees is error-prone and scales poorly beyond a few dozen entries. This tool uses the browser's File System Access API to recursively traverse a selected directory and produce a faithful nested object matching the output format of the popular directory-tree npm package (~100k weekly downloads). Extension filtering accepts any valid RegExp pattern such as /\.(js|ts|html)$/, and path exclusion removes entire subtrees matching a second regex. Depth limiting caps traversal at level d, leaving deeper directory sizes as undefined to avoid misleading partial sums. The tool approximates file sizes from File.size bytes where available. Limitations: browsers do not expose Unix file mode, mtime, or symlink targets, so attribute options are constrained to what the Web API provides. The webkitdirectory fallback reconstructs the tree from flat relative paths and cannot detect empty directories.
Formulas
The directory tree is constructed via recursive depth-first traversal. For each directory entry at depth d, the algorithm checks against the depth limit and exclusion pattern before descending.
File inclusion is determined by the extensions filter applied to each entry name:
Directory size aggregation sums all child sizes bottom-up. If any child has undefined size (depth-limited), the parent size propagates as undefined:
Where dir is the current directory handle, d is current depth level, maxDepth is the user-configured limit (defaults to โ), extRegex is the compiled extensions pattern, and n is the count of children in the directory.
Reference Data
| Attribute | Type | Files | Directories | Description |
|---|---|---|---|---|
| name | string | ✓ | ✓ | Base name of the entry |
| path | string | ✓ | ✓ | Relative path from root |
| type | string | ✓ | ✓ | "file" or "directory" |
| size | number|undefined | ✓ | ✓ | Bytes. undefined if depth-limited |
| extension | string | ✓ | ✗ | File extension including dot (e.g. .jpg) |
| children | Array | ✗ | ✓ | Nested entries array |
| normalizePath | boolean | Option | Convert \\ to / | |
| depth | number | Option | Max traversal depth. 0 = root only | |
| extensions | RegExp | Option | Include only matching file extensions | |
| exclude | RegExp | Option | Exclude paths matching pattern | |
| Common Extension Patterns | ||||
| Web files | /\.(html|css|js|ts|jsx|tsx)$/ | |||
| Images | /\.(jpg|jpeg|png|gif|svg|webp|ico)$/ | |||
| Documents | /\.(pdf|doc|docx|txt|md|csv)$/ | |||
| Audio | /\.(mp3|wav|ogg|flac|aac)$/ | |||
| Video | /\.(mp4|avi|mkv|mov|webm)$/ | |||
| Archives | /\.(zip|tar|gz|rar|7z)$/ | |||
| Data | /\.(json|xml|yaml|yml|toml)$/ | |||
| Source code | /\.(py|rb|java|go|rs|c|cpp|h)$/ | |||
| Fonts | /\.(woff|woff2|ttf|otf|eot)$/ | |||
| All text | /\.(txt|md|log|csv|tsv)$/ | |||