Link to home
Create AccountLog in
Avatar of bpl5000
bpl5000

asked on

outputting the time to a file from the command line

I have a batch file that does the following...

echo %date% - %time% - %computername% - %username%>> \\server\share\log.txt

This works, but the time is formatted for 24 hours and it shows to the 100th of a second.  I want the format to look like it does when you execute "time/t" at the command line (for example, 01:48 PM).

I tried putting it into an environment variable by doing "set sTime=time/t", but that doesn't work.  How can I output time in the format that I want?  I need the output to look like...

Tue 11/01/2011 - 01:48 PM - computername - username

Is this doable in a batch file?
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Or to get more control over the formatting look at my article here:
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

e.g.

@echo off
echo wscript.echo right(100+hour(time),2) ^& "-" ^& right(100+minute(time),2) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set hhmm=%%a
echo Now you can use this in your file: %hhmm%
And for a final way...

%time:~0,5%

will take the first 5 chars of the time.... though otherwise will display as your time settings so could be  1:15 or 13:15 or 01:15 etc.

So I'd got vbscript option above or time /t if you have to!

Steve
Avatar of bpl5000
bpl5000

ASKER

dragon-it, your first way would be perfect, but I get the error

%%T was unexpected at this time.
Sounds like you have a typo, Steve's code as shown looks good, can you paste up your exact code so we can take a look?

You are running this in a BAT file right, not from the command line?

~bp
@bpl5000:

As Bill was pointing out, if you are trying Steve's script as a DOS command line, meaning not running from within a batch file but rather a command line, you will get this error.

If you wish to try it in a command line (DOS window), you have to use only one %, not two, as following:

for /f "delims=" %T in (' time /t') do set sTime=%T

And by the way, %T or %%T are case sensitive.
Thanks guys for clarifying.

OT... but I saw some scripts the other day Rene that used %%? ... was trying to work out wth was going on... I presume it works the same way as the letters... did wonder what would be the next parameter then, I suppose the next ascii character?
Sorry Steve, my working memory has trouble processing new informations as my executive functions are challanged due to a extensive lack of sleep. :)

You know a lot more then I do in regards to programming, scripting, ect and more etc..., so I will assume that your question is more of a statement. Sorry, I just can't figure it out.

Most regards and lots of cheers,
Rene
Sorry rene, I was just waffling on OT ... I suppose after seeing you saying it was case sensitive %%t different to %%T.  This script I saw used %%? ... and it seems it does work that way:

C:\4. WIP\EE>for /f "delims=, tokens=1-5" %! in ("a,b,c,d") do @echo %! - %" - %# - %$
a - b - c - d

That could lead to some very criptic batch files :-)

No doubt this is the "proper" way to do it and been used for years by others ;)
@Steve: I never thought using ! as a variable by it's own! You rick!!
Oups, I ment "You rock!!"
Completely OT now sorry...

The help with for says to use a-z and A-Z but this opens up (and this is bit sad remembering ascii code order but they used to follow the Dragon keyboard layout pretty well and knowing that 42 was a * etc. was useful then;):

!"#$%&'()*+,-./
and perhaps more interestingly
0123456789
:;<=>?
@A-Z
[\]^_
`a-z
{|}~

Seems the only one you can't use is %%% ... and <|> and ^ need escaping with an ^, i.e. %%^< etc.

Trying to think where useful... maybe %%1 - %%9 for params 1-9 etc?
@echo off

REM Only ones that don't work are %
REM less than / more than / pipe / caret need to be escaped with ^

cls
echo.
echo ASCII 33 to 47 - !"#$%^&'()*+,-./
for /f "delims=, tokens=1-17" %%! in ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q") do @echo %%! - %%" - %%# - %%$ - %%% - %%& - %%' - %%( - 

%%) - %%* - %%+ - %%, - %%- - %%. - %%/

echo.
echo Numbers etc. ASCII 48 to 64 - 0123456789:;^<=^>?@
for /f "delims=, tokens=1-17" %%0 in ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q") do @echo %%0 - %%1 - %%2 - %%3 - %%4 - %%5 - %%6 - %%7 - 

%%8 - %%9 - %%: - %%; - %%^< - %%= - %%^> - %%? - %%@

echo.
echo ASCII 91 to 96 - [\]^^
for /f "delims=, tokens=1-6" %%[ in ("a,b,c,d,e,f") do @echo %%[ - %%\ - %%] - %%^^ - %%_ - %%`

echo.
echo ASCII 123 to 126 ^
{^|}~
for /f "delims=, tokens=1-4" %%{ in ("a,b,c,d") do @echo %%{ - %%^| - %%} - %%~

Open in new window

ASCII 33 to 47 - !"#$^&'()*+,-./
a - b - c - d - % - k - l - m - n - o

Numbers etc. ASCII 48 to 64 - 0123456789:;<=>?@
a - b - c - d - e - f - g - h - i - j - k - l - m - n - o - p - q

ASCII 91 to 96 - [\]^
a - b - c - d - e - f

ASCII 123 to 126 {|}~
a - b - c - d

Open in new window

Impressed, I am... again!!
By the way, what does OT mean? On Tea?   :)
Off topic.
Avatar of bpl5000

ASKER

Interesting!  Anyway, I was testing at the command line (D'oh!).  I put it into a batch file and it works great.  Thanks for the help!