繁体   English   中英

过滤和删除 powershell 中超过 x 天的文件和文件夹(以及文件夹内的文件)

[英]Filter and delete files and folders(and files inside of folders) older than x days in powershell

这是我在这个论坛上的第一篇文章。 我是编码初学者,我需要使用我的第一个自编码工具之一的帮助。 我制作了一个小脚本,它根据文件是否早于日期 x(lastwritetime)来删除文件。 现在我的问题是:我希望脚本还检查目录内文件夹内的文件,并且只有在文件夹确实为空时才删除该文件夹。 我不知道如何解决这个问题中的递归,似乎脚本只删除了与日期 x 相关的整个文件夹。 谁能告诉我我在这段代码中遗漏了什么并帮助我创建自己的递归来解决问题或修复代码? 谢谢大家,伙计们:这是我的代码:

如果有人知道如何使用 function 使代码工作,我会很高兴


$path = Read-Host "please enter your path"
"
"

$timedel = Read-Host "Enter days in the past (e.g -12)"

$dateedit = (Get-Date).AddDays($timedel)
"
"
Get-ChildItem $path -File -Recurse | foreach{ if ($_.LastWriteTime -and !$_.LastAccessTimeUtc -le $dateedit) {

Write-Output "older as $timedel days: ($_)" } }

" 
"
pause

Get-ChildItem -Path $path -Force -Recurse | Where-Object { $_.PsisContainer -and $_.LastWriteTime -le $dateedit } | Remove-Item -Force -Recurse 

""
Write-Output "Files deleted"

如果文件夹是空的,还要删除比过去设置的日期更早的文件夹,这会给您带来这样的问题,即一旦从此类文件夹中删除文件,文件夹的 LastWriteTime 就会被设置为该时刻。

这意味着您应该先获取旧文件夹列表,然后再开始删除旧文件,然后使用该列表删除这些文件夹(如果它们为空)。

此外,应该对来自 Read-Host 的用户输入进行最低限度的检查。 (即路径必须存在并且天数必须可转换为 integer 数字。对于后者,我选择简单地将其转换为[int]因为如果失败,代码无论如何都会生成一个执行。

尝试类似的东西

$path = Read-Host "please enter your path"
# test the user input
if (-not (Test-Path -Path $path -PathType Container)) {
    Write-Error "The path $path does not exist!" 
}
else {
    $timedel = Read-Host "Enter days in the past (e.g -12)"

    # convert to int and make sure it is a negative value
    $timedel  = -[Math]::Abs([int]$timedel)
    $dateedit = (Get-Date).AddDays($timedel).Date  # .Date sets this date to midnight (00:00:00)

    # get a list of all folders (FullNames only)that have a LastWriteTime older than the set date.
    # we check this list later to see if any of the folders are empty and if so, delete them.
    $folders = (Get-ChildItem -Path $path -Directory -Recurse | Where-Object { $_.LastWriteTime -le $dateedit }).FullName

    # get a list of files to remove
    Get-ChildItem -Path $path -File -Recurse | Where-Object { $_.LastWriteTime -le $dateedit} | ForEach-Object {
        Write-Host "older as $timedel days: $($_.FullName)" 
        $_ | Remove-Item -Force -WhatIf  # see below about the -WhatIf safety switch
    }

    # now that old files are gone, test the folder list we got earlier and remove any if empty
    $folders | ForEach-Object {
        if ((Get-ChildItem -Path $_ -Force).Count -eq 0) {
            Write-Host "Deleting empty folder: $_" 
            $_ | Remove-Item -Force -WhatIf  # see below about the -WhatIf safety switch
        }
    }

    Write-Host "All Done!" -ForegroundColor Green
}

Remove-Item 上使用的-WhatIf开关是为了您自己的安全。 这样,实际上不会删除任何文件或文件夹,而是在控制台中写入要删除的内容。 如果您对这一切都满意,请删除-WhatIf并再次运行代码以真正删除文件和文件夹

param(
    [IO.DirectoryInfo]$targetTolder = "d:\tmp",
    [DateTime]$dateTimeX = "2020-11-15 00:00:00"
)

Get-ChildItem $targetTolder -Directory -Recurse | Sort-Object {$_.FullName} -Descending | ForEach-Object {
    Get-ChildItem $_ -File | Where-Object {$_.LastWriteTime -lt $dateTimeX} | Remove-Item -Force
    if ((Get-ChildItem $_).Count -eq 0){Remove-Item $_ -Force} 
}

测试后删除 -WhatIf

尝试这样的事情:

$timedel=-12

#remove old files
Get-ChildItem "C:\temp" -Recurse -File | Where LastWriteTime  -lt  (Get-Date).AddDays($timedel)  | Remove-Item -Force

#remove directory without file
Get-ChildItem "C:\temp\" -Recurse -Directory | where {(Get-ChildItem $_.FullName -Recurse -File).count -eq 0} | Remove-Item -Force -recurse

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM