Wednesday, October 13, 2010

PowerShell GCI (Get-ChildItem) LiteralPath Issue

After finishing ripping our CD collection to FLAC format (link) we wrote some PowerShell scripts to analyze the directory hierarchy that the FLAC (*.flac) files reside in. The hierarchy is something like this \\server\music\artist\album\track.flac where “artist”, “album”, and “track” change according the CD and track ripped. Starting at the top of the node \\server\music the scripts generate the following stats:

1. Number of album folders that do not have music (at least one .flac files). This can indicate that something went wrong and needs to be checked.

2. Number of artist folders that have no album folders in them. For housekeeping reasons it’s not necessary to have empty folders hanging around.

3. Number of .flac files that are less than 500 kb. This can indicate a potential problem with the ripping process. Some .flac files can be that small such as hidden files or silent tracks.

In the course of ripping CDs the following situation for 2+ disc collection can occur:

So we have our directory structure defined, what are the basics of the PowerShell scripts? They are .ps1 files that run at a PowerShell command prompt. The key command used in the scripts is the Get-ChildItem cmdlet, or commonly typed as just gci. The gci cmdlet operates like a directory listing. The gci cmdlet has several switches that help refine what you want to have returned like only files with certain file extensions (*.flac). The scripts work by first invoking a gci (with appropriate switches) at the root directory (\\server\music), capturing that into an array, and then iterating through that array and checking each array entry for specific criteria. The problem we ran into is that some switches of the gci cmdlet did not work well with the use of brackets “[“ and “]” in the folder name.

To make the problem we encountered more concrete, let’s suppose we have the following directory structure:

The result is that nothing is found when we are expecting that is should return the file.flac.

Similarly, if you invoke the following command:

gci -path ("\\server\music\artist1\album2") -recurse -include *.flac

The result returns file.flac as expected.

The problem with the first command is the use of brackets in the first path (folder name) which are perfectly legal to use in folder names but PowerShell interprets these as range operators as discussed in this TechNet Tip. To get around the problem of having PowerShell interpret the range character we can use the -literalpath switch of the gci cmdlet.

So invoking this command:

gci -literalpath ("\\server\music\artist1\album 1 [disc 1]")

Returns file.flac as expected. The problem of using –literalpath intead of –path is that the –include switch seems to be ignored so we can’t refine what we are looking for. One workaround for this – and the point of this post – is that you have to then iterate through the items in each album folder. The following code snippet shows a workaround that will work: