【问题标题】:powershell script to delete files based on start of file namepowershell脚本根据文件名的开头删除文件
【发布时间】:2019-11-24 15:46:53
【问题描述】:

所以我在以

开头的文件夹中有文件
1__

0__

示例文件

1__shal1absahal9182skab.php
0__abns1a3bshal54a4m5sb.php

我试图让我的 powershell 脚本只删除早于60 mins1__ 文件,而0__ 可以在每个360 mins 中删除。

这是我当前的代码

$limit = (Get-Date).AddMinutes(-360)
$path = "C:\cache"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

我的脚本目前将这两个文件视为相同,并在 360 minute intivals 上删除它们。

【问题讨论】:

  • 看起来一个用户在没有解释的情况下否决了所有答案(尽管无法确定)。

标签: powershell glob get-childitem


【解决方案1】:

您可以将your own solution精简如下:

  • 使用-File-DirectoryGet-ChildItem 限制为仅输出该类型的项目。

  • -Include 与PowerShell wildcard expression 一起使用,以限制匹配名称以0__1__ 开头的文件。

$now = Get-Date
$limit_guest = $now.AddMinutes(-360) #6 hours
$limit_logged_in = $now.AddMinutes(-60) #1 hour

# Delete files older than the $limit.
Get-ChildItem -File -Path $path -Recurse -Force -Include '[01]__*' | 
  Where-Object {
    $_.CreationTime -lt ($limit_guest, $limit_logged_in)[$_.Name -like '1__*']
  } | Remove-Item -Force -WhatIf

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Directory -Path $path -Recurse -Force |
  Where-Object { 
   (Get-ChildItem -File -LiteralPath $_.FullName -Recurse -Force).Count -eq 0
  } | Remove-Item -Force -Recurse -WhatIf

注意:上面命令中的-WhatIf common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf

注意:

  • ($limit_guest, $limit_logged_in)[$_.Name -like '1__*'] 通过选择基于条件$_.Name -like '1__*'$limit_* 值之一来模拟三元条件:如果条件计算为$true,则将其解释为数组索引1,否则为0

  • -like 运算符支持与 Get-ChildItem-Include 参数相同的通配符模式,但请注意后者的 -Filter 参数 - 虽然更快 - 仅支持不同的、不太强大的模式 - 请参阅 @ 987654325@.

【讨论】:

    【解决方案2】:

    请查找脚本以获得准确的预期输出。

    用你原来的路径替换路径

    $folder = Get-ChildItem -Path "C:\Users\***\Desktop\New folder\*.php" -Recurse -Force
    
    foreach($file in $folder) 
    { 
    
        if($file.Name -match "^0_.*" -and $file.CreationTime -lt  (Get-Date).AddMinutes(-360) )
        {
            Remove-item $file  -Force -Recurse -ErrorAction SilentlyContinue
            Write-Host "Deleted"$file.Name
        }
        elseif($file.Name -match "^1_.*" -and $file.CreationTime -lt  (Get-Date).AddMinutes(-60))
        {
            Remove-item $file -Force -Recurse -ErrorAction SilentlyContinue
            Write-Host "Deleted"$file.Name
        }
        else 
        {
        Write-Host "No files found"
        }
    }
    

    【讨论】:

    • 该脚本在我的系统中运行良好。您能否解释一下您面临的问题。所以,我可以为你纠正
    【解决方案3】:

    找到了使用 if 和 else 以及一些正则表达式模式匹配的解决方案。

    $limit_guest = (Get-Date).AddMinutes(-360) #6 hours
    $limit_logged_in = (Get-Date).AddMinutes(-60) #1 hours
    $path = "C:\cache"
    
    # Delete files older than the $limit.
    Get-ChildItem -Path $path -Recurse -Force | Where-Object { if ( $_ -match "^0__.*" ) { !$_.PSIsContainer -and $_.CreationTime -lt $limit_guest } else { !$_.PSIsContainer -and $_.CreationTime -lt $limit_logged_in } } | Remove-Item -Force
    
    # Delete any empty directories left behind after deleting the old files.
    Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多