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).by MattG at 2012-11-12 15:35:26
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
You need to iterate over each file returned. The following should do the trick for you:by Lahru at 2012-11-13 08:53:04$pdfs = Get-ChildItem "C:\Some\Path" -Recurse -Include '.pdf'
That should get you going.
foreach ($pdf in $pdfs) { ((Get-Date) - $pdf.LastWriteTime).TotalMinutes }
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.by MattG at 2012-11-13 09:05:20
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
I hope that makes sense. How do I work in the if statements?
$pdfs = Get-ChildItem $dir -Include .pdf
$tifs = Get-ChildItem $dir -Include .tif
$TimeFrame = 2
foreach ($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}'
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:by Lahru at 2012-11-13 09:25:37$path = \some\path*
$files = Get-ChildItem $path -Include '.pdf','
.tif'
Also, I would have Get-ChildItem only return files:
$TimeFrame = 2
foreach ($file in $files) { ((Get-Date) - $file.LastWriteTime).TotalMinutes }
v2 Syntax:Get-ChildItem $path -Include '
.pdf','.tif' | ? { ! $_.PSIsContainer }
v3 Syntax:Get-ChildItem $path -Include '.pdf','.tif' -File
DOH! Should have seen that.by Lahru at 2012-11-13 11:18:42
What about the IF portion(s)?
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 } }