powershell check files and creation time and alert user

Server-IT 41 Reputation points
2021-06-16T12:40:32.9+00:00

Here is my script to check number of files in a given directory.

Need some help - the script below write to host files found in green and files not found from the list - which works fine

  1. I want to place a condition where all files found in the directory are of today or certain time and no older the 24 hours
  2. if they are older - send the filename and date created via smtp email
  3. if files are of different times created 24 hours apart, smtp email
  4. if all files are created less than 24 hours than copy into a different folder.

here is my script

$folder = 'D:\test'
    $files = @(
        "xyz.dat",
        "two.txt"
    )
    Write-Host "Folder: $folder."
    # Get only files and only their names
    $folderFiles = Get-ChildItem -Path $folder -Recurse -File -Name
    foreach ($f in $files) {
        if ($folderFiles -contains $f) { 
            Write-Host "File $f was found." -foregroundcolor green
        } else { 
            Write-Host "File $f was not found!" -foregroundcolor red 
        }
    }
Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Anonymous
    2021-06-17T03:02:01.057+00:00

    Hi,

    For files in subfolders "Get-ChildItem -Recurse -Name" returns "subfolder\filename", not only filenames. You may start with this.

    $folder ="D:\test1"  
    $files = @(  
        "xyz.dat",  
        "two.txt",  
        "123.jpg"  
    )  
    $FolderToday = "D:\test2\today"  
    $FolderNew = "D:\test2\new"  
    $FolderOld = "D:\test2\old"  
    $TodaysFiles = @()  
    $NewFiles = @()  
    $OldFiles = @()  
    $folderFiles = Get-ChildItem -Path $folder -Recurse -File | Where-Object {$_.Name -in $files}   
    $folderFiles | ForEach-Object {  
        if($_.CreationTime.Date -eq [datetime]::Today){  
            $TodaysFiles += $_  
        }  
        elseif (([datetime]::Now - $_.CreationTime) -lt [timespan]::FromDays(1)){  
            $NewFiles += $_  
        }  
        else{  
          $OldFiles += $_  
        }  
    }  
    if($folderFiles.Count -eq 0){  
        #send email, saying no files in list are found in the folder.  
    }  
    else{  
        if($TodaysFiles.Count -ne 0){  
            Copy-Item -Path $TodaysFiles.FullName -Destination $FolderToday   
            #send email  
        }  
        if($NewFiles.Count -ne 0){  
            Copy-Item -Path $NewFiles.FullName -Destination $FolderNew  
            #send email  
        }  
        if($OldFiles.Count -ne 0){  
            Copy-Item -Path $OldFiles.FullName -Destination $FolderOld   
            #send email  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-06-17T08:52:49.423+00:00

    Hi,

    Please try this.

    $folder ="D:\test1"  
    $files = @(  
        "xyz.dat",  
        "two.txt",  
        "123.jpg"  
    )  
    $FolderToday = "D:\test2\today"  
    $FolderNew = "D:\test2\new"  
    $oldFolder = "D:\test2\old"  
    $FolderOld = @()  
    $NewFiles = @()  
    $OldFiles = @()  
    $folderFiles = Get-ChildItem -Path $folder -Recurse -File | Where-Object {$_.Name -in $files}   
    $folderFiles | ForEach-Object {  
        if($_.CreationTime.Date -eq [datetime]::Today){  
            $TodaysFiles += $_  
        }  
        elseif (([datetime]::Now - $_.CreationTime) -lt [timespan]::FromDays(1)){  
            $NewFiles += $_  
        }  
        else{  
          $OldFiles += $_  
        }  
      
    }  
      
    if($folderFiles.Count -eq 0){  
        #send email, saying no files in list are found in the folder.  
    }  
    else{  
        if($TodaysFiles.Count -ne 0){  
            Copy-Item -Path $TodaysFiles.FullName -Destination $FolderToday  
            $TodaysFiles | Select-Object Name, LastWriteTime, CreationTime | Export-Csv -Path "$FolderToday\today.csv" -NoTypeInformation  
            #send email  
        }  
        if($NewFiles.Count -ne 0){  
            Copy-Item -Path $NewFiles.FullName -Destination $FolderNew  
            $NewFiles | Select-Object Name, LastWriteTime, CreationTime | Export-Csv -Path "$FolderNew\new.csv" -NoTypeInformation  
            #send email  
        }  
        if($OldFiles.Count -ne 0){  
            Copy-Item -Path $OldFiles.FullName -Destination $FolderOld   
            $OldFiles | Select-Object Name, LastWriteTime, CreationTime | Export-Csv -Path "$FolderOld\old.csv" -NoTypeInformation  
            #send email  
        }  
        if($OldFiles.count -eq $files.count){  
            $OldFiles | Select-Object Name, LastWriteTime, CreationTime | Export-Csv -Path "$FolderOld\NoNewFile.csv" -NoTypeInformation  
            #send email  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.