How to Get a File's Last Modified Timestamp in Batch Script
Retrieving a file's last modified date and time is a common need in automation. It allows you to check if a file has changed, determine if a backup is outdated, or log when a specific data file was last updated. Windows Batch provides a direct method to access this metadata using a FOR loop parameter expansion.
This guide will teach you how to use the %%~tF modifier to get a file's timestamp, store and parse it for use in script logic, and introduce a more robust method using WMIC to avoid common regional formatting issues.
The Core Method: Using FOR Command Expansion
The standard, built-in way to get a file's last modified timestamp is with the FOR command, which provides a special modifier to access file metadata.
The key is the ~t modifier applied to the loop variable.
FOR %%F IN (filename.txt) DO ...: Sets up a loop where%%Fwill hold the filename.%%~tF: Inside the loop, this syntax expands%%Fto the last modified date and time of the file.
Basic Example: Echoing the Full Timestamp
This script demonstrates the most direct way to print the timestamp for a specific file.
Assume we have a file named application.log.
@ECHO OFF
REM This script displays the last modified timestamp of "application.log".
FOR %%I IN ("application.log") DO (
ECHO The file "%%I" was last modified on: %%~tI
)
Output (The output format depends on your Windows regional settings. A typical US-English format is the following):
The file "application.log" was last modified on: 10/27/2023 10:15 AM
Storing the Timestamp in a Variable
To use the timestamp for comparisons or logging, you need to store it in a variable.
This script retrieves the timestamp and saves it to a variable named LAST_MODIFIED.
@ECHO OFF
SET "FILENAME=application.log"
SET "LAST_MODIFIED="
REM Use the FOR loop to get the timestamp and SET the variable.
FOR %%I IN ("%FILENAME%") DO SET "LAST_MODIFIED=%%~tI"
IF DEFINED LAST_MODIFIED (
ECHO Last modified time: %LAST_MODIFIED%
) ELSE (
ECHO Could not find file: "%FILENAME%"
)
Output:
Last modified time: 10/27/2023 10:15 AM
A More Robust Method for Consistent Formatting: WMIC
A major weakness of %%~tF is that its output format changes based on the user's region settings, making string comparisons unreliable across different machines. For a consistent, sortable format, WMIC (Windows Management Instrumentation Command-line) is a much better tool.
WMIC can return a timestamp in the format YYYYMMDDHHMMSS.fractionalseconds, which is perfect for comparisons.
This script uses WMIC to get the timestamp of kernel32.dll.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "TARGET_FILE=C:\\Windows\\System32\\kernel32.dll"
REM The WMIC command returns extra lines, so we use a FOR /F loop to capture the correct line.
FOR /F "skip=1 delims=" %%A IN ('WMIC DATAFILE WHERE "Name='%TARGET_FILE%'" GET LastModified') DO (
SET "WMIC_TIMESTAMP=%%A"
GOTO :Found
)
:Found
ECHO WMIC timestamp: !WMIC_TIMESTAMP!
Output (a clean, internationally-standard timestamp)
WMIC timestamp: 20230507011634.123456+060
This format is far easier to work with for reliable date comparisons. You can take a substring to get just the YYYYMMDDHHMMSS part.
Common Pitfalls and How to Solve Them
Problem: Handling "File Not Found" Errors
If the file you are checking does not exist, the body of the FOR %%I IN (...) loop will not run, and your variable will not be set.
Solution: Use IF EXIST First
Always check that the file exists before you attempt to get its metadata. This prevents logical errors in your script.
@ECHO OFF
SET "FILENAME=non_existent_file.log"
IF NOT EXIST "%FILENAME%" (
ECHO Error: The file "%FILENAME%" could not be found.
GOTO :EOF
)
FOR %%I IN ("%FILENAME%") DO SET "LAST_MODIFIED=%%~tI"
ECHO Last modified: %LAST_MODIFIED%
Output:
# Output: A clear and accurate error message.
Error: The file "non_existent_file.log" could not be found.
Problem: Parsing Locale-Specific Timestamps
If you need to compare just the date or time from the %%~tF output, you must parse the string. This is unreliable because the format (MM/DD/YYYY vs. DD-MM-YYYY) and delimiter (/ vs. -) can change.
Example of unreliable approach:
@ECHO OFF
REM This might work on YOUR machine, but it's not portable.
FOR %%I IN ("application.log") DO SET "TIMESTAMP=%%~tI"
FOR /F "tokens=1,2 delims=/ " %%A IN ("%TIMESTAMP%") DO (
ECHO Month: %%A, Day: %%B
)
Robust Solution: Parse the WMIC Output
The WMIC output is always in the same order, so you can reliably extract parts of it using substring operations.
@ECHO OFF
SETLOCAL
SET "TARGET_FILE=C:\\Windows\\System32\\kernel32.dll"
FOR /F "skip=1 delims=" %%A IN ('WMIC DATAFILE WHERE "Name='%TARGET_FILE%'" GET LastModified') DO (
SET "TS=%%A"
GOTO :Continue
)
:Continue
SET "YYYY=%TS:~0,4%"
SET "MM=%TS:~4,2%"
SET "DD=%TS:~6,2%"
ECHO Date components: YYYY=%YYYY%, MM=%MM%, DD=%DD%
Output:
Date components: YYYY=2023, MM=05, DD=07
Practical Example: Check if a Source File is Newer Than its Backup
This script compares the timestamps of a source file and its backup. If the source is newer, it performs the copy. This demonstrates a reliable string comparison using the WMIC format.
@ECHO OFF
SETLOCAL
SET "SOURCE_FILE=MyDocument.txt"
SET "BACKUP_FILE=MyDocument_backup.txt"
REM Get timestamp for the source file
FOR /F "skip=1" %%A IN ('WMIC DATAFILE WHERE Name^="%CD%\\%SOURCE_FILE%" GET LastModified') DO SET "SOURCE_TS=%%A"
REM Get timestamp for the backup file
FOR /F "skip=1" %%A IN ('WMIC DATAFILE WHERE Name^="%CD%\\%BACKUP_FILE%" GET LastModified') DO SET "BACKUP_TS=%%A"
ECHO Source timestamp: %SOURCE_TS%
ECHO Backup timestamp: %BACKUP_TS%
IF "%SOURCE_TS%" GTR "%BACKUP_TS%" (
ECHO The source file is newer. Copying...
COPY /Y "%SOURCE_FILE%" "%BACKUP_FILE%"
) ELSE (
ECHO The backup is up-to-date.
)
This works because the YYYYMMDDHHMMSS format can be compared alphabetically (GTR means "Greater Than").
Conclusion
Getting a file's last modified time is straightforward in batch scripting, but choosing the right method is key to reliability.
- For quick, simple tasks where the script will only run on your own machine, the
%%~tFmodifier is fast and effective. - For robust, portable scripts that need to work reliably across different computers and regions,
WMIC DATAFILE GET LastModifiedis the superior choice, as it provides a consistent, easily comparable timestamp format. - Always remember to use
IF EXISTto handle cases where the file may be missing.