Square Brackets and Recurse Parameters and Pipelines, Oh MY!

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Tony Pagliaro Tony Pagliaro 6 months, 4 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36027
    Profile photo of Tony Pagliaro
    Tony Pagliaro
    Participant

    Cranking through this Get-ChildItem-based script, everything going well, very simple. Output looks good, except one subfolder reports empty and I know it's not. Figured out it was caused by a recurse parameter. I played around a bit, the issue didn't happen when the folder name didn't contain the dreaded square-brackets. I dug deeper and found this post, which makes me sorta give up on me figuring this out by myself.

    How can I re-write this GCI script to handle the case where it finds a folder name with any windows-legal folder name character? (walk-thru below)

    $ConvertDir = 'F:\Transients\Complete\__needs convert\' 
    
    [string[]]$VideoTypes = @(
        '.mp4',
        '.mkv',
        '.m4v',
        '.avi',
        '.wmv',
        '.mpg',
        '.mpeg'
    )
    [string[]]$ImageTypes = @(
        '.bmp',
        '.jpg',
        '.jpeg',
        '.png',
        '.tif',
        '.tiff',
        '.gif',
        '.pdf',
        '.fpx'
    )
    
    $Results = $null
    $Results = New-Object system.Data.DataTable 
    $Results.Columns.Add("Folder")           | Out-Null
    $Results.Columns.Add("Video Count")      | Out-Null
    $Results.Columns.Add("Total Size (GB)")  | Out-Null
    $Results.Columns.Add("Image Count")      | Out-Null
    $Results.Columns.Add("File Count")       | Out-Null
    
    gci -Path $ConvertDir -Directory | foreach -Process {
            $Files = gci -LiteralPath $_.FullName -Recurse -File 
            $Images = $Files | Where {$_.Extension -in $ImageTypes} 
            $Videos = $Files | Where {$_.Extension -in $VideoTypes} 
    
            $Results.rows.add(
                                $_.Name, 
                                $Videos.Count, 
                                [math]::Round((($Files |
                                        measure length -sum |
                                        select -expand Sum)/1GB),2), 
                                $Images.Count,
                                $Files.Count
                             ) |
                Out-Null
        }
    
    $prop1 = @{Expression='Video Count'; Descending=$true }
    $prop2 = @{Expression='Total Size (GB)'; Descending=$true }
    
    $Results | sort $prop1, $prop2 |ft -AutoSize
    

    Basically, I'm searching for video files under the given path and my goal is to have an overview so I can determine which subfolder to clean up on any given day I might have time to dedicate to this task.
    The first variable would become a parameter one day, but for now this script is single use, so the path is hard coded.
    Then I define commonly-used media file extensions for use in a filter.
    Then I define my table properties.
    The the meat/potatoes: I use a foreach loop with literalpath parameters and pipelines (which I believe is my area of issue) to pull info for all recursive files and store them in variables for use in my display table.

    The result with the square brackets in the SPACEBALLS folder name:

    `
    PS D:\ps> D:\PS\audit to convert folder\Get-BiggestPendingConvertGroup.ps1
    
    Folder                    Video Count Total Size (GB) Image Count File Count
    ------                    ----------- --------------- ----------- ----------
    _no audio or video        3           4.16            1           4         
    _studder                  1           5.71            1           2         
    _subtitles                1           0.68            0           2         
    _sideways                 1           0.03            0           1         
    _will not play            0           0               0           0         
    _funky ff-rw              0           0               0           0         
    Spaceballs (1987) [1080p] 0           0               0           0         
    

    The result with the square brackets removed from the SPACEBALLS folder name:

    PS F:\> D:\PS\audit to convert folder\Get-BiggestPendingConvertGroup.ps1
    
    Folder                  Video Count Total Size (GB) Image Count File Count
    ------                  ----------- --------------- ----------- ----------
    _no audio or video      3           4.16            1           4         
    _studder                1           5.71            1           2         
    Spaceballs (1987) 1080p 1           1.5             1           3         
    _subtitles              1           0.68            0           2         
    _sideways               1           0.03            0           1         
    _will not play          0           0               0           0         
    _funky ff-rw            0           0               0           0         
    

    Also fun fact, this is the only post on this entire site with the word SPACEBALLS. 😀

    #36028
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I haven't been able to reproduce the problem. You're using the -LiteralPath parameter on Get-ChildItem, which is exactly what you should be doing to handle those sorts of characters. What version of PowerShell are you running?

    #36075
    Profile photo of Tony Pagliaro
    Tony Pagliaro
    Participant

    This was done using 3.0. Win Home server 2011 SP1.
    Thanks for basically telling me I'm not crazy. I was up kinda late last night though.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.