Log Date and time to a DOS Log File

March 23, 2011 at 03:22:55
Specs: Windows XP
REM The following is used to create variables with the date and time
for /F "tokens=1-4 delims=/- " %%A in ('echo %date%') do set MYDate=%%B%%C%%D
for /F "tokens=1,2* delims=: " %%I in ('echo %time%') do set MYTime=%%I%%J%%K

echo Date : %MYDate%
echo Seconds: %MYTime%

set MYExtention=%MYDate%.%MYTime%

echo Both : %MYExtention%


See More: Log Date and time to a DOS Log File

Report •

#1
March 23, 2011 at 03:23:57
Then @Echo the reults to a file i.e : echo Date and Time: %MYExtention% >> MyLogFile.txt

Report •

#2
March 23, 2011 at 05:22:01
for /F "tokens=1-4 delims=/- " %%A in ('echo %date%') do set MYDate=%%B%%C%%D
I love how you're echoing the variable, instead of doing something sensible, like using "%date%". Also, your script breaks on my system:

Wed 03.23.2011  8.13.34
H:\>for /F "tokens=1,2* delims=: " %I in ('echo %time%') do set MYTime=%I%J%K

Wed 03.23.2011  8.20.40
H:\>set MYTime=8.20.40.77

How To Ask Questions The Smart Way


Report •

#3
March 23, 2011 at 09:17:40
I hate it when people use for loops to parse when Command Line has parsing built in...

@echo off

Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%
Set TodayDate=%Year%%Day%%Month%

Set Hour=%Time:~-11,2%
Call :PadHour %Hour%
Set Minute=%Time:~-8,2%
Set Second=%Time:~-5,2%
Set TodayTime=%Hour%%Minute%%Second%

Echo %Hour% %Minute% %Second%
Echo %Year% %Day% %Month%

goto :EOF

:PadHour
Set Hour=%*
If %Hour% GEQ 10 then Goto :EOF
Set Hour=0%Hour%
Goto :EOF


http://www.intelliadmin.com/index.p...
http://www.robvanderwoude.com/batch...


Report •

Related Solutions

#4
March 23, 2011 at 09:46:35
O hay, I breaked ur script to! (Or: I just finished setting up my non-standard regional settings. Also, you have a syntax bug.)
H:\>copy con a.cmd
@echo off

Set Day=%Date:~-7,2%

 . . . . . . . . . . . . . . . . .

If %Hour% GEQ 10 then Goto :EOF
Set Hour=0%Hour%
Goto :EOF^Z
        1 file(s) copied.

Wed 2011.03.23 12.41.11
H:\>a
'then' is not recognized as an internal or external command,
operable program or batch file.
012 41 14
3.23 1. 20

How To Ask Questions The Smart Way


Report •

#5
March 23, 2011 at 09:53:35
copy con a.bat

but who uses copy con any more use edit. Unless you are using a version old DOS that is older than 5.0 but even then I use to use EDLIN over COPY CON. I don't think edlin is available in NT though but I know edit is.

P.S. All this does...

:PadHour
Set Hour=%*
If %Hour% GEQ 10 then Goto :EOF
Set Hour=0%Hour%
Goto :EOF

Is put a "0" in front of numbers less than 10 because (depending on regional settings) %time% does not and if you are writing a backup program to time stamp files you will want the 0 padding because it makes it easier to reverse pars the file name.


Report •

#6
March 23, 2011 at 10:29:49
but who uses copy con any more use edit.
Sadly, I use the x64 builds, so no 16-bit tools for me.
COPY CON -> Right-Click -> Paste -> F6 -> Enter is faster than Notepad if you already have the Command Prompt up.

For the record, I generally use Notepad++. Syntax highlighting makes things so pretty.

P.S. All this does...
...Is complain about the "then" and add a 0? :P To be fair, I make the same mistake all the time, and I didn't notice it either until I ran your script.

Really, there's no nice way to separate the date in CMD. VBS can do it, JScript can do it, Powershell can do it, Bash can do it, but not CMD. Oh well, maybe in Windows 8?

EDIT: My preferred method of padding:

SET hour=0%*
SET hour=%hour:~-2%

How To Ask Questions The Smart Way


Report •

#7
June 6, 2011 at 09:39:15
Thank you, Razor ! You are the man, now I have accurate logs.

Report •

Ask Question