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

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

Answer1:

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}

<strong>Warning</strong>, 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.

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Create a 7z Archive for files created by date using Powershell