Howto echo or get file timestamp (date) in command (cmd) .bat script

By neokrates, written on October 30, 2010

cmd bat   snippet

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 3,968 views
Rate it
Ad
Poll
  • Do you plan to migrate to new SCM?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

Given, myfile.txt exists and in same directory you run commands below.

Print timestamp:

for %a in (myfile.txt) do echo %~ta

Get timestamp:

for %a in (myfile.txt) do set myfiledate=%~ta

Then you can use it like :

echo %myfiledate%
12/15/2015 11:23 AM

💡 To use the myfiledate inside the FOR loop you will probably need to enable delayed expansion (ENABLEDELAYEDEXPANSION), like:

@echo off

@SETLOCAL ENABLEDELAYEDEXPANSION
 
for %%a in (myfile.txt) do (
  set myfiledate=%%~ta
  echo !myfiledate!
)

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +5 (from 15 votes)
10 votes 'YES'  5 votes 'NO'
TAGS
RELATED
Pages
Posts
SOCIAL
Be Sociable, Share!

Leave a Reply