Powershell至压缩文件夹&;文件,然后删除旧文件

Powershell至压缩文件夹&;文件,然后删除旧文件,powershell,compression,powershell-3.0,Powershell,Compression,Powershell 3.0,我想使用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)

我想使用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文件将无法获得我想要的文件夹结构:

logfile.zip>bigfolder>logfile.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
(根据需要修改代码)


Thx,如果我有一个文件夹和多个文件,文件名的开头有相同的命名约定,例如D:\source\emstg#12\string.xml(文件夹和文件),然后是单个文件:D:\source\emstg#12#u data.xml或D:\source\emstg#12#events.dat,则会出现一个错误,原因是类似的名称:异常调用带有“4”参数的“CreateFromDirectory”:“文件'D:\destination\esmstg#12.zip'已经存在。”临时文件也不会被删除。修改后的代码是:
…..| Where Object{$|类似于“esmstg#12*”}| ForEach Object{
zip应该保持相同的文件夹/文件结构
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++
    }
}