Search code examples
datebatch-filewindows-7last-modified

Check if file was modified after xx days in the past


I am checking for the date modified in a file. I would like to test whether the file was modified after the /d -3. According to the docs, this will check if the file was modified before that date. I need to check if the file was modified after that date. The docs also state that I can specify a date. I might end up doing it this way though it would be a little more complicated to generate a date to check against which I would prefer to avoid if possible. How might I go about this?

forfiles /m Item_Lookup2.csv /d -3 >nul 2>nul && (
  echo - updated

  goto :PROCESS_FILE
) || (

  echo - out of date
  set outdated="true"

  goto :CHECK_ERRORS
)

I found this in this answer


Solution

  • You're on the right track, but forfiles /d -n tests for files modified n days or earlier, not later. What you need to do is reverse your && and || code blocks, and maybe specify 4 days instead of 3.

    If match, then it's 4 days old or older, and classified as out of date. If no match, it's been updated in the past 3 days.

    Try this:

    forfiles /d -4 /m "Item_Lookup2.csv" >NUL 2>NUL && (
        echo - out of date
        set outdated="true"
        goto :CHECK_ERRORS
    ) || (
        echo - updated
        goto :PROCESS_FILE
    )
    

    Bonus tip: If you'd like to do some testing, you can manipulate the last modified timestamp of a file manually using a PowerShell command:

    powershell "(gi Item_Lookup2.csv).LastWriteTime='6/1/2015 09:30:00 AM'"
    

    ... will set the last modified timestamp of Item_Lookup2.csv to June 1 at 9:30 AM. Salt to taste.