question

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

powershell check files and creation time and alert user

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-server-powershell
· 2
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.

Didn't you ask this same question earlier today?

0 Votes 0 ·

I agree very similar but in this case i got specific files to check and same conditions
but here specific files and all should be in to carry the copy process, if there are any missing files from the list.
list the available and missing in a csv file, email and do nothing.



  1. check if there are new files , todays created, copy this new files, 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.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered SalmanSayeed-8316 edited

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
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.

Brilliant

\is there a way, i could list the files found in a csv format, or just list them in an email.

name date modified date created


Also i need to check if all the files requested should be new or from today, if not list them in email or csv to say not all files are new.

 $files = @(
      "xyz.dat",
      "two.txt",
      "123.jpg"
  )


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

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.



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.