This PowerShell script, FileHierarchy2csv, is designed to export the file and directory structure of a given source folder into a CSV file. It includes options to limit the directory levels to export and apply filters to ignore certain file names and extensions. The script can be run with or without a GUI, and the GUI provides an easy interface for users to configure the parameters.
- Name: Benjamin Hinz
- Date: September 22, 2024
- Version: 1.0
- GitHub Repository: Hin7zer GitHub
The following Picture shows the visual overview of the script.
The following Table is an Example of the export
| FileName | FileExtension | Level1 | Level2 | Level3 |
|---|---|---|---|---|
| TestRecord1 | wav | Music | MyMusic | - |
| Remix2012 | mp3 | Music | MyMusic | - |
| track1 | mp3 | Music | Snoop Dogg | album1 |
| track2 | mp3 | Music | Snoop Dogg | album1 |
| track1 | mp3 | Music | Snoop Dogg | album2 |
| track2 | mp3 | Music | Snoop Dogg | album2 |
| Software1 | Documents | Bills | - | |
| Software2 | Documents | Bills | - | |
| FileHierarchy2csv | md | Documents | Documentation | - |
- Export directory structure: Exports all files from a specified directory into a CSV format.
- GUI and CLI Support: Can be used with or without a graphical user interface (GUI).
- File and Extension Filters: Allows filtering by file names and extensions using a delimiter-separated list.
- Recursive Levels: Limit the depth of directory levels to be exported or analyze the full depth. Each directory name will be exported in the level field.
- Log System: Logs actions and warnings both in the console and to an optional log file and gui messages.
Some Parameters are required on execution.
The script can be customized using the following parameters:
| Parameter | Type | Default Value | Description |
|---|---|---|---|
-nogui |
Switch | false |
Runs the script without GUI if set. |
-sourcePath |
String | N/A | Path of the source folder to be exported. |
-exportFile |
String | N/A | File path where the CSV output should be saved. |
-ignore_FileNames |
String | "" (Empty) |
Delimiter-separated ; list of file names to ignore. |
-ignore_FileExtensions |
String | "csv" |
Delimiter-separated list of file extensions to ignore (e.g., "txt;doc"). |
-levels |
Integer | 0 (Unlimited) |
Limits the depth of recursion when exporting directory structure. |
Handles logging to both console and file. It supports different log levels (INFO, WARNING, etc.) and can also show messages in a dialog box in the GUI mode.
log_message: The message to log.log_level: Log severity (INFO, WARNING, etc.).log_file: Path to a log file (optional).log_gui: Iftrue, displays the log message in a message box (GUI).log_status: Iftrue, the log will be written to a file.
The core function that handles file export. It recursively scans the source directory and writes the directory structure and file details to the specified CSV file, applying the filters for ignored file names and extensions.
sourcePath: Path of the source folder to be exported.exportFile: Destination CSV file path.ignore_FileNames: List of file names to ignore.ignore_FileExtensions: List of file extensions to ignore.
Ensures the user input for the levels parameter is a valid integer. Logs a warning if not.
intvarcheck: The input value to check.
Builds and displays the graphical user interface. The GUI allows users to select a source folder and export path, configure filters, and initiate the export process.
- Text boxes for source path, export path, levels, file name filters, and extension filters.
- Buttons for source and export path selection.
- Labels for various settings and statuses.
- A status display showing the current state of the script (e.g., running, completed).
The entry point function that decides whether to run the script in GUI or command-line mode based on the nogui switch. If running in non-GUI mode, it verifies necessary parameters and calls export_structure. In GUI mode, it launches the form using load_gui.
If you want to run the script in a non-GUI mode, simply use the -nogui switch and provide the necessary parameters like source path and export file.
powershell.exe -File FileHierarchy2csv.ps1 -nogui -sourcePath "C:\MySource" -exportFile "C:\export.csv" -ignore_FileExtensions "csv;tmp"If you omit the -nogui switch, the script will run with the graphical user interface:
powershell.exe -File FileHierarchy2csv.ps1The GUI will prompt you to select the source folder, export file path, levels, and filters.
A Double Click on this script should also start the script. Keep in mind it may be neccessary to enable Powershell scripts on your device.
Logs are saved in a file named FileHierarchy2csv.log.txt by default, located in the current working directory. Log entries include timestamps, log levels, and detailed information about the script's execution.
- Levels Parameter: If
levelsis set to0, the script automatically determines the depth of recursion based on the deepest subfolder in the source path. - Filters: The script allows file names and extensions to be ignored during the export process. These filters are specified as semicolon-separated lists.
- .NET Assemblies for GUI:
System.Windows.FormsSystem.DrawingPresentationCorePresentationFramework
These are required if the script is run with the GUI. The dependencies should be loaded automatically because the script uses powershell default modules
- If the source or export paths are not provided in non-GUI mode, the script will terminate with a warning.
- Ensure that the
levelsparameter is a valid integer. If not, a warning will be logged and the process will not start.
