Filelist issue

Welcome Forums General PowerShell Q&A Filelist issue

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
9 months, 1 week ago.

  • Author
    Posts
  • #93049

    Participant
    Points: 0
    Rank: Member

    Hello All,

    I have wrote the below script to check that all of our manually backed up databases are present in the backup directory and have been written within the last 24 hours. The script works as intended (Happy with any input to improve it.) however, the sql backup appends the files with the date of the backup, this cannot be removed as it is apparently required by the development team. The problem with this is that the backup time spans across two date ranges as it runs late at night till early morning. Is there any way to add a wildcard into the name so that it does not care about the date?

    $MyFileList = @(
        "DatabaseName_Full_20180206.bak.zip"                                                                                 
    )
    
    $Current = gci "DIRECTORYNAME" -Recurse -File -Exclude '*.done','*.txt' | ?{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
    $Databases = 0
    
     $MyFileList |foreach {
        If (($Current).Name -contains $_) 
        {
            ++$Databases
        } 
        else 
        { 
            Write-Host "$_ is missing"
        }
    }
    If ($Databases -eq 24 ) {
            Write-Output "Databases OK" 
       }
    
  • #93100

    Participant
    Points: 134
    Helping Hand
    Rank: Participant

    Hmmm,
    I would recommend to specify the files you're after with a -Filter that includes the files you want and not to exclude everything else. If you need to compare objects you could use Compare-Object. I would "outsource" the file list to a csv file. That makes it easier to maintain it. So my approach would be something like this:

    $DesiredFileList = Import-Csv 'Path to the filelist csv' -Delimiter ','
    
    $CurrentFileList = Get-ChildItem -Path 'DIRECTORYNAME' -Filter *.zip |
        Where-Object {$_.LastWriteTime -gt ((Get-Date).AddDays(-1))} 
    
    Compare-Object -ReferenceObject $DesiredFileList -DifferenceObject $CurrentFileList -Property Name -IncludeEqual -PassThru 

    What I didn't get is what you mean with "add a wildcard into the name so that it does not care about the date". Instead of using the LastWriteTime you could use CreationTime. Or you could use the time stamp included in the file name.

The topic ‘Filelist issue’ is closed to new replies.