locked
delete files 5000 at a time to control deletion RRS feed

  • Question

  •  I have a large number of files in a folder and want to delete files by a parm(to control cleanup) and log deletions.

       This is close but uses lastwritetime. 


     Thanks.

     
    [IO.Directory]::EnumerateFiles("C:\FOLDER_WITH_FILES_TO_DELETE") |
    select -first 100000 | where { [IO.File]::GetLastWriteTime($_) -lt
    (Get-Date).AddDays(-90) } | foreach { $c = 0 } { Write-Progress
    -Activity "Delete Files" -CurrentOperation $_ -PercentComplete 
    ((++$c/100000)*100); rm $_ }

    Thursday, June 6, 2019 12:53 AM

Answers

  • You have a superfluous "($_)" in the "Where-Object". And I don't think that "Remove-Item" is going to work either. It wants to see a property named "Path" in the piped object.

    I'm not sure if the original poster want to delete the oldest files first, or not. Also, what if there are no files that meet the criterion in the Where-Object cmdlet (remember, they won't be sorted by date).

    I think this is closer to what he wanted (minus the logging, of course):

    Get-ChildItem c:\temp -File |
        Where {$_.LastWriteTime -lt (Get-Date).AddDays(-2)} |
            Sort LastWriteTime |
                Select -First 5000 | Select @{n='Path';e={$_.FullName}} |
                    Remove-Item  -WhatIf


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by hart60 Thursday, June 6, 2019 11:12 PM
    Thursday, June 6, 2019 7:40 PM

All replies

  • You need to ask a question,

    How to ask   questions in a technical forum


    \_(ツ)_/

    Thursday, June 6, 2019 2:33 AM
  •  I want it to always keep the last 2 days of files in the folder, but each iteration of the script deletes
    5000 files, and logs the deletions to a file with start and end times of the process. I'm going to
    run this multiple times for the folder cleanup.


     Thanks.

    Thursday, June 6, 2019 1:11 PM
  • You are giving a spec and not asking a question.  Questions start with "how"."why", "when" an other such …

    You are not saying what is not working.  Your code is quite odd.

    Please ask a question.

    Please read the following carefully: How to ask   questions in a technical forum

    Get-ChildItem c:\folder -File |
        Select-Object -first 100000 |
        Where-Object {$_.LastWriteTime($_) -lt (Get-Date).AddDays(-90)} | 
        Remove-Item $_ -WhatIf

    What does "delete by a parm" mean.  Are you asking how to use a variable as a parameter?


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Thursday, June 6, 2019 2:49 PM
    Thursday, June 6, 2019 1:30 PM
  •  Is this the fasts method for cleanup, and yes I'm asking how to use a variable as a parameter?

      Does using this slow the process?

    { Write-Progress
    -Activity "Delete Files" -CurrentOperation $_ -PercentComplete 
    ((++$c/100000)*100); rm $_ }

      
     How can I always keep the current day and previous days files, no matter when I run the process? 

     Thanks sorry for information in previous posts.

    Thursday, June 6, 2019 5:08 PM
  • You have a superfluous "($_)" in the "Where-Object". And I don't think that "Remove-Item" is going to work either. It wants to see a property named "Path" in the piped object.

    I'm not sure if the original poster want to delete the oldest files first, or not. Also, what if there are no files that meet the criterion in the Where-Object cmdlet (remember, they won't be sorted by date).

    I think this is closer to what he wanted (minus the logging, of course):

    Get-ChildItem c:\temp -File |
        Where {$_.LastWriteTime -lt (Get-Date).AddDays(-2)} |
            Sort LastWriteTime |
                Select -First 5000 | Select @{n='Path';e={$_.FullName}} |
                    Remove-Item  -WhatIf


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by hart60 Thursday, June 6, 2019 11:12 PM
    Thursday, June 6, 2019 7:40 PM
  • In your original post you used a 90-day timespan. 

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, June 6, 2019 7:42 PM
  • The current day and previous day is this:

    Where {$_.LastWriteTime -lt [datetime]::Today.AddDays(-1)} |


    \_(ツ)_/

    Thursday, June 6, 2019 9:33 PM
  •  Many Thanks Rich that's what I was looking for. Would the write-progress be a good idea just to monitor
    the deletes?

     also thanks for all replies.

    Thursday, June 6, 2019 11:16 PM
  • I think it'd just be eye candy.

    If you stick with pipelining you really won't know how many files are going to be deleted. It could be 5000, or it could be 1000, or it could be 10. Without knowing you can't calculate the percentage.

    You'd have to get all the names into an array, get the count, then pipe the array into a foreach loop where you'd update the progress bar. You can do it if you want to.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, June 7, 2019 1:53 AM