0

I've seen variations of this question answered, but typically using something like 7zip. I'm trying to find a solution that will work with the capabilities that come with windows absent any additional tools.

I have a directory that contains several hundred subdirectories. I need to individually compress each subdirectory....so I'll wind up with several hundred zip files, one per subdirectory. This is on a machine at work where I don't have administrative privileges to install new software...hence the desire to stay away from 7zip, winRar, etc.

If this has already been answered elsewhere, my apologies...

jerH
  • 1,085
  • 1
  • 12
  • 30

3 Answers3

3

Never tried that myself, but there is Compress-Archive:

The Compress-Archive cmdlet creates a zipped (or compressed) archive file from one or more specified files or folders. An archive file allows multiple files to be packaged, and optionally compressed, into a single zipped file for easier distribution and storage. An archive file can be compressed by using the compression algorithm specified by the CompressionLevel parameter.

Because Compress-Archive relies upon the Microsoft .NET Framework API System.IO.Compression.ZipArchive to compress files, the maximum file size that you can compress by using Compress-Archive is currently 2 GB. This is a limitation of the underlying API.

Here's a sample script I just hacked together:

# configure as needed
$source = "c:\temp"
$target = "d:\temp\test"

# grab source file names and list them
$files = gci $source -recurse
$files

# target exists?
if( -not (test-path $target)) {
    new-item  $target  -type directory  
}

# compress, I am using -force here to overwrite existing files
$files | foreach{
    $dest = "$target\" + $_.name + ".zip"
    compress-archive  $_  $dest  -CompressionLevel Optimal  -force
}

# list target dir contents
gci $target -recurse

You may have to improve it a bit when it comes to subfolders. In the above version, subfolders are compressed as a whole into a single file. This might not exactly be what you want.

JensG
  • 13,148
  • 4
  • 45
  • 55
  • So as someone with little to no windows scripting experience, how might this be embedded in a script that would list the parent directory contents and iterate through all the subdirectories? – jerH Dec 21 '18 at 14:19
  • 1
    The link you provided refers to powershell 6.0, On that page on the left side you can choose the PSversion and see if the cmdlet is supported - Compress-Archive requires PSv5.0+ –  Dec 21 '18 at 14:20
  • Think I may have found the looping part - https://stackoverflow.com/questions/18847145/loop-through-files-in-a-directory-using-powershell will advise.... – jerH Dec 21 '18 at 14:29
2
Get-ChildItem c:\path\of\your\folder | ForEach-Object {
    $path = $_.FullName
    Compress-Archive -Path $path -DestinationPath "$path.zip"
}

I put this, as a quick snippet. Don't hesitate to comment if this does not fit with your request. In a folder X, there are subfolders Y1, Y2...

Y1.zip, Y2.zip... will be created.

Yukeer
  • 345
  • 1
  • 2
  • 11
0

use PowerShell go the the path that you would like to compress, do:

$folderlist = Get-ChildItem "."
foreach ($Folder in $folderlist) { Compress-Archive -path $Folder.Name -destinationPath "$($Folder.Name).zip"}
Xinyo Chen
  • 26
  • 3