Search code examples
powershellscheduled-tasksbackupdotnetnuke

Powershell: Deleting old backup files but leave the recent one even if it's older


I'm tryng to write a .ps1 script that delete files older than 2 days but leave the most recent files also if old.
For the delete part internet is full of code's snippet to copy/paste.
For the "leave recent files" i'm in truble.

the structure of the bk's folder is the following:
--Db.yyyy.MM.dd.Native.bak.zip
--Files.yyyy.MM.dd.zip
--Log.yyyy.MM.dd.txt
--AND SO ON WITH THE OLDERS FILES

I wanna keep the most recent trio of this files also if older than 2 days.

If any one have a suggestion to the right approach or a solution, i'm here to learn.

tks to all.

P.S. Is the first time i use powershell and i have to do this script for work.


Solution

  • I would like to get you started so you have an idea how to approach this. It's not too hard actually if you approach it logically. First, you need to obtain the correct files from the backup folder. Then you have to examine each file by parsing the filename.

    I wonder if you cannot just take the file date and sort on the oldest? But if you really need to strip the filename, I wrote a very rough script on how such approach could look. Keep in mind, I just did some quick and dirty replace to make it work:

    #Get files 
    $zipFilesInFolder = Get-Childitem –Path "C:\Temp" | Where-Object {!$_.PSIsContainer -and  ($_.Name -like "*Files*") } | Sort-Object  -Property Name -Descending
    Write-Host 'Files found:' $zipFilesInFolder
    
    # Check files found
    [datetime]$oldestDate = Get-Date;
    [string]$oldestFile;
    
    # Check each file by parsing the filename
    Foreach ($i in $zipFilesInFolder) {
      $fileDate = $i -replace 'Files.' -replace '.zip',''
      $parsedDate = [datetime]::parseexact($fileDate, 'yyyy-MM-dd', $null)
      #If we find an older file then the one we currently have in memory, re-assign
      if($parsedDate -lt $oldestDate) {
        Write-Host 'Older file found than:' $oldestDate ', oldest is now: ' $i
        $oldestDate = $parsedDate;
        $oldestFile = $i;
      }
    }
    
    # Display and copy
    Write-Host 'Oldest file found:' $oldestFile
    

    I created a directory: C:\Temp with the files: Files.2021-04-21.zip up to Files.2021-04-26.zip

    The output looks like this:

    Files found: Files.2021-04-26.zip Files.2021-04-25.zip Files.2021-04-23.zip Files.2021-04-22.zip Files.2021-04-21.zip Files.2021-04-21.zip
    
    Older file found than: 26-4-2021 10:17:01 , oldest is now:  Files.2021-04-26.zip
    Older file found than: 26-4-2021 00:00:00 , oldest is now:  Files.2021-04-25.zip
    Older file found than: 25-4-2021 00:00:00 , oldest is now:  Files.2021-04-23.zip
    Older file found than: 23-4-2021 00:00:00 , oldest is now:  Files.2021-04-22.zip
    Older file found than: 22-4-2021 00:00:00 , oldest is now:  Files.2021-04-21.zip
    Oldest file found:  Files.2021-04-21.zip
    

    This should be enough to get your assignment done :) Good luck!

    AGAIN, I want to stress that you are probably better off by looking at the date modified of the file instead of the filename.

    In that case, do this

    # Get files 
    $zipFilesInFolder = Get-Childitem –Path "C:\Temp" | Where-Object {!$_.PSIsContainer -and  ($_.Name -like "*Files*") } | Sort-Object  -Property LastWriteTime -Descending
    Write-Host 'Files found:' $zipFilesInFolder
    
    # Check each file
    Foreach ($i in $zipFilesInFolder) {
      $i # Shows files from top to bottom, based on last modified date
    }