按名称前缀对文件进行分组,然后使用 PowerShell 归档 (zip) 每个组 [英] Group files by name prefix and then archive (zip) each group with PowerShell
问题描述
我有以下情况:
我在一个目录中有大量文件,如下所示:
I have a large number of files in a directory named as thus:
'######_dynamicname_timestamp.xml'
'######_dynamicname_timestamp.xml'
我想按 ###### 将文件分组,然后将分组的文件压缩到以组的 ###### 命名的存档中.在这种情况下,文件名和时间戳并不重要,只有前六位数字.我是 PowerSHell 的新手,还不熟悉命令(cmdlet?).在 ss64 上寻找答案时遇到了 group-object 但我仍然无法正确解决.到目前为止,这是我所得到的:
I want to group the files by ###### then zip the grouped files into an archive named after ###### of the group. In this situation the filename and timestamp are not important, only the first six digits. I am new to PowerSHell and am not familiar with the commands (cmdlets?) just yet. Looking for the answer at ss64 I came across group-object however I still can't get it right. So far this is as all I got:
ls D:\directory\ | select name | group-object name
由于时间戳和动态文件名,每个文件都在自己的单独组中.我需要一种仅按前六位数字分组的方法,然后将这些组传递给 zip 或存档命令.预先感谢您的帮助.
Due to the timestamp and dynamic filename each file is in its own individual group. I need a way to group only by the first six digits then pass those groups to a zip or archive command. Thank you in advance for your assistance.
推荐答案
这个管道应该给你想要的结果/想法如何去做
This pipeline should give you the desired result / the idea how to do it
Get-ChildItem D:\directory |
Group-Object -Property { $_.Name.Substring(0, 6) } |
% { Compress-Archive -Path $_.Group -DestinationPath "$($_.Name).zip" }
这篇关于按名称前缀对文件进行分组,然后使用 PowerShell 归档 (zip) 每个组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!