1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: batch file times

Discussion in 'Software Development' started by andythepandy, Apr 25, 2010.

Thread Status:
Not open for further replies.
  1. andythepandy

    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
     
  2. TheOutcaste

    TheOutcaste

    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
     
  3. andythepandy

    andythepandy Thread Starter

    Joined:
    Jul 2, 2006
    Messages:
    379
    Ah brilliant, that did the trick.

    Thanks very much :)
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/919135

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice