Create a 7z Archive for files created by date using Powershell

久未见 提交于 2020-01-04 06:08:22

问题


I have directory where various daily files gets copied. I need to write a power shell script which will create 7z archive based on groups of file create date.The name of the archive includes those group dates.

Example Below

FileA  06/23/11
FileB  06/23/11
FileC  06/24/11

Script should create 2 archives

062311.7z contains FileA, FileB
062411.7z contains FileC

Thanks in advance

--------- Now I have to develop a compression routine which can take care of daily/Weekly/ Monthly durations


回答1:


The first thing you need to do is group all the files via date using group-object:

$groups = dir | group-object -property {$_.LastWriteTime.Date}

then for each group, build a command string that does your zipping and execute it:

$groups | foreach{$cmd = "7z.exe a $((Get-Date $_.Name).ToString(`"MMddyy`")).7z $([string]::join(`" `", $_.Group))"; invoke-expression $cmd}

Warning, the above is not really tested (for ex. it won't work if you spaces in your filename). But hopefully, it will give you enough to proceed.



来源:https://stackoverflow.com/questions/6473973/create-a-7z-archive-for-files-created-by-date-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!