0

我对 Windows PowerShell(和一般的 Windows 控制台命令)不太熟悉,但我希望编写一个批处理文件,它可以从文件夹中的特定文件创建分散的 .zip 存档,然后删除除 .zip 文件之外的所有文件。所以,我有一个文件夹为“C:\myfolder\”,其中有一些文件,如:myfile0001.a myfile0001.b myfile0001.c myfile0002.a myfile0002.b myfile0002.c myfile0003.a myfile0003.b myfile0003.c .. 。 等等。

我想为每个 myfileXXXX.a + myfileXXXX.b + myfileXXXX.c 包创建一个 .zip 并将 .zip 命名为文件名(例如 myfile0001.zip 将包含 myfile0001.a + myfile0001.b + myfile0001.c) .

我知道我可以使用此代码逐个创建每个 .zip 存档:

powershell -Command "& {Compress-Archive -Path C:\myfolder\myfile0001.*  -CompressionLevel Optimal -DestinationPath C:\myfolder\myfile0001.zip}"

这段代码可以很好地删除除 .zip 档案之外的所有文件

powershell -Command "& {Remove-Item C:\myfolder\*.* -Exclude *.zip}"

我无法解决的是创建一个可以循环遍历所有 myfileXXXX.* 的 for 循环,并使用 XXXX 作为递增值创建一个 myfileXXXX.zip。

4

3 回答 3

0

我仍然无法测试这个,因为我现在不在 Windows 主机上,但类似这样。它需要保存为.cmd.bat文件:

@echo off
 for %%i in (C:\myfolder\*) do (
    powershell -Command "& {Compress-Archive -Path C:\myfolder\%%~ni.*  -CompressionLevel Optimal -DestinationPath C:\myfolder\%%~ni.zip}"
)
于 2019-05-22T13:05:58.317 回答
0

在 PowerShell 中,您可以执行以下操作:

Get-ChildItem -Path 'C:\myfolder' -File | Where-Object { $_.BaseName -match 'myfile\d{4}' } | Group-Object BaseName | ForEach-Object {
    $target = Join-Path -Path ($_.Group[0].DirectoryName) -ChildPath ('{0}.zip' -f $_.Group[0].BaseName)
    $_.Group | Compress-Archive -CompressionLevel Optimal -DestinationPath $target
    $_.Group | Remove-Item -Force
}

把它写成命令行有点难看,但你去吧:

powershell -Command "& {Get-ChildItem -Path 'C:\myfolder' -File | Where-Object { $_.BaseName -match 'myfile\d{4}' } | Group-Object BaseName | ForEach-Object { $target = Join-Path -Path ($_.Group[0].DirectoryName) -ChildPath ('{0}.zip' -f $_.Group[0].BaseName); $_.Group | Compress-Archive -CompressionLevel Optimal -DestinationPath $target; $_.Group | Remove-Item -Force}}"
于 2019-05-22T13:21:40.467 回答
0

这个怎么样?(只需使用 powershell)基于基本名称的 Zip 文件。.zip 会自动添加到存档文件名中。

powershell "ls | foreach { compress-archive $_ $_.basename -update -verbose }"


VERBOSE: The archive file path 'C:\users\js\foo\file1' supplied to the DestinationPath patameter does not include .zip extension. Hence .zip is
 appended to the supplied DestinationPath path and the archive file would be created at 'C:\users\js\foo\file1.zip'.
VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "C:\users\js\foo\file1.1".
VERBOSE: Adding 'C:\users\js\foo\file1.1'.
VERBOSE: The archive file path 'C:\users\js\foo\file1' supplied to the DestinationPath patameter does not include .zip extension. Hence .zip is
 appended to the supplied DestinationPath path and the archive file would be created at 'C:\users\js\foo\file1.zip'.
VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "C:\users\js\foo\file1.2".
VERBOSE: Adding 'C:\users\js\foo\file1.2'.
VERBOSE: The archive file path 'C:\users\js\foo\file1' supplied to the DestinationPath patameter does not include .zip extension. Hence .zip is
 appended to the supplied DestinationPath path and the archive file would be created at 'C:\users\js\foo\file1.zip'.
VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "C:\users\js\foo\file1.3".
VERBOSE: Adding 'C:\users\js\foo\file1.3'.
于 2019-05-22T14:38:42.310 回答