繁体   English   中英

Powershell 压缩文件夹和文件,然后删除旧文件

[英]Powershell to zip folder & files, then delete old files

我想使用 Powershell 自动执行以下操作:1. 压缩超过 7 天的日志文件(.xml 和 .dat 扩展名),2. 将这些压缩档案复制到其他地方,3. 然后从源中删除原始日志文件。

我正在使用从各种资源拼凑而成的以下 Powershell 脚本。

function New-Zip
  {
   param([string]$zipfilename)
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false
  }

function Add-Zip
  {
   param([string]$zipfilename)

   if(-not (test-path($zipfilename)))
    {
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false  
    }

  $shellApplication = new-object -com shell.application
  $zipPackage = $shellApplication.NameSpace($zipfilename)

   foreach($file in $input) 
     { 
        $zipPackage.CopyHere($file.FullName)
        Start-sleep -milliseconds 500
     }
   }

$targetFolder = 'C:\source'
$destinationFolder = 'D:\destination\'
$now = Get-Date
$days = 7
$lastWrite = $now.AddDays(-$days)

Get-ChildItem $targetFolder -Recurse | Where-Object { $_ -is [System.IO.FileInfo] } | ForEach-Object {
If ($_.LastWriteTime -lt $lastWrite)
{
    $_ | New-Zip $($destinationFolder + $_.BaseName + ".zip") 
    $_ | Add-Zip $($destinationFolder + $_.BaseName + ".zip")
}
}

Get-ChildItem $targetFolder -Recurse -Include "*.dat", "*.xml" | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$days))} | Remove-Item -Force

这个脚本不工作得相当好,因为它存档文件,并将其复制在目标文件夹。

如果我有C:\\source\\bigfolder\\logfile.dat 的结构,则生成的 zip 文件将不会像我希望的那样获得文件夹结构:

日志文件.zip>大文件夹>日志文件.dat

相反,它只是获取: logfile.zip>logfile.dat

有人可以帮忙解决这个问题吗?

为了更好地对其进行微调,我希望尽可能构建一些逻辑,以便仅在满足特定条件时才压缩文件。

我压缩的原始日志文件具有如下命名例程:

Folders: 
  emstg#12_list\randomstring.xml

Individual log files:
  emstg#12_query_data.xml
  emstg#12_events_cache.dat etc... 

如您所见,这些文件的开头与 emstg#number 相同。

如何在上面的脚本中实现“名称检测”机制?

谢谢

你可以使用[System.IO.Compression]压缩一个文件夹我是根据你的脚本写的。 我的想法是将需要压缩的文件的整个文件夹结构复制到临时文件夹中,然后压缩该临时文件夹。 对于名称检测,您只需要另一个where-object (根据需要修改代码)

function Zip
  {
    param(
        [string]$source,
        [string]$des
    )
    add-type -AssemblyName System.IO.Compression.FileSystem
    [System.IO.Compression.ZipFile]::CreateFromDirectory($source,$des,'Optimal',$true)
    Start-sleep -s 1
}


$targetFolder = "C:\source"
$destinationFolder = "C:\destination"
$temp = "C:\temp"
$now = Get-Date
$days = 7
$lastWrite = $now.AddDays(-$days)
$i = 1

Get-ChildItem $targetFolder -Recurse | Where-Object { $_ -is [System.IO.FileInfo] } | Where-Object {$_ -like "*.*"} | ForEach-Object {
    If ($_.LastWriteTime -lt $lastWrite) {
        $halfDir = $_.DirectoryName.Trim($targetFolder)
        $s = $temp + "\" + $i + "\" + $halfDir
        $d = $destinationFolder + "\" + $_.BaseName + ".zip"
        Copy-Item $_.DirectoryName -Destination $s
        Copy-Item $_.FullName -Destination $s
        Zip -source $s -des $d
        $i++
    }
}

暂无
暂无

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

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