Computer Hope

Microsoft => Microsoft DOS => Topic started by: eagle on March 14, 2009, 08:32:04 PM

Title: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: eagle on March 14, 2009, 08:32:04 PM
I am backing up some of my files multiple times in a day.  As a point of reference (version control & testing), i am trying to insert BOTH the date AND the time into the file name so I may document what is working & what is not.

The File that I am using is called catalog.mdb.

I am using a batch command to copy the file to the designated folder, but then I am manually changing the file name to include the date & time.

I would like to add to this batch command to insert both the date & time {of the back up taking place} into the file name.
(i.e.  catalog 2009.03.13 2126.mdb)

The format of the time is not a great necessity, but military time is what I am used to.  LOL

Please note that I have been looking through the various topics here inthe forum, but have not found anything that is doing BOTH Date & Time.

I have gotten the date inserted; and I got the time inserted, but having trouble getting BOTH of them inserted.  The cobwebs from 20+ years of not doing this type of thing has proven to be quite dense.

Thank you in advance for your participation in this topic.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Reno on March 14, 2009, 11:45:55 PM
C:\batch>echo %date%
15/03/2009

C:\batch>echo %time%
12:43:41,01
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Dias de verano on March 15, 2009, 01:53:55 AM
I often use the Visual Basic Script date & time functions because you can be independent of the system default date & time format, as seen in %date% and %time%, which are decided by the Regional Settings in force.

Code: [Select]
see below
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: ghostdog74 on March 15, 2009, 01:59:43 AM
I would think its better in terms of efficiency, to have the dates/time evaluated inside the evaluate.vbs script and then return a string containing the date and time for the "for" loop to parse. That way, there's no need to call cscript 5 times.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Dias de verano on March 15, 2009, 02:16:05 AM
I would think its better in terms of efficiency, to have the dates/time evaluated inside the evaluate.vbs script and then return a string containing the date and time for the "for" loop to parse. That way, there's no need to call cscript 5 times.

I agree. No sooner said than done...

Code: [Select]
@echo off
echo dd=day(date)>TimeStamp.vbs
echo mm=month(date)>>TimeStamp.vbs
echo yyyy=year(date)>>TimeStamp.vbs
echo hh=hour(time)>>TimeStamp.vbs
echo mn=minute(time)>>TimeStamp.vbs
echo wscript.echo dd^&" "^&mm^&" "^&yyyy^&" "^&hh^&" "^&mn>>TimeStamp.vbs
for /f "tokens=1-5 delims= " %%A in ( ' cscript //nologo TimeStamp.vbs ' ) do (
set dd=%%A
set mm=%%B
set yyyy=%%C
set hh=%%D
set mn=%%E
)
del TimeStamp.vbs
if %dd% LEQ 9 set dd=0%dd%
if %mm% LEQ 9 set mm=0%mm%
if %hh% LEQ 9 set hh=0%hh%
if %mn% LEQ 9 set mn=0%mn%
REM Format requested: catalog 2009.03.13 2126.mdb
rename "catalog.mdb" "catalog %yyyy%.%mm%.%dd% %hh%%mn%.mdb"


Alternatively, keep this script in the folder

TimeStamp.vbs

Code: [Select]
dd=day(date)
mm=month(date)
yyyy=year(date)
hh=hour(time)
mn=minute(time)
wscript.echo dd&" "&mm&" "&yyyy&" "&hh&" "&mn

and you don't need to create it each time you run the batch

Code: [Select]
@echo off
for /f "tokens=1-5 delims= " %%A in ( ' cscript //nologo TimeStamp.vbs ' ) do (
set dd=%%A
set mm=%%B
set yyyy=%%C
set hh=%%D
set mn=%%E
)
if %dd% LEQ 9 set dd=0%dd%
if %mm% LEQ 9 set mm=0%mm%
if %hh% LEQ 9 set hh=0%hh%
if %mn% LEQ 9 set mn=0%mn%
REM Format requested: catalog 2009.03.13 2126.mdb
rename "catalog.mdb" "catalog %yyyy%.%mm%.%dd% %hh%%mn%.mdb"

Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Reno on March 15, 2009, 03:48:02 AM
batch script can also independent of the system default date & time format (99%)

Code: [Select]
@echo off
for /f "skip=1 tokens=2-4 delims=(./-)" %%a in ('echo.^|date') do (
for /f "tokens=1-5 delims=./-:, " %%A in ("%date:~-10% %time%") do (
set %%a=%%A
set %%b=%%B
set %%c=%%C
set   h=%%D
set   m=%%E
))
echo rename "catalog.mdb" "catalog %yy%.%mm%.%dd% %h%%m%.mdb"

if he doesnt need the date/time to be independent of regional setting, the batch code can be shortened.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Dias de verano on March 15, 2009, 03:59:59 AM
Code: [Select]
set %%a=%%A
set %%b=%%B
set %%c=%%C

This is a novelty to me. Could you talk me through what is going on here?
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Reno on March 15, 2009, 04:08:37 AM
C:\batch>date
The current date is: 15/03/2009
Enter the new date: (dd-mm-yy)

the dd-mm-yy will always be same for every english-windows-pc. (this is the first loop)

on the second loop do match-making:
C:\batch>echo %date:~-10%                (get the last 10 characters, stripping off day if exist)
15/03/2009
set %%a=%%A          -   expanded to set dd=15
set %%b=%%B          -   expanded to set mm=03
set %%c=%%C          -   expanded to set yy=2009

i am saying its going to work 99% because i am not sure if other language windows pc also returned dd, mm, yy.


YIHAAAA, i just break 50 posts and move up in rank to "Beginner" =)
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: ghostdog74 on March 15, 2009, 04:21:28 AM
why wait for code to break , when you can avoid it in the first place. 100% independent is still superior than 99%.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Reno on March 15, 2009, 05:17:14 AM
whatever you say ghostdog. i dont have that habit always arguing with people.

each language has its advantages and drawback.
some people prefer BMW, some people prefer Mercedez and some others prefer Ferrari.
Nothing is perfect.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Dias de verano on March 15, 2009, 05:22:51 AM
I love arguing with people! I can see the merits of both points of view. I have learned, though, through bitter experience, that the dull looking code that works 99.9999% of the time is preferable to the clever code that might work 99% of the time.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: ghostdog74 on March 15, 2009, 05:37:11 AM
whatever you say ghostdog. i dont have that habit always arguing with people.
you should treat that as educated discussions. :)
And what i mentioned is definitely true in the real world. whether you are doing system administration, writing web sites, programming desktop apps etc. Writing code/program that are resilient to system failures will put one at an advantage.

Quote
each language has its advantages and drawback.
some people prefer BMW, some people prefer Mercedez and some others prefer Ferrari.
Nothing is perfect.
yes, this saying is true. Nothing is perfect. However, is one going to leave it at that? Or is one going to strive to be perfect ? ;)
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: eagle on March 15, 2009, 12:15:26 PM
The Jist of all this is to use the most efficient method possible.  If that means using the system date & time; then so be it ...

My intentions on this, once figured out, a batch file to back up the file; then CALL on this batch file to do the renaming functions.

According to Reference number: CH000987 ... I can get the Date OR the Time into the file name.  But I was hoping there was an easy way to COMBINE both into one.

I did do some experimenting, and did not get it to do both items.

All of your input is valued immensely.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: Dias de verano on March 15, 2009, 12:46:29 PM

You have already been given some code that works.
Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: eagle on March 15, 2009, 03:49:39 PM
I Agree ...

The first initiative surprised me that it wasn't more direct than it really is ... LOL

Thank You to All who assisted with this.

I have a LOT of admiration for those who have the knowledge to answer these questions on this forum.  I am a bit jealous as well, because nothing would please me more than to be a major contributor to what takes place on this forum.

Thank You VERY, VERY Much
 :D ;D 8)

Title: Re: Renaming a File to INCLUDE BOTH Date & Time Stamp
Post by: JenniC on May 01, 2009, 01:34:37 PM

Quote
Several times a day, you want to backup a file C:\folder1\catalog.mdb to C:\folder2\catalog <timestamp>.mdb.

The following script in biterscripting ( http://www.biterscripting.com/install.html - it is free) will do the job.

Code: [Select]
system copy "C:\folder1\catalog.mdb" ("\"C:\folder2\catalog "+gettime()+".mdb\"")
Come to think of it, there is only one line in this script.

J