How to get last modified date on Windows command line for a set of files?

batch-file, command, windows

Solution

In the code that follows, change `%` to `%%` for use in batch file, for `%~ta` syntax enter `call /?`

for %a in (MyFile.txt) do set FileDate=%~ta

Sample output:

for %a in (MyFile.txt) do set FileDate=%~ta
set FileDate=05/05/2020 09:47 AM

for %a in (file_not_exist_file.txt) do set FileDate=%~ta
set FileDate=

Problem

I have been using the following command to get the file date. However, the `fileDate` variable has been returning blank value ever since we moved to a different server (Windows Server 2003). ``` FOR /f %%a in ('dir myfile.txt^|find /i " myfile.txt"') DO SET fileDate=%%a ``` Is there any other more reliable way to get the file date?

Original source