简体   繁体   English

在Powershell V4中按日期对文件夹进行分组

[英]Archive folders group by date in powershell V4

I'm looking to archive files in a path that are older than X number of day. 我希望将文件归档的日期早于X天。 I have a folder with a years worth of files and the end goal is to Archive items based on the moth. 我有一个文件夹,里面存放了数年的文件,最终目的是根据蛾来存档项目。 So i'll have a folder for each month. 所以我每个月都有一个文件夹。

$SourcePath = "C:\users\StackoverFlow\Downloads"

$maxdays ="-30"

$CurrentDate = Get-Date

$ArchiveDate = $CurrentDate.AddDays($maxdays)

$destination = "C:\users\StackoverFlow\Desktop\Downloads.Zip"

$groups = Get-ChildItem $SourcePath | 
    Where-Object { ($_.LastWriteTime -lt $ArchiveDate) -and ($_.psIsContainer -eq $false) } | 
    group {"'{0}\{1}\{2:D2}'" -f $_.CreationTime}

ForEach ($group in $groups) {
    ForEach($file in $group.Group){
       Add-Type -assembly "system.io.compression.filesystem"
     [io.compression.zipfile]::CreateFromDirectory($SourcePath, $destination) 
    }
}

I was hoping that the RegEx would do this, but it doesn't seem to do anything.. 我希望RegEx可以做到这一点,但似乎什么也没做。

Suggestions? 有什么建议吗?

If you want to group them by Month and Year, make sure your format string represents just those values. 如果要按月和年对它们进行分组,请确保格式字符串仅代表这些值。

The -f operator supports standard date time formatting, so you could do: -f运算符支持标准日期时间格式,因此您可以执行以下操作:

Group-Object {"{0:MMyyyy}" -f $_.CreationTime}

To zip the grouped files into individual archives, you'll have to moved them into individual folders first: 要将分组的文件压缩到单个存档中,您必须首先将它们移动到单个文件夹中:

Add-Type -assembly "system.io.compression.filesystem"

$SourcePath = "C:\users\StackoverFlow\Downloads"
$destination = "C:\users\StackoverFlow\Desktop\{0}"

$maxdays = -30
$CurrentDate = Get-Date

$ArchiveDate = $CurrentDate.AddDays($maxdays)

$groups = Get-ChildItem $SourcePath | 
    Where-Object { ($_.LastWriteTime -lt $ArchiveDate) -and ($_.psIsContainer -eq $false) } | 
    Group-Object { "{0:MMyyyy}" -f $_.CreationTime }

# Create a temporary working dir
$TmpDirPath = Join-Path $([System.IO.Path]::GetTempPath()) $([System.IO.Path]::GetRandomFileName())
$TmpDirectory = New-Item -Path $TmpDirPath -ItemType Directory 

ForEach ($group in $groups) {
    # Create a new directory for the group
    $GroupDirectory = New-Item -Path (Join-Path $TmpDirectory.FullName -ChildPath $group.Name) -ItemType Directory
    # Move files into the new directory
    $group.Group | Move-Item -Destination $GroupDirectory.FullName

    # Create the month-specific archive
    [System.IO.Compression.ZipFile]::CreateFromDirectory($GroupDirectory.FullName, ($destination -f $group.Name)) 
}

This will create a Zip archive per month. 这将每月创建一个Zip存档。
If you want it all in one zip file, move the CreateFromDirectory call outside the loop and target the top-level temporary directory we created: 如果您希望将其全部保存在一个zip文件中,请将CreateFromDirectory调用移到循环外,并定位我们创建的顶级临时目录:

$destination = "C:\users\StackoverFlow\Desktop\Downloads.zip"
# ...
foreach ($group in $groups) {
    # Create a new directory for the group
    $GroupDirectory = New-Item -Path (Join-Path $TmpDirectory.FullName -ChildPath $group.Name) -ItemType Directory
    # Move files into the new directory
    $group.Group | Move-Item -Destination $GroupDirectory.FullName

}
[System.IO.Compression.ZipFile]::CreateFromDirectory($TmpDirectory.FullName, $destination) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM