Avatar of namerg
namerg
Flag 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

Powershell

Avatar of undefined
Last Comment
namerg

8/22/2022 - Mon
Mazdajai

$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.

    }
SubSun

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

ASKER
I will give it a shot...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Mazdajai

Have you try the above code? It should give you the days old.
namerg

ASKER
@Mazdajai, I just got to the office. Will try..
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 ?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SubSun

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

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
footech

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SubSun

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
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.
SubSun

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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
namerg

ASKER
All right. Got it.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.