Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
share|improve this question
    
Could you give an example of what you want/expect? –  mailq Aug 4 '11 at 17:47

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
It works this way in XP as well. –  RobW Aug 8 '11 at 22:56
    
@RobW: Thanks, I didn't have any XP boxes around to check myself. –  Tonny Aug 9 '11 at 12:52
    
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: 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 9 '11 at 22:54
    
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

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:\]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.