Delete files older than x days in a folder, but IGNORE files in subfolders

Welcome Forums General PowerShell Q&A Delete files older than x days in a folder, but IGNORE files in subfolders

Viewing 5 reply threads
  • Author
    Posts
    • #192286
      Participant
      Topics: 2
      Replies: 3
      Points: 28
      Rank: Member

      So far I have found nothing that addresses my goal. I want to delete all files older than x days with a certain file filter. It will not seem to find ANY files unless I use recurse. But if I use recurse, it includes all files matching the filter in the sub folders. The check to see if it is a container appears to work if you do not want to delete the folder, but it still allows the script to see (and delete) all files matching the date and filter criteria in those subfolders.

      Does PS even have a way to ignore files in subfolders that would otherwise match the date and file filter criteria?? I only want to ‘look’ in the root folder.

      Below is a script that works IF I user recurse, but it includes all files in the sub folders as well, which I cannot have. Since I had to create the test folders and files, I actually added 1 day so all files would fall within the date range I want to include. Normally, that would be – days instead of + days. I use a variable to hold the child item results to get the count, and whatif to see what would happen. When I do NOT use recurse, the file count changes to 0, though there are a dozen files in the root folder (path) that meet the date and filter criteria. SO with recurse, it includes ALL files even in sub folders, but without recurse, it finds nothing.

      $HowOld = +1
      
      #Path to the root folder
      
      $Path = "C:\Users\LANID\Documents\NAS_Cleanup_Test"
      
      $LastWrite = (Get-Date).AddDays($HowOld).ToString('MM/dd/yyyy')
      
      $filtervar = "*.*"
      
      write-output $Path
      
      Write-Output $LastWrite
      
      #Get the files to delete
      
      $filesToDelete = Get-ChildItem -Path $Path -include $filtervar -File -Recurse | Where {($_.creationtime -lt $LastWrite)}
      
      $filesToDelete.Count
      
      #Delete the files
      
      $filesToDelete | Remove-Item -Force – WhatIf
      
    • #192427
      Participant
      Topics: 5
      Replies: 2411
      Points: 6,193
      Helping Hand
      Rank: Community MVP

      I’m confused. Do you want to find files even in subfolders or not? If not you should not use the parameter -Recurse. You named your variable to specify the age of the files you’re after “LastWrite” but you’re using the property “creationTime“. Did you mean “LastWriteTime” actually?

      BTW: You don’t need to use the method .toString() for your variable $LastWrite. Powershell deals very well with [DateTime] objects and properties.

    • #192502
      Participant
      Topics: 0
      Replies: 1
      Points: 14
      Rank: Member

      Learning with the help of community and new to blog.

      Tried the below one added -Filter parameter

          $HowOld = -1
      
          #Path to the root folder
      
          $Path = "D:\Powershell\test"
      
          $LastWrite = (Get-Date).AddDays($HowOld)
      
          $filtervar = "*.*"
      
          write-output $Path
      
          Write-Output $LastWrite
      
          #Get the files to delete
      
          $filesToDelete = Get-ChildItem -Path $Path -Filter $filtervar   |
      
          Where-Object {($_.LastWriteTime -lt $LastWrite)}
      
          $filesToDelete.count
      
          #Delete the files
      
          $filesToDelete | Remove-Item -Force
    • #192517
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      Try adding a \* to the end of the path. Get only CSV files in the script directory created within the last 30 days

      $files = Get-ChildItem -Path 'C:\Scripts\*' -Recurse -File -Include '*.csv' | 
               Where {$_.LastWriteTime -gt (Get-Date).AddDays(-30)}
      
      $files
      

      Also, please be clear on what the error is (if there is one), what the current results are, what the expected result should be. For instance, I have a scripts directory and a sub-folder Corp. I only want to get text files from the scripts directory and the Corp directory, but there are many sub-directories….

    • #192526
      Participant
      Topics: 5
      Replies: 106
      Points: 273
      Helping Hand
      Rank: Contributor

      By not knowing what the file type is, I’ll use the csv as an example. Also you did not specify how old the files should be, so let’s make it 3. Path is c:\temp as I had some files there 🙂

      The command could be as easy as:

      Get-ChildItem c:\temp\*.csv | where {$_.creationtime -le (get-date).adddays(-3)} | remove-item -WhatIf

      In my case returns:

      What if: Performing the operation “Remove File” on target “C:\temp\auto.csv”.
      What if: Performing the operation “Remove File” on target “C:\temp\Encoding Time.csv”.
      What if: Performing the operation “Remove File” on target “C:\temp\psorg.csv”.
      
    • #197399
      Participant
      Topics: 2
      Replies: 3
      Points: 28
      Rank: Member

      Thank you all for your input!!

      I have found that our server is using a very old version 2 of ps. I ended up using the “.Name -like”, and it works. Of course when we start running this for real, we will remove -WhatIf 🙂

      “$filesToDelete = Get-ChildItem -Path $Path | Where-Object {$_.Name -like “*.log”} | Where {($_.creationtime -lt $CreateDate)}”

      “$filesToDelete | Remove-Item -Force -WhatIf”

       

Viewing 5 reply threads
  • The topic ‘Delete files older than x days in a folder, but IGNORE files in subfolders’ is closed to new replies.