title: How do I compare timestamps of files in a batch script?
body: <p>In DOS batch files, the method of achieving certain things is somewhat obfuscated. Fortunately, there is a fantastic reference site for batch scripting: <a href="http://ss64.com/nt/" rel="nofollow noreferrer">Simon Sheppard's SS64</a>. (The same site also has plenty of information about <em>Bash</em>.)</p>
  
  <p>One difficulty is branching execution based on whether a directory is empty.
  The obvious <code>if exist "%dir%\*.*"</code> doesn't work. But it can be done with this conditional execution trick:</p>
  
  <pre><code>( dir /b /a "%dir%" | findstr . ) &gt; nul &amp;&amp; (
    echo %dir% non-empty
  ) || (
    echo %dir% empty
  )
  </code></pre>
  
  <p>Another awkward problem is branching according to file contents.
  Again that can be done like this:</p>
  
  <pre><code>( fc /B "%file1%" "%file2%" | find "FC: no differences encountered" ) &gt; nul &amp;&amp; (
    echo "%file1%" and "%file2%" are the same
  ) || (
    echo "%file1%" and "%file2%" are different
  )
  </code></pre>
  
  <p><strong>So, my question is:<br>
  Is there a way to do branch according to the time-stamps of files?</strong></p>
  
  <p>This is the sort of thing I want:</p>
  
  <pre><code>REM *** pseudo-code!
  if "%file1%" is_newer_than "%file2%" (
    echo "%file1%" is newer
  ) else if "%file1%" is_older_than "%file2%" (
    echo "%file2%" is newer
  ) else (
    echo "%file1%" and "%file2%" are the same age
  )
  </code></pre>
  
  <p>Thanks.</p>