Batch File Timestamp

  • Thread starter Roger Grossenbacher
  • Start date
R

Roger Grossenbacher

Stephen said:
Phil,

thanks for this, it's proved most useful. However, when I
remove the 'echo' as suggested and run the script it
returns an error 'The syntax of the command is
incorrect'. When I view it in the command window it looks
fine to me.

I'm using the following line of code

for /f "tokens=1-5 delims=/:." %%a in ("%date%%time%") do
ren D:\MyTestFile.txt D:\MyTestFile-%%a%%b%%c%%d%%e.dat

The command window returns

D:\>ren D:\MyTestFile.txt D:\MyTestFileSat 13092003
93014.dat
The syntax of the command is incorrect

Does anyone have any further advice? I'd also like to
know how to trim out the spaces from the filename. Thanks.

This is happening because the builtin %date% and %time% variables perform
INCONSISTENTLY. They vary based on the operating system version and the
regional date/time settings.

There are three problems with using %date% and %time% for this purpose:

1. In your specific case, the Day Of Week is present in
%date%, so it is returning "Sat dd-mm-yyyy".
Type "ECHO ___%date%___" at a command prompt to see it.

2. %time% does not pad the hours with a zero when they are
less than 10. This will cause your file to be renamed
INCONSISTENTLY. For example:

MyTestFile1309200393014.dat {at 9:30am}
MyTestFile13092003103014.dat {at 10:30am}

Using the format "yyyymmddhhmnss", which represents

yyyy = four digit year
mm = two digit month (padded with zero for
months less than 10)
dd = two digit day
hh = two digit hour {padded with zero for
hours less than 10}
mn = two digit minute
ss = two digit seconds

will give you

MyTestFile20030913093014.dat {at 9:30am}
MyTestFile20030913103014.dat {at 10:30am}

which will always sort correctly for you.

3. The solution will only work in Windows 2000, XP and Server 2003.
The %date% and %time% variable are not available under Windows NT.

*******

The Mount/\Commands GetLogDate and GetLogTime overcome all three of these
problems for yourself (with your specific problem), and for anyone else who
would need to solve the same problem on their system at a later date.

GetLogDate CONSISTENTLY returns yyyymmdd
and saves the value to %#LogDate%

GetLogTime CONSISTENTLY returns hhmnss
and saves the value to %#LogTime%


For an easier to understand example, see
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogDate.htm) and
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogTime.htm)

*******

-tsg
____________________________________________________________
TheSystemGuard.com | BoomingOrFuming.com | MountCommands.com
Free and "Almost Free" Knowledge for Windows System Admins!
 
M

Marco Maier

Roger said:
This is happening because the builtin %date% and %time% variables perform
INCONSISTENTLY. They vary based on the operating system version and the
regional date/time settings.

There are three problems with using %date% and %time% for this purpose:

1. In your specific case, the Day Of Week is present in
%date%, so it is returning "Sat dd-mm-yyyy".
Type "ECHO ___%date%___" at a command prompt to see it.

2. %time% does not pad the hours with a zero when they are
less than 10. This will cause your file to be renamed
INCONSISTENTLY. For example:

MyTestFile1309200393014.dat {at 9:30am}
MyTestFile13092003103014.dat {at 10:30am}

Using the format "yyyymmddhhmnss", which represents

yyyy = four digit year
mm = two digit month (padded with zero for
months less than 10)
dd = two digit day
hh = two digit hour {padded with zero for
hours less than 10}
mn = two digit minute
ss = two digit seconds

will give you

MyTestFile20030913093014.dat {at 9:30am}
MyTestFile20030913103014.dat {at 10:30am}

which will always sort correctly for you.

3. The solution will only work in Windows 2000, XP and Server 2003.
The %date% and %time% variable are not available under Windows NT.

*******

The Mount/\Commands GetLogDate and GetLogTime overcome all three of these
problems for yourself (with your specific problem), and for anyone else who
would need to solve the same problem on their system at a later date.

GetLogDate CONSISTENTLY returns yyyymmdd
and saves the value to %#LogDate%

GetLogTime CONSISTENTLY returns hhmnss
and saves the value to %#LogTime%


For an easier to understand example, see
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogDate.htm) and
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogTime.htm)

*******

-tsg
____________________________________________________________
TheSystemGuard.com | BoomingOrFuming.com | MountCommands.com
Free and "Almost Free" Knowledge for Windows System Admins!

Who is Roger Grossenbacher?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top