自动增加 ZIP 压缩的代码?

Code for automating incremented ZIP compression?

我正在尝试压缩一个包含 800 张图片的文件夹,每个 ZIP 文件只包含 10 张或更少的图片,所以我最终应该得到 80 个 ZIP 文件。如果有人知道执行此操作的 BAT 文件代码,我将不胜感激。我也不想在压缩后删除文件。

我知道我可能会使用 7-Zip,但我似乎无法在任何地方找到这个问题的答案。谢谢!

您可以使用类似于

的方式在 Powershell 中存储文件列表
$fileList = Get-Item -Path "C:\MyPhotosDir\*"

然后为 7zip 设置一个别名

set-alias sz "$env:ProgramFiles-Zipz.exe"

然后按照

创建一个带有计数器的循环
$i = 1
foreach $file in $fileList 
    #Build foder name name
    $folderDir = "C:\MyPhotoArchive$($i - ($i % 10) + 1).7z"
    sz a -t7z $folderDir $file.filename
end for

我已经用 VB 写了一段时间,如果 Powershell 语法有点不对,我深表歉意。本质上应该将 10 个文件添加到 "C:\MyPhotoArchive1",将 10 个文件添加到 "C:\MyPhotoArchive2"。我已经很长时间没有使用 7zip 将文件添加到存档中,但我认为该调用仅使用 a 并且应该将文件添加到存档中,并在需要时创建一个。

尝试以下方法PowerShell

# Setup variables (Change)
$ZipFolder = "T:\YourFolder\WithFiles\ToZip"
Zip = "C:\Program Files-Zipz.exe"
$NewZipsFolder = "T:\FolderToPut\AllOfThe\ZipsIn"

# Script Variables
$pendingFiles = @() 
$fileNumber = 1

# Get a list of all the files to be zipped
Get-ChildItem $ZipFolder | sort $_.FullName | ForEach-Object { $pendingFiles += $_.FullName  }

# While there are files still to zip 
While($pendingFiles){

    # Select first 10 files to zip and zip them
    $ToZip = $pendingFiles | Select -First 10 
    & Zip "a" "$NewZipsFolder\File-$fileNumber.7z" $ToZip 

    # Remove first 10 zipped files from pending files array
    $pendingFiles = $pendingFiles | Where-Object { $ToZip -notcontains $_ } 
    $fileNumber++ 
} 

这将创建一个包含所有需要压缩的文件的列表。然后使用 7z.exe (7-zip).

将它们分批压缩为 10 个文件

注意:对于变量 $ZipFolder$NewZipsFolder,请勿在文件夹路径 (\) 上放置尾部反斜杠。