【问题标题】:Deleting files with PowerShell script使用 PowerShell 脚本删除文件
【发布时间】:2016-06-24 14:22:16
【问题描述】:

我在 PowerShell 中有一个脚本,它扫描一个文件夹目录,这些文件夹的名称使用以下约定:yyyymmdd。它扫描目录并找到所有最新的和最多一周前的文件夹,然后将它们复制到另一个目录。将它们复制到另一个目录后,我想让它删除新目录中以相同方式命名且超过 18 个月的文件夹。有没有简单的方法可以做到这一点?我已经粘贴了下面的脚本。

$targetdirectory = "\\DPR320-W12-1600\PRTG"
$sourcedirectory = "C:\Users\Public\Documents\PRTG Traffic Grapher"
$todaysdate=get-date
$minusoneweek=$todaysdate.adddays(-7)
$minusdate=($minusoneweek.Month).tostring(),($minusoneweek.day).tostring(),($minusoneweek.year).tostring()
$todaysdatestring=($todaysdate.Month).tostring(),($todaysdate.day).tostring(),($todaysdate.year).tostring()
$oldfilename=$minusdate[0]+$minusdate[1]+$minusdate[2]+" backup"
$newfilename=$todaysdatestring[0]+$todaysdatestring[1]+$todaysdatestring[2]+" backup"


Get-ChildItem $sourcedirectory\config | Where-Object { 
    $_.PsIsContainer -and 
    $_.BaseName -match '\d{6}' -and 
    ([DateTime]::ParseExact($_.BaseName, 'yyyyMMdd', $null) -gt (Get-Date).AddDays(-7)) 
} |Copy-Item -Recurse -Force -Destination $targetdirectory\$oldfilename\config

Copy-Item -Force $sourcedirectory\config.csv -Destination $targetdirectory\$oldfilename
Copy-Item -Force $sourcedirectory\config.prtg -Destination $targetdirectory\$oldfilename

rename-item $targetdirectory\$oldfilename $newfilename

【问题讨论】:

    标签: windows powershell filesystems prtg


    【解决方案1】:

    根据您与 FoxDeploy 的讨论: 这将遍历 $YourDirectory 并检查名称是否代表早于 18 个月的日期。 将其用于我自己的清理工作,但我的名称采用点分格式。

    $CleanupList = Get-ChildItem $YourDirectory
    $Threshold = (get-date).AddMonths(-18)
    
    foreach ($DirName in $CleanupList)
    {
        If (([datetime]::ParseExact($DirName.BaseName,'yyyyMMdd',$null)) -lt $Threshold)
        {
            Remove-Item $DirName.FullName -Force -Recurse
        }
    }
    

    【讨论】:

    • 我的文件夹不一定遵循该命名约定。如何让脚本忽略它们并只关注符合命名约定的文件?
    • 如果你能提供一些例子来展示这个想法。到目前为止,我只能考虑检查 ParseExact 是否真的能够从文件夹名称中获取日期。
    【解决方案2】:

    假设 $targetDirectory 包含您要删除的文件(那些超过 18 个月的文件),只需将其添加到脚本的末尾即可:

    #resolve this day, 18 months ago
    $18Months = (get-date).AddMonths(-18)
    
    #get the name in the right format (i.e. 20141224)
    $18MonthString = get-date -Date $18Months -UFormat "%Y%m%d"
    
    #find the files with a name like this and delete them
    $OldPrtgFiles = dir $targetDirectory | Where Name -like "$18MonthString*" 
    $OldPrtgFiles | remove-item -whatif
    

    第一次执行将显示-WhatIf 视图,以显示哪些文件将被删除。如果您对此感到满意,请删除 WhatIf。

    【讨论】:

    • 感谢您的回答!这会起作用,但是,写入时间不会是 18 个月前,因为一些文件是旧数据,最近才被复制到目录中。我怎样才能知道使用文件夹名称删除哪些文件?命名格式是获取数据的日期,例如 20140624。这将是 2014 年 6 月 24 日获取的数据的文件夹名称。
    • 啊,我明白了。所以我们想删除名称为 20141224(18 个月前)的文件
    • 知道了,现在检查我的答案。
    • 谢谢!澄清一下,这会删除所有 18 个月及以上的文件,还是仅删除 18 个月前的文件?
    • 在我们的例子中,我们正在删除距今天正好 18 个月的文件。如果要删除所有超过 18 个月的文件,请将逻辑更改为 $OldPrtgFiles = dir $targetDirectory | Where BaseName -le "$18MonthString"
    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多