适用于具有.20181104、20181105等重复日期扩展名的文件的Powershell删除脚本

时间:2018-11-14 21:24:40

标签: powershell scripting

全部

这是我的脚本,用于删除我们一台PTV服务器上的日志文件。这里的问题是日志文件的命名方式显示了不同的日期扩展名,例如“ blah.log.20181102”,“ blah.log.20181103”

我的难题是如何删除末尾具有不同日期的文件。我想删除一些文件,所以我使用了measure-object,但是即使删除后,它也一直给我一个“ 0”的计数。下面是我的脚本:

$limit = (Get-Date).AddDays(-30)
$path = 'C:\Users\uyr2f3d\Downloads\Hello1'
$path = 'C:\Users\uyr2f3d\Downloads\Hello2'
$path = 'C:\Users\uyr2f3d\Downloads\Hello3'

Get-Childitem -Path @(
    'C:\Users\uyr2f3d\Downloads\Hello1\*'
    'C:\Users\uyr2f3d\Downloads\Hello2\*'
    'C:\Users\uyr2f3d\Downloads\Hello3\*'
) -Include '*.tmp', '*.log' -Force -Recurse |
    Where-Object { -not $_.PSIsContainer -and $_.LastWriteTime -lt $limit } |
    Remove-Item | ForEach-Object { $_ }

Measure-Object
Get-Date
Read-Host -Prompt 'The files have been deleted successfully'

我想要一个提示,显示删除的文件数量以及删除的时间。这就是读取主机的提示。我愿意简化和改进此查询,因为它还会从3个不同的文件夹中删除文件。

1 个答案:

答案 0 :(得分:0)

我不知道“删除时”是什么意思……在运行脚本时将其删除。 [咧嘴]

这将接受文件列表,解析日期的扩展名,将以天为单位的年龄与截止年龄进行比较,然后根据测试删除或不删除。然后显示总计数和已删除的计数。

# save the old InfoPref
$OldInfoPref = $InformationPreference
# enable Info output
$InformationPreference = 'Continue'

# fake reading in a list of files
#    in real life, use Get-ChildItem
$FileList = @(
    [System.IO.FileInfo]'One.log.20180101'
    [System.IO.FileInfo]'Two.log.20180202'
    [System.IO.FileInfo]'Three.log.20180303'
    [System.IO.FileInfo]'Four.log.20180404'
    [System.IO.FileInfo]'Five.log.20180505'
    [System.IO.FileInfo]'Ten.log.20181010'
    [System.IO.FileInfo]'Nine.log.20180909'
    [System.IO.FileInfo]'Eight.log.20180808'
    [System.IO.FileInfo]'Eleven.log.20181111'
    )

$MaxDaysOld = 30
$Today = (Get-Date).Date

$DeletedFileList = foreach ($FL_Item in $FileList)
    {
    $DaysOld = ($Today - [datetime]::ParseExact($FL_Item.Extension.Trim('.'), 'yyyyMMdd', $Null)).Days
    if ($DaysOld -gt $MaxDaysOld)
        {
        Write-Information ('The file named {0} is too old [{1} days].' -f $FL_Item.Name, $DaysOld)
        Write-Information '    ----- The file would be deleted -----.'
        Write-Information ''

        # put your delete command on the next line


        # send the deleted file name to the deleted file collection
        $FL_Item
        }
        else
        {
        Write-Information ('The file named {0} is only {1} days old.' -f $FL_Item.Name, $DaysOld)
        Write-Information '    The file would NOT be deleted.'
        Write-Information ''
        }
    }

'=' * 50
'Total file count   = {0}' -f $FileList.Count
'Deleted file count = {0}' -f $DeletedFileList.Count

# restore the old InfoPref
$InformationPreference = $OldInfoPref

输出...

The file named One.log.20180101 is too old [317 days].
    ----- The file would be deleted -----.

The file named Two.log.20180202 is too old [285 days].
    ----- The file would be deleted -----.

The file named Three.log.20180303 is too old [256 days].
    ----- The file would be deleted -----.

The file named Four.log.20180404 is too old [224 days].
    ----- The file would be deleted -----.

The file named Five.log.20180505 is too old [193 days].
    ----- The file would be deleted -----.

The file named Ten.log.20181010 is too old [35 days].
    ----- The file would be deleted -----.

The file named Nine.log.20180909 is too old [66 days].
    ----- The file would be deleted -----.

The file named Eight.log.20180808 is too old [98 days].
    ----- The file would be deleted -----.

The file named Eleven.log.20181111 is only 3 days old.
    The file would NOT be deleted.

==================================================
Total file count   = 9
Deleted file count = 8