question

SalmanSayeed-8316 avatar image
0 Votes"
SalmanSayeed-8316 asked IanXue-MSFT edited

Powershell check files today in a directory

  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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

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
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SalmanSayeed-8316 avatar image
0 Votes"
SalmanSayeed-8316 answered SalmanSayeed-8316 commented

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"
}


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I mistakenly used single-quotes on the last Write-Host. They should be double-quotes. I fixed the code sample I posted.

0 Votes 0 ·
SalmanSayeed-8316 avatar image
0 Votes"
SalmanSayeed-8316 answered

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'
  }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.