Skip to content

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

@karlronsaria

Description

@karlronsaria

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions