[英]How can I loop through specific file names with the Compress-Archive command in Windows PowerShell?
I am not too familiar with Windows PowerShell (and Windows console commands in general), but I wish to write a batch file which can create sperated .zip archives from specific files in a folder, then delete all but the .zip files. 我对Windows PowerShell(以及一般的Windows控制台命令)不太熟悉,但我希望编写一个批处理文件,可以从文件夹中的特定文件创建已编译的.zip存档,然后删除除.zip文件之外的所有文件。 So, I have a folder as "C:\\myfolder\\" and some files in it like: myfile0001.a myfile0001.b myfile0001.c myfile0002.a myfile0002.b myfile0002.c myfile0003.a myfile0003.b myfile0003.c ... and so on.
所以,我有一个文件夹“C:\\ myfolder \\”,其中的一些文件如:myfile0001.a myfile0001.b myfile0001.c myfile0002.a myfile0002.b myfile0002.c myfile0003.a myfile0003.b myfile0003.c .. 。 等等。
I want a create a .zip for every myfileXXXX.a + myfileXXXX.b + myfileXXXX.c package and name the .zips as the file names (for example myfile0001.zip would contain myfile0001.a + myfile0001.b + myfile0001.c). 我想为每个myfileXXXX.a + myfileXXXX.b + myfileXXXX.c包创建一个.zip,并将.zips命名为文件名(例如myfile0001.zip将包含myfile0001.a + myfile0001.b + myfile0001.c) 。
I know that I can use this code to create each .zip archive one-by-one: 我知道我可以使用此代码逐个创建每个.zip存档:
powershell -Command "& {Compress-Archive -Path C:\myfolder\myfile0001.* -CompressionLevel Optimal -DestinationPath C:\myfolder\myfile0001.zip}"
And this code working fine to delete all but the .zip archives 并且此代码可以正常删除除.zip档案之外的所有内容
powershell -Command "& {Remove-Item C:\myfolder\*.* -Exclude *.zip}"
What I cannot solve is to create a for cycle which can loop through all myfileXXXX.* and create a myfileXXXX.zip using the XXXX as the increasing value. 我无法解决的是创建一个for循环,它可以遍历所有myfileXXXX。*并使用XXXX作为增加值创建一个myfileXXXX.zip。
I cannot still test this as I am not on a windows host now, but something like this. 我现在还不能测试这个,因为我现在不在Windows主机上,但是这样的话。 It needs to be saved as a
.cmd
or .bat
file: 它需要保存为
.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}"
)
In PowerShell, you could do: 在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
}
Writing it as commandline gets a little ugly, but here you go: 把它写成命令行有点难看,但是你走了:
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}}"
How's this? 这个怎么样? (Just use powershell) Zip files based on the basename.
(只需使用powershell)基于basename的Zip文件。 .zip automatically gets added to the archive filename.
.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'.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.