2

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

Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
HairOfTheDog
  • 1,948
  • 1
  • 22
  • 28

2 Answers2

2

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
mklement0
  • 245,023
  • 45
  • 419
  • 492
1

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
Owain Esau
  • 1,710
  • 1
  • 15
  • 26
  • Your answer gets `Compress-Archive` to work, but I'd still like to know what the difference is between the two ways of using `Get-ChildItem` with wildcards – HairOfTheDog Nov 28 '18 at 23:48
  • I am not entirely sure both methods work for me. Can you see what the results are for `$CsvFiles.psstandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames` where `$CsvFiles` is populated using `Get-ChildItem "$ActivityLogDirectory" -Filter *.csv` ? – Owain Esau Nov 28 '18 at 23:58
  • Using either way of calling `Get-ChildItem` and then calling `$CsvFiles.psstandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames` returns the same results `LastWriteTime`, `Length` and `Name` – HairOfTheDog Nov 29 '18 at 00:02
  • 1
    Strange, i am getting the same results now: `What if: Performing the operation "Compress-Archive" on target " C:\test\New Microsoft Excel Workshee.csv C:\test\New Microsoft Excel Worksheet (2).csv".` When you create the variable with your first method, the full path is being passed to `Compress-Archive` with the second, it is only passing the name `Compress-Archive : The path 'New Microsoft Excel Workshee.csv' either does not exist or is not a valid file system path` – Owain Esau Nov 29 '18 at 00:07