用于根据文件名开头

时间:2019-11-24 15:46:53

标签: powershell glob get-childitem

所以我在以

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

0__

示例文件

1__shal1absahal9182skab.php
0__abns1a3bshal54a4m5sb.php

我正在尝试使我的Powershell脚本仅删除早于1__的{​​{1}}文件,而60 mins可以每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 动机中将它们都删除。

3 个答案:

答案 0 :(得分:0)

您可以按照以下步骤简化your own solution

  • 使用-File-Directory限制Get-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__*']通过基于条件$limit_*选择$_.Name -like '1__*'值之一来模拟三元条件:如果条件的取值为$true,则将其解释为数组索引1,否则为0

  • -like运算符支持与Get-ChildItem的{​​{1}}参数相同的通配符模式,但是请注意,后者的-Include参数-尽管速度更快-仅支持不同的,功能较弱的模式-请参见this answer

答案 1 :(得分:0)

请找到用于确切预期输出的脚本。

用原始路径替换路径

$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"
    }
}

答案 2 :(得分:-1)

使用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