# Checking multiple files ...

**URL:** <https://forums.powershell.org/t/checking-multiple-files/345>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/checking-multiple-files/345 "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-multiple-files/345/1 "2011-12-31T18:30:00Z")

</div>

by Lahru at 2012-11-19 11:26:40

> Is there a way to do this?  
> For each/all files in a directory.  
> Subtract the file’s modified time from the current time  
> then  
> If the time frame for ANY file is outside of a specified timeframe  
> Complete an action?  
>   
> My purpose in this is to check up on a proprietary service that sometimes hangs but doesn’t stop.  
> If I could script the logic outlined above, then finding a file that is so many minutes old would indicate that the service isn’t working and I’d like to then trigger an alert.

by DonJ at 2012-11-19 11:32:26

> Yeah, pretty easily.   
>   
> `Get-ChildItem -path \whatever | ForEach {}`  
>   
> Inside that ForEach block, you’d put your calculation and use an If construct to check your timeframe. Something like $_.LastWriteTime - $_.CreationTime would be the calculation.  
>   
> In v3, add the -File parameter to Get-ChildItem so that it doesn’t return directories, if there are subfolders in there. Add -recurse to recurse files in subfolders.

by Lahru at 2012-11-19 12:25:52

> OK, awesome, so that gets me here: `$files = get-childitem -path \any\where* -include *.pdf, *.tif, *.jpgForeach ($file in $files)	{ $time = ((get-date) - $file.LastWriteTime).Totalminutes IF($time -gt 2) { Write-Host "$file is $time minutes old." }	}`What I’d like to do is send an email rather than write-host and I can do this with the send-MailMessage Cmdlet, but I don’t need a message per file.  
> So is there a way to get only 1 ACTION if 2 files return $True?  
>   
> I could write a new-item to a specific directory and then check that directory sending a message if a Trigger File exists, but I think that is getting too complicated.

by DonJ at 2012-11-19 12:39:03

> Write-Host is evil ;).  
>   
> Just accumulate your message in a variable:  
>   
> $message = ‘’  
>   
> And:  
>   
> $message += "$file is $time minutes old"  
>   
> You can pass that to Send-MailMessage at the end of your script. Is $message -eq ‘’ at the end, then don’t send anything. I could probably turn this into a one-liner, even.  
>   
> `$mailbody = Dir \any\where* -include .txt,.pdf | where { ((get-date) - $.lastwritetime).totalminutes -gt 2 } | Select @{n=‘File’;e={$.name}},@{n=‘Minutes Old’;e={((get-date) - $.lastwritetime).totalminutes}} `  
>   
> _Then just use Send-MailMessage and $mailbody. Shame Send-Mail doesn’t accept pipeline input. Coulda made it a true one-liner._

_by Lahru at 2012-11-20 08:18:35_

_> Ok I am not completely certain what is happening in your one liner. But I need to refine my trigger for the action regardless of the action.  
>   
> I want to send an alert or complete an action such as restarting a service,   
> but I don’t want to send out 50 messages (1 for each file) & I don’t want to restart a service 50 times either.  
> **How would I get 1 single action out of this?**  
> So in other words; regardless of whether I get 1 old file or 100 I want only 1 alert. How can I do that?  
>    
> my attempts to run the one-liner against a directory I know has files that match the "too old" parmeters I get this]Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.[/powershell]_

_by DonJ at 2012-11-20 08:26:34_

> _`$mailbody = Dir \any\where* -include .txt,.pdf | where { ((get-date) - $`_`.lastwritetime).totalminutes -gt 2 } | Select @{n=‘File’;e={$.name}},@{n=‘Minutes Old’;e={((get-date) - $.lastwritetime).totalminutes}} if ($mailbody) { Send-MailMessage -To x -From x -Body $mailbody }`  
>   
> The one-liner is just doing what your script was doing. If you prefer that:  
>   
> `$files = get-childitem -path \any\where* -include *.pdf, *.tif, *.jpg$message = ‘’Foreach ($file in $files) { $time = ((get-date) - $file.LastWriteTime).Totalminutes IF($time -gt 2) { $message += "$file is $time minutes old.`n" } }if ($message -ne ‘’) { Send-MailMessage -To x -From x -Body $message }`  
>   
> You got the error because, I suspect, you tacked Send-MailMessage onto the end of what I gave you. Can’t do that, because nothing in it accepts pipeline input.

---

<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:03pm UTC](https://forums.powershell.org/t/checking-multiple-files/345/2 "2024-05-16T21:03:49Z")

</div>


