Powershellを使用して自動化する: 1.ログファイル(.xmlと.dat拡張子)の圧縮を7日より後にする 2.これらの圧縮されたアーカイブ他の場所にあり、 3.元のログファイルをソースから削除します。Powershellでフォルダとファイルを圧縮してから古いファイルを削除する
私はさまざまなリソースからまとめた次の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の構造を持っている場合:私は希望として\ソース\ bigfolder \ logfile.dat、結果のzipファイルには、フォルダ構造を得ることはありません。
logfile.zip> bigfolder>ログファイルを
.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番号の数と同じである見ることができるとおり。
上記のスクリプトで「名前検出」メカニズムを実装する方法は?
おかげ
Thxを、いくつかのバグ が必要D:\ source \ emstg#12_data.xml **またはD:\ source \ emstg#D:\ source \ emstg#12 \ string.xml(フォルダとファイル) 12_events.dat **、似たような名前のためにエラーが発生しました: "4"引数を指定して "CreateFromDirectory"を呼び出す例外: "ファイル 'D:\ destination \ esmstg#12.zip'は既に存在します。 *一時ファイルも削除されません。 変更されたコードは: '..... | Where-Object {$ _ -like "esmstg#12 *"} | ForEach-Object {' ZIPは同じフォルダ/ファイル構造を保持する必要があります – valdroni