Reputation: 13
I am writing a script to test for old files in a synchronized folder, and send a text message if a file is older than 2 hours (this would mean that a file failed to update). I haven't included the "texting" part, because that works fine.
When I run my script I get three date:times for many of the files. Why do I get three values for the "last" writetime? And, how do I only look at the most recent "last" writetime?
Here's my script...
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
Here's my output...
TX029.7Z
Wednesday, September 13, 2017 11:44:54 AM
Monday, November 13, 2017 8:01:34 PM
Monday, November 13, 2017 7:45:45 PM
TX059.7Z
Tuesday, August 22, 2017 7:56:18 PM
Monday, November 13, 2017 8:03:56 PM
Monday, November 13, 2017 8:41:05 PM
TX013.7Z
Monday, November 13, 2017 8:01:33 PM
Monday, November 13, 2017 7:33:20 PM
Friday, November 10, 2017 2:38:20 PM
TX050.7z
Monday, November 13, 2017 8:02:17 PM
Monday, November 13, 2017 9:46:19 AM
Tuesday, October 31, 2017 7:46:01 AM
Help would be greatly appreciated!!
I ran the code that Stephen suggested. I am still getting multiple values for LastWriteTime for many of my files.
This is the resultant output on 11/24/17 at 8:57 AM, when all files show a timestamp in a dir of approximately 8:46 AM or so.
PS C:\Windows\system32> $Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#this if statement does not return a true / false value
#PowerShell is using whether an object is returned by Get-ChildItem
# or not to try and determine a boolean value
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
#at this point all we know is that some file exists with a write time
#of more than 2 hours but it could be any file
#You then get the name but only of all *.7z files in the path
#but there's no guarantee that this is the file you need
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
#This line gets the lastwritetime for all files in the folder
#regardless of the lastwritetime of the file
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
TX025.7Z
Thursday, November 23, 2017 11:04:20 AM
Friday, November 24, 2017 8:46:54 AM
TX029.7Z
Wednesday, September 13, 2017 11:44:54 AM
Thursday, November 23, 2017 11:01:26 AM
Friday, November 24, 2017 8:45:45 AM
TX049.7Z
Thursday, November 23, 2017 11:03:18 AM
Wednesday, November 22, 2017 5:46:02 PM
GA007.7Z
Thursday, November 23, 2017 7:45:38 PM
Thursday, November 23, 2017 11:02:20 AM
GA004.7Z
Friday, November 24, 2017 8:47:01 AM
Thursday, November 23, 2017 11:30:53 AM
GA003.7Z
Friday, November 24, 2017 8:47:10 AM
Thursday, November 23, 2017 10:30:48 AM
MO001.7Z
Thursday, November 23, 2017 11:04:45 AM
Thursday, November 23, 2017 7:47:08 PM
TX059.7Z
Tuesday, August 22, 2017 7:56:18 PM
Thursday, November 23, 2017 11:03:48 AM
Friday, November 24, 2017 8:41:02 AM
AR001.7Z
Thursday, November 23, 2017 7:46:02 PM
Thursday, November 23, 2017 11:05:08 AM
TX013.7Z
Thursday, November 23, 2017 11:01:25 AM
Friday, November 24, 2017 8:34:11 AM
TX050.7z
Thursday, November 23, 2017 11:02:03 AM
Friday, November 24, 2017 8:46:19 AM
TX024.7Z
Thursday, November 23, 2017 11:03:48 AM
Thursday, November 23, 2017 8:40:36 PM
GA010.7Z
Friday, November 24, 2017 8:45:24 AM
Thursday, November 23, 2017 11:02:39 AM
TX002.7Z
Thursday, November 23, 2017 11:02:03 AM
Friday, November 24, 2017 8:43:45 AM
TX035.7Z
Thursday, November 23, 2017 11:03:17 AM
Friday, November 24, 2017 8:51:39 AM
PS C:\Windows\system32>
Upvotes: 0
Views: 7612
Reputation: 4081
You are overly complicating the process. You can simply add a select-object to grab the 2 properties you want.
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#Filter for *.7z, with a lastwritetime older than 2 hours
#Grab only the name and lastwritetime
Get-Childitem $FPath -Filter "*.7z" |
Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) } |
Select-Object Name, LastWriteTime
}
EDIT: Explaining the logic of the original process
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#this if statement does not return a true / false value
#PowerShell is using whether an object is returned by Get-ChildItem
# or not to try and determine a boolean value
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
#at this point all we know is that some file exists with a write time
#of more than 2 hours but it could be any file
#You then get the name but only of all *.7z files in the path
#but there's no guarantee that this is the file you need
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
#This line gets the lastwritetime for all files in the folder
#regardless of the lastwritetime of the file
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
Without seeing the original file structure its hard to tell exactly what you are working with but the above should explain why you are getting multiple lastwritetime values in your script.
Upvotes: 2