【问题标题】:PowerShell script to archive all old files in a directory based of monthPowerShell脚本将所有旧文件存档在基于月份的目录中
【发布时间】:2020-02-06 16:16:10
【问题描述】:

使用 PowerShell 我需要归档过去 7 年的所有历史文件,以根据创建日期的月份和年份进行压缩,并在压缩后删除文件。

实施解决方案后,我应该会看到如下所示的文件 示例:

FolderName_2013-Jan.zip
FOlderName_2013-Feb.zip
FOlderName_2013-Mar.zip
FOlderName_2013-Jan.zip
FOlderName_2017-Jan.zip
FOlderName_2017-Dec.zip

我找到了几篇文章,但它们缺少动态传递每个月的开始和结束的信息(例如:2013-01-01 到 2013-01-31 或 2015-06-01 到 2015-06-30 ) 过滤 2013 年以来所有月份的文件。

非常感谢任何帮助。

【问题讨论】:

标签: powershell


【解决方案1】:

到目前为止,您尝试过什么吗?请按照以下步骤操作...

  • 使用Get-ChildItemSelect-Object -Unique 获取唯一的创建日期
  • 获取所有文件并将它们存储在一个变量中
  • ForEach date 使用 Where-Object 过滤文件并将它们通过管道传输到 Compress-Archive

在你的水平上尝试它,但仍然无法弄清楚然后参考这个gist

【讨论】:

  • 谢谢 Kiran 会尝试利用您的建议。
  • 我必须通过循环遍历存档目录中的所有子文件夹,在这些单个文件所在的同一文件夹中创建这些 zip 文件。希望我必须为每个执行嵌套。
  • 你提到这个gist了吗?
  • 嗨 Kiran 我确实看过代码,但由于代码中使用 CreationTime 的方式,代码没有压缩所有文件。
【解决方案2】:

使用上面 Kiran 建议的Get-ChildItem 命令,您可以生成 FileInfo 和 DirectoryInfo 对象。其中每一个都有存储在各种属性中的文件和/或目录的时间戳,例如LastWriteTimeLastAccessTimeCreationTime。这些属性中的每一个都包含[DateTime] 类型的数据。一旦你的每个文件/文件夹都有一个[DateTime] 项目,你就可以这样做:

#Set variable $MY to be 7 years ago
$Now = (Get-Date)
$MY = $Now.AddYears(-7)

#Write custom Get-ChildItem statements to put list of files/folders in $AllItems variable

#Now, loop through with each month/year
Do
    #Filter $AllItems for the proper Month-Year
    $ThisMonthsItems = $AllItems.Where({$_.LastWriteTime.Month -eq $MY.Month -and $_.LastWriteTime.Year -eq $MY.Year})

    #Use $ThisMonthsItems to add items to your .zip file
    #This step left for you to fill with your compression routines

    $MY = $MY.AddMonths(1)
Loop Until ($MY.Month -eq $Now.Month -and $MY.Year -eq $Now.Year)

您可能需要调整循环以获得正确的时间窗口,但基本的想法应该适合您。

祝你好运

【讨论】:

    【解决方案3】:
    function Compress-Files {
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # Sourcepath
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        $SourcePath,
    
        # number of months to compress in past
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, Position = 2)]
        $Months
    
    )
    
    #region variables
    $FullPath = $SourcePath + '*.*'
    <#
        $date = (get-date).ToString("yyyy-MM-dd")
        $yesterday = (get-date).AddDays(-1).ToString("yyyyMMdd")
        $lastmonth = (get-date).AddMonths(-1).ToString("yyyyMMdd")
        $lastyear = (get-date).AddYears(-1).ToString("yyyyMMdd")
    #>
    #endregion
    
    
    #region Compress months
    #Compress files based on months in the past.
    $Months = '-' + $Months
    $Month = (get-date).AddMonths($Month).ToString("yyyyMMdd")
    
    #$files = Get-ChildItem $FullPath | ? {$_.CreationTime -ge $Months -and $_.CreationTime -le $lastmonth}
    #$files = Get-ChildItem $FullPath | ? {$_.CreationTime.ToString("yyyyMMdd") -ge $Months -and $_.CreationTime.ToString("yyyyMMdd") -le $lastmonth}
    
    $files = Get-ChildItem -Path $FullPath | ? { $_.LastWriteTime.ToString("yyyyMMdd") -le $Month -and $_.Name -notlike "*.zip" -and $_.Mode -notlike "d*" }
    
    
    
    foreach ($file in $files) {
        $filename = ($file.LastWriteTime).ToString("yyyy-MM")
        $DestinationPath = $SourcePath + $filename + '.zip'
        $file | Compress-Archive -DestinationPath $DestinationPath -update  
        Remove-Item $file -Force
        #if ($delete){ }
    
    
        ###Move Archiving logs to a text file
        "$DestinationPath|$file " | Out-File  "C:\temp\ExtractsArchive\Archivelog.txt" -Append
    }
    #>
    

    }

    #####Finally use the function on each and every sub directory
    

    $RootFileDirectory = "C:\temp\ExtractsArchive\"

    $Directories = Get-ChildItem -Path $RootFileDirectory -Recurse -Directory |选择全名

    ForEach ($Directory IN $Directories) {

    $SourcePath = $Directory.FullName.ToString() + "\"
    
    #$SourcePath.GetType()
    
    Compress-Files -SourcePath  $SourcePath -Months 2
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多