0
GetChildItem "C:\foo\bar" -Recurse -File | where { $_.Name -notmatch '[a-zA-Z_0-9^0-9]*(.pl|.xap|.go|.zip|.py|.tar|.gz|.tgz)' } | Compress-Archive -DestinationPath "C:\archive\xyz.zip"

When I execute the GetChildItem command without output redirection, the command is returning exactly what I need. However when I redirect the output to Compress-Archive, this cmdlet does not preserve the directory structure (all files are archived at the root) of \bar and does not include all of the files when the GetChildItem command is executed in isolation.

Would appreciate any input. I would typically use unix shell command(s), however I am restricted by my environment.

Update (3/4/19):

GetChildItem "C:\foo\bar" -Recurse | where { $_.Name -notmatch '[a-zA-Z_0-9^0-9]*(.pl|.xap|.go|.zip|.py|.tar|.gz|.tgz)' } | Compress-Archive -DestinationPath "C:\archive\xyz.zip"

Removing the -File switch/argument resolves the folder structure issue, but files that I want to ignore in the -notmatch switch are no longer ignored. Given the following example:

/archive
    -/a
       -file.pl
    -/b
       -file.py
       -file1.html
    -index.html
    -some_junk.zip

This command will produce the following output:

VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "
C:\archive\a
C:\archive\b".
VERBOSE: Adding 'C:\archive\a\file.pl'.
VERBOSE: Adding 'C:\archive\b\file.py'.
VERBOSE: Adding 'C:\archive\b\file1.html'.
VERBOSE: Adding 'C:\archive\index.html'.
VERBOSE: Adding 'C:\archive\some_junk.zip'.

In this case the .py, .zip, and .pl files are being included in the .zip

unjankify
  • 190
  • 9
  • I would suggest changing all curly 'smart-quotes' to straight ones. You have probably copied this from some internet page or modified it in Word? – Theo Mar 02 '19 at 13:19
  • @Theo: I am going to assume you mean the use of `'` vs `\`` Realized I wrote this post on my phone which inserted the smart-quotes. I updated the post. – unjankify Mar 02 '19 at 15:55
  • ..I stil see smart-quotes around `“C:\foo\bar”` and `“C:\archive\xyz.zip”`. Untested, but perhaps by removing the `-File` switch from the `Get-ChildItem` cmdlet will allow for also sending the folder objects through the pipeline? Now it just sends file objects to `Compress-Archive`. You can test by appending a `-WhatIf` switch at the very end. – Theo Mar 03 '19 at 11:33
  • @Theo: Thanks for the -WhatIf flag, will come in handy. When I removed the -File argument, the entire directory is sent to Compress-Archive and does not ignore any files under that directory. – unjankify Mar 04 '19 at 16:21
  • I don't think your regex does what you expect of it. Maybe try `[a-z_0-9^]+(\.pl|\.xap|\.go|\.zip|\.py|\.tar|\.gz|\.tgz)$` – Theo Mar 04 '19 at 16:28
  • Tried it, still the same. – unjankify Mar 04 '19 at 16:36
  • Ended up switching to a unix distribution and using the `zip` binary/package with the `-x` flag – unjankify Mar 04 '19 at 21:48

0 Answers0