windowspowershellcommand-line

A PowerShell script to list all files and folders within a directory structure to a text file


PowerShell Version 5.1.19041.6093

this script creates the text file with the structure of all folders and files inside but also give this error.

PS D:\Csharp Projects\Slicer> # Get the current folder
>> $rootPath = Get-Location
>> $outputFile = Join-Path $rootPath "FolderStructure.txt"
>>
>> function Get-FolderTree {
>>     param (
>>         [string]$path,
>>         [int]$level = 0
>>     )
>>
>>     $indent = " " * ($level * 4)
>>
>>     if ($level -eq 0) {
>>         # Safe root name extraction
>>         $rootName = Split-Path -Leaf $path
>>         if ([string]::IsNullOrWhiteSpace($rootName)) {
>>             $rootName = $path  # fallback for root drives like D:\
>>         }
>>         Add-Content -Path $outputFile -Value "$rootName\"
>>     }
>>
>>     # Get folders
>>     Get-ChildItem -Path $path -Directory -Force | Sort-Object Name | ForEach-Object {
>>         Add-Content -Path $outputFile -Value "$indent├── $($_.Name)\"
>>         Get-FolderTree -path $_.FullName -level ($level + 1)
>>     }
>>
>>     # Get files
>>     Get-ChildItem -Path $path -File -Force | Sort-Object Name | ForEach-Object {
>>         Add-Content -Path $outputFile -Value "$indent│   $($_.Name)"
>>     }
>> }
>>
>> # Clear output if it already exists
>> if (Test-Path $outputFile) {
>>     Remove-Item $outputFile
>> }
>>
>> # Run the scan
>> Get-FolderTree -path $rootPath.FullName
>> Write-Host "`n?? Folder structure saved to: $outputFile"

in the end it gives this error

Split-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:15 char:38
+         $rootName = Split-Path -Leaf $path
+                                      ~~~~~
    + CategoryInfo          : InvalidData: (:) [Split-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Spli
   tPathCommand

and after it this message:

 Folder structure saved to: D:\Csharp Projects\Slicer\FolderStructure.txt

and if i edit the text file i see the full structure of all the files folders.

but how can i fix the error? even if it's creating the text file.


Solution

  • That happens because you obtain $rootPath from Get-Location, which returns a System.Management.Automation.PathInfo, not a System.IO.DirectoryInfo, and the PathInfo has no property FullName (which you use when you call the function); you need to use the property Path instead.
    You should also pass the FileSystem PSProvider in Get-Location, because your current location could also be HKCU:\ or other ones.
    So get the location like this:
    $rootPath = Get-Location -PSProvider FileSystem
    And call the function like this:
    Get-FolderTree -Path $rootPath.Path

    On a side note: The parameters in your function should be in Pascal Case (only local variables should use camel case):

        param (
            [string]$Path,
            [int]$Level = 0
        )
    

    See here for details: Strongly Encouraged Development Guidelines > Use Pascal Case for Parameter Names
    https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines?view=powershell-7.5#use-pascal-case-for-parameter-names