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.
tree /Finstead? 🤔Get-PSTreeif you need a more detailed view ;)