PSFolderSize icon indicating copy to clipboard operation
PSFolderSize copied to clipboard

Get-FolderSize using FolderName parameter fails when a name contains regex symbols

Open karlronsaria opened this issue 3 years ago • 1 comments

Hi!

I love this tool, and I've been using it for a while now. I ran into a problem when I tried using it on my Program Files. When I enter

Get-FolderSize -FolderName Notepad++

I get the error

parsing "Notepad++" - Nested quantifier +.
At C:\Program Files\WindowsPowerShell\Modules\PSFolderSize\1.6.9\Functions\Private\Get-FolderList.ps1:58 char:21
+                     ($_.BaseName -match "$FolderName") -and
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

I noticed this is caused by lines 54 and 65 in PSFolderSize/PSFolderSize/Functions/Private/Get-FolderList.ps1.

($_.FullName -match ".+$FolderName.+")   -and 
($_.BaseName -match "$FolderName") -and 

I was able to fix it by using [Regex]::Escape, recommended in https://stackoverflow.com/questions/23651862/powershell-how-to-escape-all-regex-characters-from-a-string.

($_.FullName -match (".+" + [Regex]::Escape($FolderName) + ".+")) -and 
($_.BaseName -match [Regex]::Escape($FolderName)) -and 

Thank you for your time.

karlronsaria avatar Mar 28 '21 18:03 karlronsaria

I will work on adding this in, thanks for the report!

gngrninja avatar Apr 09 '21 16:04 gngrninja