iT邦幫忙

0

排程的應用-刪除檔案

請教大家
要如何利用排程功能,來刪除指定路徑下的檔案(有條件的刪除),例如要刪除/var/atlassian/application-data/confluence/backups/ 路徑下,只保留每個月第一天的檔案,其它的檔案則把它都刪除,煩請大家指點 感謝

2 個回答

0
阿輪
iT邦新手 5 級 ‧ 2017-05-31 11:31:35

簡單一點方式
每個月第一天排程複製A資料夾檔案到B資料夾
每個月底排程刪除A資料夾所有檔案
這樣你B資料夾就有每個月第一天的備份了

0

同上~!可以使用 robocopy or powershell 實做
PowerShell

$Now = Get-Date 
#定義要刪除的天數
$Days = "0"
#定義路徑
$TargetFolder = "D:\Scan Files"
#定義副檔名
$ExtensionPDF = "*.pdf"
$ExtensionTIF = "*.tif"
$ExtensionTXT = "*.txt"
#定義要刪除的日期 (刪除今天往前Days的log)
$LastWrite = $Now.AddDays(-$Days)

$FilesPDF = Get-Childitem $TargetFolder -Include $ExtensionPDF -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
$FilesTIF = Get-ChildItem $TargetFolder -Include $ExtensionTIF -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
$FilesTXT = Get-ChildItem $TargetFolder -Include $ExtensionTXT -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}



Write-Host "Check PDF Extension Files"
foreach ($File in $FilesPDF) 
{
    if ($File -ne $NULL)
    {
        write-host "Deleting File $File" -ForegroundColor "DarkRed"
        $File.FullName | Out-File 'd:\delete_log.txt' -Append
        Remove-Item $File.FullName | Out-Null
    }
    else
    {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        Start-Sleep -Seconds 5
    }
}

Write-Host "Check TIFF Extension Files"
foreach ($File in $FilesTIF) 
{
    if ($File -ne $NULL)
    {
        write-host "Deleting File $File" -ForegroundColor "DarkRed"
        $File.FullName | Out-File 'd:\delete_log.txt'-Append
        Remove-Item $File.FullName | Out-Null
    }
    else
    {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        Start-Sleep -Seconds 5
    }
}

Bath for Robocopy

rem 刪除前一天備份的檔案
rd "d:\upsdata\delete_log\" /s /q

rem 移動大於兩天的檔案至 delete_log 資料夾
robocopy "D:\Scan Files" "D:\DELETE_LOG" /e /zb /maxage:2 /xf "D:\Scan Files\*.txt" /move /log:"d:\delete_log.txt"

我要發表回答

立即登入回答