Home > front end >  How to get the amount of hours passed since the last modification of each file?
How to get the amount of hours passed since the last modification of each file?

Time:12-07

I am using this batch script to give me output of the last modified date using the Date and time format:

forfiles /M "my file.txt" /C "cmd /c echo @file @fdate @ftime" > ".\Logs\my file.txt"

But instead of date and time, I want it to show me when it was updated by hours, minutes and seconds.

For example, the above code output looks like this:

"my file.txt" 6/12/2022 2:44:58 AM

my desired result is:

"my file.txt" was updated 1 hours 3 minutes 2 seconds ago

How can get this result and which language is more convenient for this?

CodePudding user response:

A PowerShell solution:

The example uses:

Get-ChildItem -LiteralPath . -Filter *.txt | 
  ForEach-Object {
    $timeSpan = New-TimeSpan -Start $_.LastWriteTime
    "`"$($_.Name)`" was last updated {0} day(s), {1} hour(s), {2} minute(s), {3} second(s) ago" -f 
      $timeSpan.Days, $timeSpan.Hours, $timeSpan.Minutes, $timeSpan.Seconds -replace
        '\b0 day\(s\), (0 hour\(s\), (0 minute\(s\), )?)?'
  }

CodePudding user response:

As per my comment:

Get-ChildItem -Path 'D:\Temp\appsettings.txt' | Select-Object -Property '*' 
# These are all the possible properties in Windows for a file.
# Results
<#
 PSPath            : Microsoft.PowerShell.Core\FileSystem::D:\Temp\appsettings.txt
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::D:\Temp
PSChildName       : appsettings.txt
PSDrive           : D
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             D:\Temp\appsettings.txt
                    InternalName:     
                    OriginalFilename: 
                    FileVersion:      
                    FileDescription:  
                    Product:          
                    ProductVersion:   
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:         
                    
BaseName          : appsettings
Target            : {}
LinkType          : 
Name              : appsettings.txt
Length            : 190
DirectoryName     : D:\Temp
Directory         : D:\Temp
IsReadOnly        : False
Exists            : True
FullName          : D:\Temp\appsettings.txt
Extension         : .txt
CreationTime      : 25-Mar-21 10:38:14
CreationTimeUtc   : 25-Mar-21 17:38:14
LastAccessTime    : 17-Oct-22 23:00:44
LastAccessTimeUtc : 18-Oct-22 06:00:44
LastWriteTime     : 25-Mar-21 10:38:14
LastWriteTimeUtc  : 25-Mar-21 17:38:14
Attributes        : Archive
#>

Formatting dates is just this:

https://devblogs.microsoft.com/scripting/formatting-date-strings-with-powershell/

You can add any other string you want in that formatted output as well.

Get-ChildItem -Path 'D:\Temp\appsettings.txt' | 
Select-Object -Property FullName, LastWriteTime
# Results
<#
FullName                LastWriteTime     
--------                -------------     
D:\Temp\appsettings.txt 25-Mar-21 10:38:14
#>

Clear-Host
Get-ChildItem -Path 'D:\Temp\*.txt' | 
Select-Object -Last 1 | 
ForEach-Object {
    $FileTime = New-TimeSpan -Start $PSItem.LastWriteTime
    "$($PSItem.Name) was updated $($FileTime.Days) days $($FileTime.Hours) hours $($FileTime.Minutes) minutes $($FileTime.Seconds) Seconds ago"
}
# Results
<#
[abc] - Copy.txt was updated 930 days 9 hours 2 minutes 35 Seconds
#>
  • Related