0

我想制作一个 PowerShell 脚本,当它们 7 天大时删除某个文件夹中的任何类型的文件。我遇到的问题是创建一个日志文件,其中包含脚本运行时已删除或先前删除的所有文件的日期、时间和名称。

我想知道是否有办法修改此页面上的答案:https ://stackoverflow.com/questions/12326171/powershell-script-to-delete-sub-folders-and-files-if-creation-date -is-7-days-bu?lq=1

我希望它不会发送包含报告的电子邮件,而是会在我存储脚本的文件夹中创建一个包含名称、时间和日期的所有已删除文件的日志文件(请注意我想要每次都附加日志文件而不覆盖它)。很抱歉,因为我不知道如何编写代码,所以很长一段时间以来我一直在尝试自己做这件事,我一直在问问题等等,但似乎仍然无法让它发挥作用。所以是的,如果有人可以修改该脚本,将不胜感激!谢谢!

这是我对脚本所做的(如果有帮助的话),但它不起作用(同样我不知道如何编码):

$report_items = @()
# set folder path
$dump_path = "C:FileDeleter\AutoDeleteFilesInThisFolder"
# set min age of files
$max_days = "-7"
# get the current date
$curr_date = Get-Date
# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)
# get the sub directories
$sub_dirs = Get-ChildItem $dump_path | Where-Object {!$_.PSIsContainer }
$sub_dirs | % {
    if (Get-ChildItem $_.FullName -Recurse | Where-Object { $_.LastWriteTime -gt     $del_date } ) {
        $report_items += New-Object -Type PSObject -Property @{
            Time = Get-Date -f "hh:mm:ss"
            Message = "Skipping " + $_.FullName + " because it contains items newer     than " + $del_date
        }
    }
    else {
        Remove-Item $_.FullName -Recurse
        $report_items += New-Object -Type PSObject -Property @{
            Time = Get-Date -f "hh:mm:ss"
            Message = "Deleting " + $_.FullName + " because it contains no items newer     than " + $del_date
        }
    }
}
$report_items | out-file "C:\FileDeleter\PruningReport.txt"
4

2 回答 2

0

不是那么简洁,但我喜欢在 PowerShell 中不要过度流水线。

[String]                   $strBaseDir       = "c:\temp";
[String]                   $strFileFilter    = "*.txt";
[Int]                      $intDayThreshold  = 7;
[System.IO.FileSystemInfo] $objFile          = $null;
[Int]                      $intLastWriteDays = 0;
[String]                   $strLogFileName   = "d:\data\yourlogfile.log";

Get-ChildItem -LiteralPath $strBaseDir -Filter $strFileFilter -Recurse | Where-Object{ ! $_.PSIsContainer } |
    Foreach-Object {
        $objFile = $_;

        $intLastWriteDays = [Math]::Round((New-TimeSpan -Start $objFile.LastWriteTime -End (Get-Date)).TotalDays);

        if ( $intLastWriteDays -ge $intDayThreshold ) {
            Write-Output -InputObject ( "{0:G} : Deleting file [{1}] with last write time of [{2:G}], which is [{3}] day(s) ago." -f (Get-Date), $objFile.FullName, $objFile.LastWriteTime, $intLastWriteDays ) | Add-Content -Path $strLogFileName -PassThru;
            $objFile | Remove-Item -Force -ErrorAction silentlyContinue;
            if ( ! $? ) {
                Write-Output -InputObject "ERROR : Failed to remove file." | Add-Content -Path $strLogFileName -PassThru;
                } #if
        } else {
            Write-Output -InputObject ( "{0:G} : Skipping file [{1}] with last write time of [{2:G}], which is [{3}] day(s) ago." -f (Get-Date), $objFile.FullName, $objFile.LastWriteTime, $intLastWriteDays ) | Add-Content -Path $strLogFileName -PassThru;
        } #else-if

        } #Foreach-Object
于 2013-05-03T21:15:12.110 回答
0

这应该这样做:

Get-ChildItem -Path "C:\FileDeleter\AutoDeleteFilesInThisFolder" -Filter *.torrent -Recurse | Where { $_.CreationTime -lt (Get-Date).AddDays(-7) } | %{Remove-Item -Force; if ($?) {"$(Get-Date -Format 'MM-dd-yy HH:mm:ss.ff')  $_.Name" | Add-Content 'C:\FileDeleter\PruningReport.txt'}}

(阻止的原因if ($?)是仅在文件删除成功时才写入日志条目 - 您不想假设它是成功的。)

于 2013-05-02T21:44:58.400 回答