Batch File Timestamp


S

Stephen

I'm trying to create a batch file that will rename another
file and include a timestamp in the file name. For
example, incoming file is named MyTestFile.dat and I want
to rename it to MyTestFile-180623.dat

Any advice would be appreciated I'm sure it's quite a
simple line or two of code. Thanks.
 
Ad

Advertisements

P

Phil Robyn

Stephen said:
I'm trying to create a batch file that will rename another
file and include a timestamp in the file name. For
example, incoming file is named MyTestFile.dat and I want
to rename it to MyTestFile-180623.dat

Any advice would be appreciated I'm sure it's quite a
simple line or two of code. Thanks.
C:\cmd>\temp\xtime
ren Testfile.dat Testfile-104020.dat

C:\cmd>rlist \temp\xtime.cmd
=====begin C:\temp\xtime.cmd ====================
1. @echo off
2. for /f "tokens=1-3 delims=:." %%a in (
3. "%time%") do echo ren Testfile.dat Testfile-%%a%%b%%c.dat
=====end C:\temp\xtime.cmd ====================

Remove the word 'echo' from line 3 if this does what you intend.
 
B

Bill Fletcher

Just add the line
ren MyTestFile.dat MyTestFile-%time%.dat

or, if you also want the date

ren MyTestFile.dat MyTestFile-%date%_at_%time%.dat


Bill
 
G

guard

I'm trying to create a batch file that will rename another
file and include a timestamp in the file name. For
example, incoming file is named MyTestFile.dat and I want
to rename it to MyTestFile-180623.dat

Any advice would be appreciated I'm sure it's quite a
simple line or two of code. Thanks.
Under Windows NT/2K/XP/K3, this can be done in two self-documenting lines of
code using the Mount/\Command "GetLogTime".

For example:

%.GetLogTime%
REN MyTestFile.dat MyTestFile-%#LogTime%.dat

GetLogTime returns hhmnss CONSISTENTLY under NT/2K/XP/K3 and saves this
value in variable %#LogTime%. Output is always the same regardless of
variations in operating systems and regional date/time formats.

See (http://TheSystemGuard.com/MtCmds/GetValue/GetLogTime.htm#GuardNotes)
for more information and a color-keyed example.

*******

GetLogTime is part of the .GetValue Series of Mount/\Commands.

Some other Get[Value] commands are:

GetLogDate (yyyymmdd)
GetIPAddr (nnn.nnn.nnn.nnn)
GetMac (xx-xx-xx-xx-xx-xx)
GetFree (free space on current drive in bytes)
GetRole (Server/Wkstn)

Each one displays to the console AND saves the value to a variable for
further processing. See them all at
(http://TheSystemGuard.com/MtCmds/GetValue)

*******

The complete .Mount/\Command Set contains over 200 commands. These
are extensions to the builtin command set that perform CONSISTENTLY across
NT/2K/XP/K3 and provide self-documenting code for your scripts.

See (http://MountCommands.com) for more information.

*******

Notes:

1. .Mount/\Commands are constructed using ONLY builtin
commands common to all four platforms (NT/2K/XP/K3).
There is NO BINARY CODE, only scripting commands!
2. .M/\C's are NOT case sensitive. Mixed case is used
for Visual Clarity only.
3. The (FREE) Advanced NT/2K/XP Command Library (ntlib.cmd)
provides over 50 sample Mount/\Commands to assist with
writing and documenting cross-platform scripts.
You can obtain it at (http://ntlib.com).

*******

All the commands internal to cmd.exe are documented in "Mounted Help" pages
in TheGuardBook (http://TheSystemGuard.com/TheGuardBook/CCS-Int).

This includes a color-keyed page highlighting the differences between all
internal commands in NT/2K/XP/K3.

The "Common Help" from each OS's help screen is also available for
comparison.

*******

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

Juergen Schwarze

Someone without a real name wrote:

[lots of advertising crap]

Stephen started this thread in microsoft.public.win2000.cmdprompt.admin.
There is absolutly no need to post your advertising messages to 3 other
newsgroups, where nobody can see Stephens first posting.

It seems to me, that you are not interested in helping other people.
You just want to sell your 'products' over the usenet.

Once again: Please stop this.


Juergen


P.S.: You don't use a real name for your postings and even on your web-
site there is no personal name. Do you think people will trust such a
person?
 
S

Stephen

-----Original Message-----


C:\cmd>\temp\xtime
ren Testfile.dat Testfile-104020.dat

C:\cmd>rlist \temp\xtime.cmd
=====begin C:\temp\xtime.cmd ====================
1. @echo off
2. for /f "tokens=1-3 delims=:." %%a in (
3. "%time%") do echo ren Testfile.dat Testfile-%%a%%b%% c.dat
=====end C:\temp\xtime.cmd ====================

Remove the word 'echo' from line 3 if this does what you intend.

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

.
Phil,
 
Ad

Advertisements

S

Stephen

-----Original Message-----


C:\cmd>\temp\xtime
ren Testfile.dat Testfile-104020.dat

C:\cmd>rlist \temp\xtime.cmd
=====begin C:\temp\xtime.cmd ====================
1. @echo off
2. for /f "tokens=1-3 delims=:." %%a in (
3. "%time%") do echo ren Testfile.dat Testfile-%%a%%b%% c.dat
=====end C:\temp\xtime.cmd ====================

Remove the word 'echo' from line 3 if this does what you intend.

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

.
Phil,
 
S

Stephen

-----Original Message-----


C:\cmd>\temp\xtime
ren Testfile.dat Testfile-104020.dat

C:\cmd>rlist \temp\xtime.cmd
=====begin C:\temp\xtime.cmd ====================
1. @echo off
2. for /f "tokens=1-3 delims=:." %%a in (
3. "%time%") do echo ren Testfile.dat Testfile-%%a%%b%% c.dat
=====end C:\temp\xtime.cmd ====================

Remove the word 'echo' from line 3 if this does what you intend.

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

.
Phil,
 
S

Stephen

-----Original Message-----


C:\cmd>\temp\xtime
ren Testfile.dat Testfile-104020.dat

C:\cmd>rlist \temp\xtime.cmd
=====begin C:\temp\xtime.cmd ====================
1. @echo off
2. for /f "tokens=1-3 delims=:." %%a in (
3. "%time%") do echo ren Testfile.dat Testfile-%%a%%b%% c.dat
=====end C:\temp\xtime.cmd ====================

Remove the word 'echo' from line 3 if this does what you intend.

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

.
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.
 
G

guard

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!
 
A

Al Dunbar

Juergen Schwarze said:
Someone without a real name wrote:

[lots of advertising crap]

Stephen started this thread in microsoft.public.win2000.cmdprompt.admin.
There is absolutly no need to post your advertising messages to 3 other
newsgroups, where nobody can see Stephens first posting.

It seems to me, that you are not interested in helping other people.
You just want to sell your 'products' over the usenet.

Once again: Please stop this.


Juergen


P.S.: You don't use a real name for your postings and even on your web-
site there is no personal name. Do you think people will trust such a
person?
You are saying this to someone who continues to hide behind the phony name,
and continues to fail to follow common usenet etiquette. Yet there actually
*are* people associated with that paragon of entrepeneurship, "The System
Guard", one such being:

John D. Zanette
Vice President
JWC Computer Communications
(e-mail address removed)

I hesitated to post the above address, but then realized that it can only be
to their benefit for a real person to receive the complaints that otherwise
are falling on deaf ears, as their behavious here cannot possibly be having
a positive impact on their business.

/Al
 
Ad

Advertisements

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?
 
Ad

Advertisements

Joined
Sep 2, 2011
Messages
1
Reaction score
0
"Stephen" <(E-Mail Removed)> wrote in message
news:08d401c379d1$b8864980$(E-Mail Removed)...
>
> >-----Original Message-----
> >Stephen wrote:
> >
> >> I'm trying to create a batch file that will rename
> another
> >> file and include a timestamp in the file name. For
> >> example, incoming file is named MyTestFile.dat and I
> want
> >> to rename it to MyTestFile-180623.dat
> >>
> >> Any advice would be appreciated I'm sure it's quite a
> >> simple line or two of code. Thanks.
> >
> >C:\cmd>\temp\xtime
> >ren Testfile.dat Testfile-104020.dat
> >
> >C:\cmd>rlist \temp\xtime.cmd
> >=====begin C:\temp\xtime.cmd ====================
> >1. @echo off
> >2. for /f "tokens=1-3 delims=:." %%a in (
> >3. "%time%") do echo ren Testfile.dat Testfile-%%a%%b%%
> c.dat
> >=====end C:\temp\xtime.cmd ====================
> >
> >Remove the word 'echo' from line 3 if this does what you
> intend.
> >
> >--
> >Phil Robyn
> >Univ. of California, Berkeley
> >
> >u n z i p m y a d d r e s s t o s e n d e - m a
> i l
> >
> >.
> >
>
> 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/Get...GetLogDate.htm) and
(http://TheSystemGuard.com/MtCmds/Get...GetLogTime.htm)

*******

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

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

Similar Threads

Timestamp as Name of File 0
batch file 5
batch file 3
batch file 4
Batch files 1
Batch file 3
batch file problem 8
logging batch files 8

Top