如何从不同位置删除超过X天的多个文件?

时间:2016-03-16 20:23:16

标签: powershell

   void double_swap(double* x, double* y)

这是一个很好的PowerShell脚本,用于删除超过X天数的文件,但我们希望删除在不同位置保存的多个文件,每个路径的天数不同。

例如,如果我们必须删除超过3天的文件,那么我们可以添加另一个位置,例如:

#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date

#----- define amount of days ----#
$Days = "3"
#----- define folder where files are located ----#
$TargetFolder = "C:\test2"
#----- define extension ----#
$Extension = "*.*"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-ChildItem $TargetFolder -Include $Extension -Recurse |
         Where  {$_.LastWriteTime -le "$LastWrite"} 

foreach ($File in $Files) {
    if ($File -ne $NULL) {
        Write-Host "Deleting File $File" -ForegroundColor "Red"
        Remove-Item $File.FullName | Out-Null
    } else {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
    }
}

它将处理这两个文件夹,但我们想知道是否有一种方法可以为不同的文件夹设置不同的天数。

$TargetFolder = "C:\test","C:\test2" 我们过去常常在配置文件中这样做:

Location 1;//Server1/c$/processed;14
Location 2;//Server2/backup/;3

4 个答案:

答案 0 :(得分:3)

使用" age"等参数调用脚本。和"位置"

Param
(
    [int]$days,
    [string]$Targetfolder
)
#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date

#----- define amount of days ----#
#$Days = '3'
#----- define folder where files are located ----#
#$TargetFolder = 'C:\test2'
#----- define extension ----#
$Extension = '*.*'
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where-Object  {$_.LastWriteTime -le "$LastWrite"} 

foreach ($File in $Files) 
{
    if ($File -ne $NULL)
    {
        write-host "Deleting File $File" -ForegroundColor 'Red'
        Remove-Item $File.FullName | out-null
    }
    else
    {
        Write-Host 'No more files to delete!' -foregroundcolor 'Green'
    }
}

将其另存为.ps1并将其称为MyScript.ps1 -Targetfolder C:\Blah -Days 4

答案 1 :(得分:1)

这是我的解决方案。您可以通过复制/粘贴到脚本中来使用它。然后,这样称呼:

PS> $Folders = 'C:\Folder1','C:\Folder2'
PS> .\Remove-OldFiles.ps1 -FolderPath $Folders -DaysOld 30 -WhatIf

然后,如果一切都很好,请删除-WhatIf。这应该比你在PSv4中使用较新的.where()和.foreach()方法的脚本快得多。

#Requires -Version 4

[CmdletBinding(SupportsShouldProcess,ConfirmImpact = 'High')]
param (
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string[]]$FolderPath,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [int]$DaysOld,

    [Parameter()]
    [ValidateNotNullOrEmpty()]
    [ValidateLength(1, 3)]
    [string]$FileExtension
)

$Now = Get-Date

$gciParams = @{
    'Recurse' = $true
    'File' = $true
}

if ($PSBoundParameters.ContainsKey('FileExtension')) {
    $gciParams.Filter = "Extension -eq $FileExtension"
}

$LastWrite = $Now.AddDays(-$DaysOld)

foreach ($path in $FolderPath)
{
    $gciParams.Path = $path
    ((Get-ChildItem @gciParams).Where{ $_.LastWriteTime -le $LastWrite }).foreach{
        if ($PSCmdlet.ShouldProcess($_.FullName, 'Remove'))
        {
            Remove-Item -Path $_.FullName -Force
        }
    }
}

答案 2 :(得分:1)

我建议参数化脚本,如@restless1987所述。但是,如果要从文件中读取信息,可以在第二个PowerShell脚本中创建哈希表:

# config.ps1

$locations = @{
  'C:\processed' = 14
  'D:\backup'    = 3
}
脚本中的

dot-source文件:

. 'C:\path\to\config.ps1'

所以你可以像这样使用哈希表:

foreach ($TargetFolder in $locations.Keys) {
    $LastWrite = $Now.AddDays(-$locations[$TargetFolder])
    $Files = Get-ChildItem $TargetFolder -Include $Extension -Recurse |
             Where-Object  {$_.LastWriteTime -le $LastWrite} 

    foreach ($File in $Files) {
        ...
    }
}

答案 3 :(得分:0)

为您的工作创建一个函数,并将您的路径保存在哈希表中。

$targetFolder = @()

$targetFolder += @{'Folder'='c:\test1'; 'Extension'='*.*'; 'Days'=3}
$targetFolder += @{'Folder'='c:\test2'; 'Extension'='*.*'; 'Days'=5}
$targetFolder += @{'Folder'=@('c:\test3', 'c:\test4'); 'Extension'='*.*'; 'Days'=7}

function DeleteOldFiles($TargetFolder, $Extension, $Days)
{
    $lastWrite = (Get-Date).AddDays(-$Days)

    # do your Get-ChildItem logic here
    $files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where  {$_.LastWriteTime -le "$lastWrite"} 

    foreach ($file in $files) 
    {
        if ($file -ne $null)
        {
            Write-host "Deleting File $file" -ForegroundColor "Red"
            Remove-Item $file.FullName | out-null
        }
        else
        {
            Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }
}

$targetFolder | DeleteOldFiles $_.Folder $_.Extension $_.Days