# Checking Time Variance on multiple files

**URL:** <https://forums.powershell.org/t/checking-time-variance-on-multiple-files/325>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/checking-time-variance-on-multiple-files/325 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/checking-time-variance-on-multiple-files/325/1 "2011-12-31T18:30:00Z")

</div>

by Lahru at 2012-11-12 12:15:54

> I have process in place in my network where files are moved from 1 directory to another via script(s).  
> Then there is a service that looks at that other directory and imports either .pdf, .tif, or .jpg files into a program.  
> This process of importing occurs once per minute during a specified period of time, something like 16 hours.  
> Once the files are successfully imported they are auto-removed from this "IMPORT" directory  
>   
> Sometimes the service hangs or otherwise fails to complete an import, but remains in a ‘running’ state.  
>   
> If I can check the LastWriteTime of the files in the "import" directory and compare to the current system time I can launch an email at admins notifying them that something is wrong, IF the variance is outside of a specified time frame.  
>   
> **Problem is] Multiple files. There are sometimes multiple files being dealt with so I might have 2 or more PDFs that got moved into here, but the service didn’t do the actual import and as a result they might remain behind. Also, the name of the file(s) will never be the same.**  
>   
> **Is there a way for me to handle multiple files like this?  
> Multiple file Types as well? Like pdf &/OR jpg?**  
>   
> **I have tried the following, but to no avail:  
> `$dir = "\Some\path"$pdf = Get-ChildItem $dir*.pdf$Variance = ((Get-Date) - $pdf.LastWriteTime).TotalMinutes`  
> I tried to work this out by entering each line of code 1x1 into a console, but got this on the last:  
> `Cannot find an overload for "op_Subtraction" and the argument count: "2".At line:1 char:1+ $Variance = ((Get-Date) - $tif.LastWriteTime).TotalMinutes`**

by MattG at 2012-11-12 15:35:26

> You need to iterate over each file returned. The following should do the trick for you:  
> `$pdfs = Get-ChildItem "C:\Some\Path&quot; -Recurse -Include '.pdf'foreach ($pdf in $pdfs) { ((Get-Date) - $pdf.LastWriteTime).TotalMinutes }`_That should get you going._

_by Lahru at 2012-11-13 08:53:04_

_> Ok so I can see how that works. It doesn’t need to be recursive since files in the subfolders shouldn’t trigger any action.  
> I need to include some kind of If-Else statement though. I also need to include more than one type of file. Can I do 2 foreach loops? Like so]$dir = \Some\Path  
> $pdfs = Get-ChildItem $dir -Include .pdf  
> $tifs = Get-ChildItem $dir -Include .tif  
>   
> foreach ($pdf in $pdfs) { ((Get-Date) - $pdf.LastWriteTime).TotalMinutes }  
> foreach ($tif in $tifs) { ((Get-Date) - $tif.LastWriteTime).TotalMinutes }[/powershell]Here is the Logic I am trying to follow`$path = \some\path$pdfs = Get-ChildItem $dir -Include .pdf$tifs = Get-ChildItem $dir -Include .tif$TimeFrame = 2foreach ($pdf in $pdfs)	{ ((Get-Date) - $pdf.LastWriteTime).TotalMinutes }	If 'Time variance for any Pdf is -gt $TimeFrame {do something} else {do nothing}'	foreach ($tif in $tifs) { ((Get-Date) - $tif.LastWriteTime).TotalMinutes }	If 'Time variance for any Tif is -gt $TimeFrame {do something} else {do nothing}'` I hope that makes sense. How do I work in the if statements?_

_by MattG at 2012-11-13 09:05:20_

_> No need to do that twice. The -Include option takes an array of strings (check out Get-Help Get-ChildItem). The following will handle both pdf and tif files:  
> `$path = \some\path*$files = Get-ChildItem $path -Include '``.pdf','``.tif'$TimeFrame = 2 foreach ($file in $files) { ((Get-Date) - $file.LastWriteTime).TotalMinutes }`Also, I would have Get-ChildItem only return files:  
>   
> v2 Syntax: `Get-ChildItem $path -Include '``.pdf','.tif' | ? { ! $_.PSIsContainer }`v3 Syntax: `Get-ChildItem $path -Include '.pdf','.tif' -File`_

_by Lahru at 2012-11-13 09:25:37_

_> DOH! Should have seen that.  
> What about the IF portion(s)?_

_by Lahru at 2012-11-13 11:18:42_

_> Does this look right?`$dir = "\Some\Path"$TimeFrame = 2$files = Get-ChildItem $dir -Include '``.pdf', '*.tif' | ? { ! $_.PSIsContainer }foreach ($file in $files)	{ if (((Get-Date) - $file.LastWriteTime).TotalMinutes -gt $TimeFrame) { Send-MailMessage -From Sender@Somewhere.net -To "Recipient <Recipient@Somewhere.net>" -Subject "Subject" -Body "Body of the email message" -SmtpServer ThisSmtpServer.Somewhere.net } }`_

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:02pm UTC](https://forums.powershell.org/t/checking-time-variance-on-multiple-files/325/2 "2024-05-16T21:02:20Z")

</div>

Closed on May 16, 2024, 9:02pm UTC


