PC Review


Reply
Thread Tools Rate Thread

Batch File Timestamp

 
 
Roger Grossenbacher
Guest
Posts: n/a
 
      13th Sep 2004
"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!


 
Reply With Quote
 
 
 
 
Marco Maier
Guest
Posts: n/a
 
      13th Sep 2004
Roger Grossenbacher wrote:
> "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!
>
>


Who is Roger Grossenbacher?
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch File Timestamp Roger Grossenbacher Microsoft Windows 2000 New Users 1 13th Sep 2004 10:02 PM
Re: Batch File Timestamp guard Microsoft Windows 2000 2 13th Sep 2003 05:10 PM
Re: Batch File Timestamp guard Microsoft Windows 2000 New Users 2 13th Sep 2003 05:10 PM
Re: Batch File Timestamp guard Microsoft Windows 2000 0 13th Sep 2003 02:00 PM
Re: Batch File Timestamp guard Microsoft Windows 2000 New Users 0 13th Sep 2003 02:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:20 PM.