Link to home
Start Free TrialLog in
Avatar of namerg
namergFlag for United States of America

asked on

How to count the number of days since a file has been touched against todays'date

Hello,
I would like to know how to count the number of days/months that a file has been modified.
For example if file xyx was modified on March 31 and is withing the range of 30 days/1 month, it is ok. But, if the file was modified or last touch on Feb 28, hmm there is a problem.
I have this code but I am stuck.
$now = Get-Date
#$strDate = "{0:yyyyMMdd}" -f ($now)
#$now = $now.ToString("MMddyyyy")

$folder = "\\jupiter\Shared\PA\ARS\REMITS"
#$files0 = gci $folder | Where-Object {$_.LastWriteTime.ToString("yyyyMMdd") -lt $strDate }
$files0 = gci $folder | ForEach-Object { ($_.LastWriteTime)}
foreach ($strDates in $files0)
{
 $difference = $now - $strDates
}
#if $files0.LastWritetime
#$days = $now - $files0.LastWriteTime
$Bingo = 1

Open in new window

Avatar of Mazdajai
Mazdajai
Flag of United States of America image

$fo = "\\jupiter\Shared\PA\ARS\REMITS"
foreach($fi in $fo)
 {
$ti = ((Get-Date) - $fi.CreationTime).Days
write-host "$fi.Name" is ((Get-Date) - $fi.lastwriteTime).Days old.

    }
@namerg, If you just want to count days, in your script $difference.days will give you the count..
Avatar of namerg

ASKER

I will give it a shot...
Have you try the above code? It should give you the days old.
Avatar of namerg

ASKER

@Mazdajai, I just got to the office. Will try..
Avatar of namerg

ASKER

@Mazadaji, it works 90% but I do not want to list all the files.
REMIT-US11-CLD-835RAW-033013-0549.RCV.Name is 2 old.
REMIT-US11-CLD-835RAW-033013-0550.RCV.Name is 2 old.
REMIT-US19-CLD-835RAW-032613-0089.RCV.Name is 7 old.
REMIT-US19-CLD-835RAW-032713-0090.RCV.Name is 7 old.
REMIT-US19-CLD-835RAW-032813-0091.RCV.Name is 6 old.
REMIT-US19-CLD-835RAW-033013-0092.RCV.Name is 2 old.
REMIT-US19-CLD-835RAW-040213-0093.RCV.Name is 1 old.

Open in new window

From the listing need to check the closest one to today's date, if that closest one is older than 30 days, there is a problem otherwise is ok. Note those filenames are randomly created, not fixed.
By the way, why it lists .Name ?
Try this and see if it works for you.. It should list the closest modified one from the files..
$now = Get-Date
$folder = "\\jupiter\Shared\PA\ARS\REMITS"
$files0 = GCI $folder
$(foreach ($strDates in $files0)
{
 New-Object PSObject -Property @{
 File = $strDates.FullName
 "Old in Days" = ($now - $strDates.LastWriteTime).Days
 }
}) | Sort -Property "Old in Days" | Select File,"Old in Days" -First 1

Open in new window

Avatar of namerg

ASKER

Error:
Unexpected token 'Old in Days' in expression or statement.
At C:\scripts\FTP_Tasks\11_Remits.ps1:6 char:74
+  New-Object PSObject -Property @{ File = $strDates.FullName "Old in Days" <<<<
  = ($now - $strDates.LastWriteTime).Days }
    + CategoryInfo          : ParserError: (Old in Days:String) [], ParentContai
nsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

____________________________________________________________________________

Open in new window

SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you using the same code which I posted? It seems like the "File" and "Old in Days" are in same line.. If yes you need to separate them with semicolon..

File = $strDates.FullName;"Old in Days"  = ($now - $strDates.LastWriteTime).Days

Open in new window

Avatar of namerg

ASKER

I do not want the list of all the files neither want them sort.
I care about the LastWriteTime of those agains todays date.
How many days have passed, if the file closest to todays date is more than 30 days, there is a problem. Otherwise, is ok..
@footech, I tried your code and does not list anything.
In that case what footech mentioned should work..
$date = (Get-Date).AddDays(-30)
$folder = "\\jupiter\Shared\PA\ARS\REMITS"
If (gci $folder | Where {$_.LastWriteTime -lt $date} | Sort LastWriteTime -Descending  | Select -First 1 ){
Write-host "There is a problem"
}
Else
{
Write-host "OK"
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namerg

ASKER

All right. Got it.