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?
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 thatGet-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):