2

I've been wondering, using this dos command

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

Is there a way to have the full date from the day to the second?

EDIT:

For now I get :

set FileDate=04/08/2011 15:37

What I want is :

set FileDate=04/08/2011 15:37:04

2 Answers2

2

The %~tX output uses the short-format as defined in your regional settings. Unfortunately the short-format can never include seconds (at least on Win7, not sure about other Windows versions).

As far as I know there is no way to do what you want using just cmd-script commands. You will need an extra utility that pulls the full date/time info from the file-system.

Tonny
  • 6,332
  • 1
  • 18
  • 31
  • 1
    It works this way in XP as well. – RobW Aug 08 '11 at 22:56
  • @RobW: Thanks, I didn't have any XP boxes around to check myself. – Tonny Aug 09 '11 at 12:52
  • 2
    If seconds are important, the data is stored, just not available via the FOR command. You can look at the GNU utils STAT.EXE command (via this project: http://gnuwin32.sourceforge.net/packages.html). STAT.EXE shows that seconds are stored. With only lite looking, I've not found another way to get to the seconds. – RobW Aug 09 '11 at 22:54
  • 2
    A port of stat would do the trick indeed. And bare in mind that FAT file-systems only store the seconds with 2 second resolution. – Tonny Sep 13 '11 at 11:02
-2

First: That's not a "DOS command". DOS has nothing to do with it and is not involved at all. It is the built in FOR command of CMD, Microsoft's default command interpreter that is bundled with Windows NT. The FOR command in Microsoft's COMMAND, its command interpreter for DOS, has no such functionality, not least because DOS has no notion of file creation timestamps.

Second: As a command interpreter built-in this sort of thing depends from what command interpreter one actually uses in the first place. What you want is dead easy to achieve with JP Software's command interpreter TCC, for example:

[C:\]echo %@filedate[config.sys,c,4] %@filetime[config.sys,c,s]
2008-11-07 18:11:38

[C:\]
JdeBP
  • 3,990
  • 18
  • 17