locked
Can we add a filter with compress-Archive comdlet RRS feed

  • Question

  • Hi,

    I was looking into Compress-Archive cmdlt. I have a big folder (200 - 300MB). I want to zip it using power-shell as a part of a solution I am developing. I have to skip some type of file e.g. *.log and *.exe I don't want to be in the zip file. Anybody please suggest how I can achieve this. Do we have any straight option for this or any alternate way to achieve this?

    Thanks & Regards,
    Girish

    Monday, November 27, 2017 3:41 PM

Answers

  • First:  don't use line extenders for obvious reasons.
    Next: use exclude on command for performance
    Also: avoid unnecessary quotes
    Finally: format code for easy readability.

    Get-ChildItem -Path D:\* -Recurse -File -Exclude *.log,*.exe | 
        Where-Object {$_.Directory -notmatch 'folder1|folder2|folder3' } |
        Compress-Archive -DestinationPath D:\file.zip
    


    \_(ツ)_/

    Wednesday, November 29, 2017 8:17 AM

All replies

  • How about piping get-childitem to it?

    Get-ChildItem -recurse -exclude *.log,*.exe | Compress-Archive -DestinationPath file.zip

    (Edit: it doesn't preserve the path)




    • Edited by JS2010 Tuesday, November 28, 2017 3:14 PM
    Monday, November 27, 2017 3:48 PM
  • Get-CHhildItem will not work well under some circumstances.  Use the following. "Path" takes an array of paths and masks.

    Compress-Archive -Path d:\test\*.htm,d:\test\*.txt,d:\test\*.ps1 -DestinationPath d:\test\test.zip


    \_(ツ)_/


    Monday, November 27, 2017 4:05 PM
  • Thanks ,

    So  here we are saying that rather than excluding the specific file extensions we will include the extension that needs to be archieved. Am I right? Also what can be done if I want to exclude folders with some specific name.

    Regards,
    Girish

    Tuesday, November 28, 2017 11:14 AM
  • Hi Girish,

    >> Also what can be done if I want to exclude folders with some specific name.
    Based on my research, you can also have a try with the following demo command, please replace the value to your actual value. For your reference, hope it is helpful to you:
    Get-ChildItem -Path 'D:' -Recurse | `
    Where-Object {$_.PsIsContainer -eq $false -and $_.Extension -ne '.log' -and $_.Extension -ne '.exe' -and $_.Directory -notmatch 'excludefoldername'} | `
    Compress-Archive -DestinationPath 'D:\file.zip'

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, November 29, 2017 7:56 AM
  • First:  don't use line extenders for obvious reasons.
    Next: use exclude on command for performance
    Also: avoid unnecessary quotes
    Finally: format code for easy readability.

    Get-ChildItem -Path D:\* -Recurse -File -Exclude *.log,*.exe | 
        Where-Object {$_.Directory -notmatch 'folder1|folder2|folder3' } |
        Compress-Archive -DestinationPath D:\file.zip
    


    \_(ツ)_/

    Wednesday, November 29, 2017 8:17 AM
  • Hi jrv,

    Thank you for your suggestion, I have benefited a lot from them.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, November 29, 2017 10:03 AM
  • The  following guidelines for best formatting and code structure: https://github.com/PoshCode/PowerShellPracticeAndStyle


    \_(ツ)_/

    Wednesday, November 29, 2017 10:20 AM
  • Note that using get-childitem doesn't preserve the directory structure in the zip file. Here's a solution using a temp folder: https://www.petri.com/custom-archiving-powershell-5-0 (There's probably some nice .net way to do it.)

    • Edited by JS2010 Wednesday, November 29, 2017 3:54 PM
    Wednesday, November 29, 2017 2:41 PM