batch-filecmd

Find and compare folder modification times


I ask for help It is necessary to compare the date and time of folder modification and if the folder has not been modified for more than 4 hours then echo 1 if it has been modified then 0

I found out the date and time of the change

FOR %A IN ("C:\Program Files") DO @ECHO=%~tA

Solution

  • When it comes to dates and cmd there are many ways, usually resorting in using some external command like powershell, vbs and a couple of other methods.

    I would, in this instance, suggest resorting to powershell to make life easier for you.

    There are a few ways to do this in PS, but personally, using epoch time just makes calculations much simpler. With epoch time we're dealing with seconds and can do simple maths, i.e 14400 seconds = 4 hours.

    $now = [datetimeoffset]::UtcNow.ToUnixTimeSeconds()
    $file = [datetimeoffset]::new((Get-Item "C:\Program Files\").LastWriteTimeUtc).ToUnixTimeSeconds()
    
    if ($now - $file -gt 14400) {
        Write-Output "Older than 4 hours"
    } else {
        Write-Output "Modified within last 4 hours"
    }
    

    You can edit the output as you please.

    If you really want to use this in a batch-file, you can just incorporate this function. Sadly, you did not give enough context to be able to provide you with a complete solution.