Powershell check files today in a directory

Server-IT 41 Reputation points
2021-06-16T11:57:00.557+00:00
  1. i need some help with the script below, want to check the files in a particular directory with UK system date - to check if the are created today - might be files comes from different country.
  2. further there may be some old files, if this is the case alert the user with number of old and new files in the folder List item
  3. if there are files created from today, than copy this files to a different location
    4.if there are no files in the folder, alert user via email to say no files arrived in the directory. $Directories = "d:\test" $validfiles = gci $directories | Where {$.Length -eq 0 -and $.CreationTime.Date -ne [datetime]::Today} if (!($validfiles)) { cls
    Write-Host 'No files are found from today' Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com
    }
    else
    { cls
    Write-Host 'files are found from today'
    Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com }
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,622 questions
No comments
{count} votes

Accepted answer
  1. Rich Matheisen 35,036 Reputation points
    2021-06-16T14:58:01.177+00:00

    This might be a starting point:

    $Directories = "d:\test"
    
    $OtherDirectory = "D:\OtherTest"
    $TodaysDirectory = "D:\TodayTest"
    
    $ThisDay = [datetime]::Today
    $OtherFiles = 0
    $TodaysFiles = 0
    Get-ChildItem $directories | 
        ForEach-Object{
            if ($_.Length -eq 0 -and $_.CreationTime.Date -ne $ThisDay){  # What about non-zero length files?
                $OtherFiles++
                Copy-Item . . .
            }
            elseif($_.Length -eq 0 -and $_.CreationTime.Date -eq $ThisDay){  # What about non-zero length files?
                $TodaysFiles++
                Copy-Item . . .
            }
        }
    
    if (-not $TodaysFiles){
        Clear-Host
        Write-Host 'No files are found from today, and $OtherFiles old files were found'
        Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com
    }
    elseif($OtherFiles){
        Clear-Host
        Write-Host "found $OtherFiles old files, and $TodaysFiles current files"
        Send-MailMessage . . . 
    }
    else{
        Clear-Host
        Write-Host "$TodaysFiles are found from today, and $OtherFiles old files were also found"
        Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com
    }
    
    No comments

3 additional answers

Sort by: Most helpful
  1. Server-IT 41 Reputation points
    2021-06-16T16:26:42.06+00:00

    when i run the script, i am getting this message, i would like to see the number of old files and new files names and actual numbers

    $TodaysFiles are found from today, 
    

    In my folder i got 3 files created today and nothing else.

    1. your question what about the files greater than 0
      copy them into another directory and list them in a csv file and email.

    Conditions i am trying

    1. check if there are any files in the directory and they are from todays creation, copy to another folder, list them in csv and email
    2. if there are no new files - check for old files and if found list them in csv and email
    3. if no files are found. just email, saying no files are found in the folder.

      check directory not empty - the script below works but the first two conditions point 1 and point 2 need some help.

      $directoryInfo = Get-ChildItem $directories | Measure-Object
      $directoryInfo.count #Returns the count of all of the objects in the directory
      if ($directoryInfo.count -ne 0) {

    All the code

    }
    else
    { clear-host
    "Directory is empty"
    }


  2. Server-IT 41 Reputation points
    2021-06-17T08:01:14.027+00:00

    here is my modified code and need some help stored the filename and cratiion date in a csv format.

    $Directories = "d:\test1"
    
     $OtherDirectory = "D:\OtherTest"
     $TodaysDirectory = "D:\TodayTest"
    
     $ThisDay = [datetime]::Today
     $OtherFiles = 0
     $TodaysFiles = 0
    
     $directoryInfo = Get-ChildItem $directories | Measure-Object
    $directoryInfo.count #Returns the count of all of the objects in the directory
    if ($directoryInfo.count -ne 0)
    
    {
     Get-ChildItem $directories | 
         ForEach-Object{
             if ($_.Length -eq 0 -and $_.CreationTime.Date -ne $ThisDay){  # What about non-zero length files?
                 $OtherFiles++
                 $outfile = $OtherFiles.FullName + "out" 
                 #Copy-Item . . .
             }
             elseif($_.Length -eq 0 -and $_.CreationTime.Date -eq $ThisDay){  # What about non-zero length files?
                 $TodaysFiles++
                 #Copy-Item . . .
             }
         }
    
     if (-not $TodaysFiles -and -not $OtherFiles){
         Clear-Host
         Write-Host "No files are found from today and old files"
         #Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com
     }
     elseif($OtherFiles){
         Clear-Host
         #Write-Host "found $OtherFiles old files, and $TodaysFiles current files"
         Write-Host "found $OtherFiles old files, and $TodaysFiles current files"
         #Send-MailMessage . . . 
     }
     else{
         Clear-Host
         Write-Host "$TodaysFiles are found from today"
         #Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com
     }
     }
     else
     {
     clear-host
     Write-Host '$directoryinfo directory is empty'
     }
    
    No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 18,351 Reputation points Microsoft Employee
    2021-06-17T08:30:15.027+00:00

    Hi,

    Please try this

    $Directories = "d:\test1"  
    $OtherDirectory = "D:\OtherTest"  
    $TodaysDirectory = "D:\TodayTest"     
    $ThisDay = [datetime]::Today  
    $OtherFiles = @()  
    $TodaysFiles = @()     
    $directoryInfo = Get-ChildItem $directories | Measure-Object  
    $directoryInfo.count #Returns the count of all of the objects in the directory  
    if ($directoryInfo.count -ne 0)  
    {  
        Get-ChildItem $directories |   
            ForEach-Object{  
                if ($_.Length -eq 0 -and $_.CreationTime.Date -ne $ThisDay){  # What about non-zero length files?  
                    $OtherFiles += $_  
                    $outfile = $OtherFiles.FullName + "out"   
                    #Copy-Item . . .  
                }  
                elseif($_.Length -eq 0 -and $_.CreationTime.Date -eq $ThisDay){  # What about non-zero length files?  
                    $TodaysFiles += $_  
                    #Copy-Item . . .  
                }  
            }          
        if (-not $TodaysFiles -and -not $OtherFiles){  
            Clear-Host  
            Write-Host "No files are found from today and old files"  
            #Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com  
        }  
        elseif($OtherFiles){  
            Clear-Host  
            #Write-Host "found $OtherFiles old files, and $TodaysFiles current files"  
            Write-Host "found $OtherFiles old files, and $TodaysFiles current files"  
            $OtherFiles | Select-Object Name, CreationTime | Export-Csv -Path "$OtherDirectory\otherfiles.csv" -NoTypeInformation   
            #Send-MailMessage . . .   
        }  
        else{  
            Clear-Host  
            Write-Host "$TodaysFiles are found from today"  
            $TodaysFiles | Select-Object Name, CreationTime | Export-Csv -Path "$TodaysDirectory\todayfiles.csv" -NoTypeInformation   
            #Send-MailMessage -To support@contoso.com -From server@contoso.com -Subject "Something wrong" -Body $body -SmtpServer smtp.contoso.com  
        }  
    }  
    else  
    {  
        clear-host  
        Write-Host '$directoryinfo directory is empty'  
    }  
    

    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.

    No comments