2012-01-11 81 views
1

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

在Powershell中处理此问题的最佳方法是什么?

编辑:除非有更好的办法,我认为zip文件应在\\server\oldlogs创建并命名特定日期,例如。 \\server\oldlogs\20110630_logs.zip。所有在\\server\logs最后修改超过90天前的文件都应该被添加到合适的.zip文件中。

+0

在\\服务器\记录有子目录。这些子目录将包含不以.zip结尾的文件。该过程应标识过去90天内尚未(创建/修改/访问?)的所有文件,并将其放入现有目录结构中的.zip文件中。创建存档文件后,应该删除那些相同的文件。准确? – billinkc 2012-01-11 18:17:59

+0

@billinkc - 现在你提到它了,在'\\ server \ oldlogs'目录中创建zip文件似乎更简单。我想结束一天中命名的zip文件,其中包含该日期上次修改的文件。那么是的,一旦他们被压缩,他们应该被删除。我会更新这个问题。谢谢。 – 2012-01-11 18:32:40

+0

为什么你使用powershell标签?你需要关于问题或功能脚本的意见吗? – mjsr 2012-01-11 19:16:36

回答

0

这是66%的解决方案(午餐结束)。我不能让本土拉链使用PowerShell的工作,但如果你有安装PowerShell的社区扩展,应该换出拉链通话瞬间 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工作。

+0

感谢这很有趣。不完全正确,因为> 90天以前的所有文件都会被添加到同一个zip文件中,而不是与上次写入日期相对应的zip文件。我想我可以适应这个。 – 2012-01-11 20:03:58

+0

另外,ZipIt是否应该添加包含完全合格的路径? – 2012-01-11 20:10:30

0

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

日期过滤器可以去像这样:

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