Keep x number of files and delete all others - Powershell

前端 未结 4 649
天命终不由人
天命终不由人 2020-12-15 18:01

I am trying to write a script that will look through a set of folders and keep only the last 10 files. The files in each folder could be created daily, weekly or monthly. I

相关标签:
4条回答
  • 2020-12-15 18:34

    How to call this GCI command in SQL server , was trying below code but returning error

    declare @deleteoldtempxe nvarchar(1000)
            set @deleteoldtempxe='powershell.exe gci '+''''+ 'I:\New folder\'+''''+' -Recurse| where{-not $_.PsIsContainer}| sort CreationTime -desc| 
        select -Skip 1| Remove-Item -Force'
        select @deleteoldtempxe
    
    EXEC master.dbo.xp_cmdshell @deleteoldtempxe
    
    0 讨论(0)
  • 2020-12-15 18:36

    You can sort by CreationTime descending and skip the first 10. If there are less than 10 files it will not remove any.

    gci C:\temp\ -Recurse| where{-not $_.PsIsContainer}| sort CreationTime -desc| 
        select -Skip 10| Remove-Item -Force
    
    0 讨论(0)
  • 2020-12-15 18:39

    Updated

    $path = "C:\TestDir"
    
    # Create 12 test files, 1 second after each other
    1..12 | % {
        Remove-Item -Path "$path\$_.txt" -ea SilentlyContinue
        $_ | Out-File "$path\$_.txt"
        Start-Sleep -Seconds 1
    }
    
    $files = Get-ChildItem -Path $path -Recurse | Where-Object {-not $_.PsIsContainer}
    $keep = 10
    if ($files.Count -gt $keep) {
        $files | Sort-Object CreationTime | Select-Object -First ($files.Count - $keep) | Remove-Item -Force -WhatIf
    }
    

    Remove the -WhatIf parameter to Remove-Item when your ready to delete for real.

    0 讨论(0)
  • 2020-12-15 18:42

    @Andy: My rep is too low to comment so posting an answer. If you are getting strange results, it may be because you are looking in Windows Explorer which by default shows the Last Modified date instead of the Creation Date. To get results consistent with that shown in Windows Explorer, swap LastWriteTime for CreationTime like so: gci C:\temp\ -Recurse| where{-not $_.PsIsContainer}| sort LastWriteTime -desc | select -Skip 10| Remove-Item -Force

    0 讨论(0)
提交回复
热议问题