如何在 Windows PowerShell 中使用 Compress-Archive 命令遍历特定文件名?
How can I loop through specific file names with the Compress-Archive command in Windows PowerShell?
我不太熟悉 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 并将 .zips 命名为文件名(例如 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 循环,并创建一个 myfileXXXX.zip 使用 XXXX 作为递增的值。
我现在还不能测试这个,因为我现在不在 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}"
)
在 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}}"
这个怎么样? (只需使用 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'.
我不太熟悉 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 并将 .zips 命名为文件名(例如 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 循环,并创建一个 myfileXXXX.zip 使用 XXXX 作为递增的值。
我现在还不能测试这个,因为我现在不在 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}"
)
在 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}}"
这个怎么样? (只需使用 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'.