Okay, the standard Windows tree command doesn't have a direct way to filter by multiple specific extensions while still showing the tree structure leading to them. It can only show all files (/F) or no files.
However, we can pipe the output of tree /F (which shows all files) to findstr to filter the lines. This will show the files you want, correctly indented, but it will not show directory lines unless those directories happen to have names ending in .mod or .work (which is unlikely).
Here's the command:
tree /F | findstr /I /R /C:"\\.mod$" /C:"\\.work$"
Let's break it down:
-
tree /F:
tree: The command to display the folder structure.
/F: Displays the names of the files in each folder.
-
|:
- This is the "pipe" operator. It sends the output of the command on its left (
tree /F) as input to the command on its right (findstr ...).
-
findstr /I /R /C:"\\.mod$" /C:"\\.work$":
findstr: A command-line utility that searches for patterns of text in files (or, in this case, in the piped input).
/I: Makes the search case-insensitive (so it will find .MOD, .Mod, .mod, etc.).
/R: Uses search strings as regular expressions.
/C:"string": Uses the specified string as a literal search string. We use this twice for two separate patterns.
"\\.mod$": This is the first regular expression.
\\.: Matches a literal dot (. is a special character in regex, so \ escapes it).
mod: Matches the letters "mod".
$: Anchors the match to the end of the line. So, it only matches lines ending with .mod.
/C:"\\.work$": This is the second regular expression, looking for lines ending with .work.
How it works and its limitations:
tree /F will generate the full tree structure with all files.
findstr will then look at each line output by tree /F.
- If a line ends with
.mod (case-insensitive) OR .work (case-insensitive), findstr will print that line.
- Other lines (directory names, other file types) will be filtered out.
Example Output:
If your directory structure is:
MyProject/
├───src/
│ ├───main.mod
│ ├───utils.txt
│ └───helper.work
├───docs/
│ └───readme.md
└───build/
└───output.mod
The command would output something like:
│ ├───main.mod
│ └───helper.work
└───output.mod
Notice that the directory names (src/, build/) are not shown because they don't end in .mod or .work. However, the files that do match are shown with their original indentation from the tree command, giving you a sense of their location.
If you need a more sophisticated tree that shows directories only if they contain the desired files, you'd typically need to switch to PowerShell or a third-party tool.