Solved: batch file times

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

andythepandy

Thread Starter
Joined
Jul 2, 2006
Messages
379
Hi all,

I have a batch file that is being run repeatedly. (As in at the end of the file, it the runs itself again)

I have the following to generate a file friendly date (it removes the colons):
Code:
for /f "tokens=1-3 delims=:. " %%a in ("%time%") do set time=%%a-%%b-%%c
The first time this file runs it produces a date like this : 00-00-00 (as expected)
The next time it runs it produces this : 00-00-00--
Then 00-00-00---- and so on adding two dashes each time.

Also the time is identical every time the file runs.

How can I get it to
a)generate a time in the format 00-00-00 every time, and
b)generate the real time every time

Any suggestions?

Thanks,
Andy
 
Joined
Aug 7, 2007
Messages
9,028
You are using time as a variable, so after the first run of the For loop, time refers to your variable, not the special variable that shows the time.
Example:
%time% = 6:04:51.62
%%a=6
%%b=04
%%c=51
The loop sets time to 6-04-51
Next time the loop runs, %time% is 6-04-51, not the current time.
%%a=6-04-51
%%b=
%%c=
There will be no %%b or %%c as there is no space, :, or . in 6-04-51, so time gets set to %%a--, or 6-04-51--

Change the loop to this:
Code:
for /f "tokens=1-3 delims=:. " %%a in ("%time%") do set [COLOR=Red][B]_[/B][/COLOR]time=%%a-%%b-%%c
And use %_time% as your variable to create the file name
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top