Get-ChildItem and wildcards and filtering

6.4k Views Asked by At

I have two different ways of getting files with a wildcard pattern:

Get-ChildItem "$ActivityLogDirectory/*.csv"

and

Get-ChildItem "$ActivityLogDirectory" -Filter *.csv

I prefer to use the latter instead of the former because the former (Get-ChildItem "$ActivityLogDirectory/*.csv") has, on occasion, given me a permission denied error.

They both appear to return the same results, but when I try to compress the resulting files with this command:

Compress-Archive -Update -Path $CsvFiles -DestinationPath C:\Users\admin\Downloads\foo.zip

the former succeeds while the latter fails with the following error:

Compress-Archive : The path 'rgb dev automation store a_1-1_2194_20181120.csv'
either does not exist or is not a valid file system path.
At line:1 char:1
+ Compress-Archive -Update -Path $CsvFiles -DestinationPath C:\Users\ad ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (rgb dev automat...94_20181120.csv:String) [Compress-Archive], InvalidOperationException
    + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Compress-Archive

So what's the difference between these two ways of getting a listing of files using wildcards? Or perhaps asked another way, why does using -Filter *.csv cause the Compress-Archive cmdlet to fail?

enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

The reason you're seeing different behavior is the - obscurely situational - stringification behavior of the objects output by Get-ChildItem:

This answer details when Get-ChildItem output happens to stringify to a mere filename vs. a full path, and it so happens that Get-ChildItem "$ActivityLogDirectory" -Filter *.csv stringifies to mere filenames.

The workaround is to explicitly stringify the objects as their full paths via their FullName property (PSv3+ syntax):

$CsvFiles = (Get-ChildItem "$ActivityLogDirectory" -Filter *.csv).FullName
4
On

If you are running this from a shell with the location of the folder where the CSV files are located then this will work. What you are doing by passing the $CsvFiles variable into Compress-Archive is trying to run against the file name in the current context. To fix this pass the full path $CsvFiles.FullName:

$Csvfiles = (Get-Childitem $ActivityLogDirectory -Filter *.csv)
Compress-Archive -Update -Path $Csvfiles.fullname -DestinationPath C:\Users\admin\Downloads\foo.zip