【问题标题】:Zipping log files in Powershell filtered by datePowershell中按日期过滤的压缩日志文件
【发布时间】:2012-01-11 18:06:26
【问题描述】:

\\server\logs 下有一个子目录树,其中包含不遵循一致文件命名约定的归档日志文件。我想运行一个每日计划任务,将 90 天或更早的非压缩文件压缩为每日压缩文件。我希望在每个 zip 中维护目录树结构和文件名。

在 Powershell 中解决此问题的最佳方法是什么?

编辑:除非有更好的方法,否则我认为 zip 文件应该在 \\server\oldlogs 中创建并以特定日期命名,例如。 \\server\oldlogs\20110630_logs.zip\\server\logs 下的所有文件最后一次修改时间超过 90 天前应添加到相应的 .zip 文件中。

【问题讨论】:

  • 在 \\server\logs 下有子目录。这些子目录将包含不以 .zip 结尾的文件。该过程应识别过去 90 天内未(创建/修改/访问?)的所有文件,并将它们放入现有目录结构中的 .zip 中。创建存档文件后,应删除这些相同的文件。准确吗?
  • @billinkc - 既然你提到了它,在\\server\oldlogs 目录中创建 zip 似乎更简单。我想以一天命名的 zip 文件结束,其中包含在该日期最后修改的文件。那么是的,一旦它们被压缩,它们应该被删除。我会更新问题。谢谢。
  • 为什么要使用powershell标签?。您需要有关问题或功能脚本的意见吗?
  • @voodoomsr - 因为我要求在 Powershell 中解决这个问题的最佳方法。

标签: logging scripting powershell zip


【解决方案1】:

这是 66% 的解决方案(午餐结束)。我无法让本机 zip 与 PowerShell 一起使用,但如果您安装了 PowerShell 社区扩展,那么更换 zip 调用应该很容易 How to create a zip archive with PowerShell?

# purloined from http://blogs.inetium.com/blogs/mhodnick/archive/2006/08/07/295.aspx
# I am not smart enough to make it work. Creates an empty .zip file for me
Function ZipIt
{
    param
    (
        [string]$path
    ,   [string]$files
    )

    if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

    if (-not (test-path $path)) { 
      set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    } 

    $zipFile = (new-object -com shell.application).NameSpace($path) 
    #$files | foreach {$zipfile.CopyHere($_.fullname)} 
    foreach($file in $files)
    {
        $zipfile.CopyHere($file)
    }
}

# this script is reponsible for enumerating subfolders
# for each file it finds, it will test the age on it
# returns an array of all the files meeting criteria
Function Walk-SubFolder
{
    param
    (
        [string]$RootFolder
    )

    $searchPattern = "*.*"
    $maxAge = 90
    $now = Get-Date

    # receiver for the files
    [string[]] $agedOutFileList = @()

    foreach($file in [System.IO.Directory]::GetFiles($RootFolder, $searchPattern, [System.IO.SearchOption]::AllDirectories))
    {
        # test whether the file meets the age criteria
        $age = [System.IO.File]::GetLastWriteTime($file)
        $days = ($now - $age).Days
        if (($now - $age).Days -ge $maxAge)
        {
            # this needs to be archived
            Write-Host("$file is aged $days days")
            $agedOutFileList = $agedOutFileList + $file
        }
    }

    return $agedOutFileList
}

Function PurgeFiles
{
    param
    (
        $fileList
    )
    foreach($file in $fileList)
    {
        # this should be in a try/catch block, etc, etc
        [System.IO.File]::Delete($file)
    }
}

$sourceFolder = "C:\tmp\so"
$zipFile = "C:\tmp\so.zip"


$oldFiles = Walk-SubFolder $sourceFolder
ZipIt $zipFile $oldFiles

#This is commented out as the zip process is not working as expected
#PurgeFiles $oldFiles

我会看看我是否可以稍后处理它以使 zip 按预期工作。

【讨论】:

  • 感谢这很有趣。但并不完全正确,因为所有大于 90 天的文件都被添加到同一个 zip 文件中,而不是与其最后写入日期相对应的 zip 文件中。我想我可以适应这个。
  • 另外,ZipIt 是否应该添加包含完全限定的路径?
【解决方案2】:

从 .NET 3 开始,System.IO.Packaging.ZipPackage 可以进行压缩。 this example 中没有子文件夹,但可以处理 apparently 中的子文件夹。

日期过滤器可能是这样的:

$ninetyDaysAgo = (get-date).adddays(-90)
$Files | where {$_.lastWriteTime -lt $ninetyDaysAgo}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2019-11-07
    • 2015-08-15
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多