如何修改此PowerShell脚本,以便创建日期时间和已删除或以前删除的所有文件的名称的日志?

时间:2013-05-02 21:13:55

标签: powershell

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

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

我希望它不会发送包含报告的电子邮件,而是会创建一个日志文件,其中包含我在存储脚本的文件夹中的名称,时间和日期删除的所有文件(请注意我' d喜欢它每次都不覆盖它时附加日志文件)。我很抱歉,因为我不知道如何编码,所以我一直坚持尝试自己这么做很长一段时间,我一直在问问题,但似乎仍然无法让它工作。所以,如果有人可以修改该脚本,那将非常感谢!谢谢!

以下是我对脚本所做的事情(如果它有帮助),但它不起作用(再次我不知道如何编码):

$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"

2 个答案:

答案 0 :(得分: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 ($?)块的原因是,如果文件删除成功,则只写一个日志条目 - 你不想假设它是。)

答案 1 :(得分: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