Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude a folder in compress-archive

Tags:

powershell

Can I somehow exclude a folder when I compress an archive like this?

$compress = Compress-Archive $DestinationPath $DestinationPath\ARCHIVE\archiv-$DateTime.zip -CompressionLevel Fastest

Now it always saves the whole folder structure of $destinationpath to the archive, but since the archive is in the same folder, it always gets zipped into a new archive, making the archive double in size every time I run the command.

like image 911
Balthazar Avatar asked Dec 10 '16 23:12

Balthazar


People also ask

How do I exclude files from Winrar?

You can exclude the files and folders using -x switch in the command.

How do I exclude a subfolder in PowerShell?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.

How do I exclude a zip file in Linux?

Exclude Multiple Files/Directories from Zip Archive You can define -x multiple times in a single zip command to exclude multiple files and directories from zip archive.

How do I compress a folder in a folder?

Right click the file or folder you want to compress and select "Properties". On the General tab in Properties, click the button Advanced. In the next window, tick the check box Compress contents to save disk space under the Compress or Encrypt attributes section.


2 Answers

Get all the files you want to compress, excluding the files and folders you don't want compressed and then pass that to the cmdlet

# target path
$path = "C:\temp"
# construct archive path
$DateTime = (Get-Date -Format "yyyyMMddHHmmss")
$destination = Join-Path $path "ARCHIVE\archive-$DateTime.zip"
# exclusion rules. Can use wild cards (*)
$exclude = @("_*.config","ARCHIVE","*.zip")
# get files to compress using exclusion filer
$files = Get-ChildItem -Path $path -Exclude $exclude
# compress
Compress-Archive -Path $files -DestinationPath $destination -CompressionLevel Fastest
like image 154
Nkosi Avatar answered Oct 19 '22 20:10

Nkosi


you can use -update option of Compress-Archive. Select your subdirs with Get-ChildItem and Where

like it:

$YourDirToCompress="c:\temp"
$ZipFileResult="C:\temp10\result.zip"
$DirToExclude=@("test", "test1", "test2")

Get-ChildItem $YourDirToCompress -Directory  | 
           where { $_.Name -notin $DirToExclude} | 
              Compress-Archive -DestinationPath $ZipFileResult -Update
like image 34
Esperento57 Avatar answered Oct 19 '22 19:10

Esperento57