0

So i have files in a folder that start with

1__

and

0__

example files

1__shal1absahal9182skab.php
0__abns1a3bshal54a4m5sb.php

i am trying to get my powershell script to only delete files of 1__ that are older than 60 mins while 0__ can be deleted every 360 mins.

here is my current code

$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

my script currently treats both files as the same and deletes them both on 360 minute intivals.

mklement0
  • 382,024
  • 64
  • 607
  • 775
C0nw0nk
  • 870
  • 2
  • 13
  • 29
  • 1
    It looks like a single user down-voted all answers without an explanation (though there's no way to know for sure). – mklement0 Nov 25 '19 at 16:34

3 Answers3

0

You can streamline your own solution as follows:

  • Use -File and -Directory to limit Get-ChildItem to output only items of that type.

  • Use -Include with a PowerShell wildcard expression to limit matching to files whose names start with 0__ or 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

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

Note:

  • ($limit_guest, $limit_logged_in)[$_.Name -like '1__*'] emulates a ternary conditional by selecting one of the $limit_* values based on conditional $_.Name -like '1__*': if the conditional evaluates to $true, it is interpreted as array index 1, otherwise as 0.

  • The -like operator supports the same wildcard patterns as Get-ChildItem's -Include parameter, but note that the latter's -Filter parameter - while faster - supports only different, less powerful patterns - see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

Please find the script for exact expected output.

Replace path with your original path

$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"
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Narayana Lvsl
  • 315
  • 2
  • 10
  • The script is working fine in my system. Could you please explain where you are facing the issue. So, that I can correct for you – Narayana Lvsl Nov 26 '19 at 03:01
-1

Found a solution using if and else with some regex pattern matching.

$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
mklement0
  • 382,024
  • 64
  • 607
  • 775
C0nw0nk
  • 870
  • 2
  • 13
  • 29