Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于根据文件名开头删除文件的powershell脚本_Powershell_Glob_Get Childitem - Fatal编程技术网

用于根据文件名开头删除文件的powershell脚本

用于根据文件名开头删除文件的powershell脚本,powershell,glob,get-childitem,Powershell,Glob,Get Childitem,所以我在一个文件夹中有以 1__ 及 示例文件 1__shal1absahal9182skab.php 0__abns1a3bshal54a4m5sb.php 我正在尝试让我的powershell脚本仅删除1_u__中早于60分钟的文件,而0__可以每隔360分钟删除一次 这是我目前的代码 $limit = (Get-Date).AddMinutes(-360) $path = "C:\cache" # Delete files older than the $limit. Get-Chi

所以我在一个文件夹中有以

1__

示例文件

1__shal1absahal9182skab.php
0__abns1a3bshal54a4m5sb.php
我正在尝试让我的powershell脚本仅删除
1_u__
中早于
60分钟的文件,而
0__
可以每隔
360分钟删除一次

这是我目前的代码

$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分钟
intivals时删除它们。

找到了使用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
您可以按如下方式进行简化:

  • 使用
    -File
    -Directory
    限制仅输出该类型的项目

  • 使用带有PowerShell的
    -Include
    ,将匹配限制为名称以
    0\uuuuu
    1\uuuuuu
    开头的文件

注意:上面命令中的预览操作。一旦确定操作将执行所需操作,请删除
-WhatIf

注:

  • ($limit\u guest,$limit\u logged\u)[$$.Name-类似于'1\\\']
    根据条件值选择一个
    $limit\u*
    值来模拟三元条件:如果条件值的计算结果为
    $true
    ,则将其解释为数组索引
    1
    ,否则为
    0

  • -like
    运算符支持与
    Get ChildItem
    -Include
    参数相同的通配符模式,但请注意后者的
    -Filter
    参数-虽然更快-仅支持不同的、功能较弱的模式-请参阅


请找到准确预期输出的脚本

将路径替换为原始路径

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

看起来像是一个用户在没有解释的情况下否决了所有答案(尽管没有办法确定)。脚本在我的系统中运行良好。你能解释一下你面临的问题吗。所以,我可以帮你纠正
$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
$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"
    }
}