Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi.
In the W2k version of command the variable %date% gives you the current date eg "fri 26/07/02"
We have a set of programs that run off a bat file and we would also like to record daily logs, so naturally, the most sensible name for each log would be its date. The problem with...
Echo blah blah blah >> %date%.txt
(">>" because the "schedule" is run several times a day)
...is the fact that the date variable has "/" in, which is invalid for a file name.
Is there anyway to gain the date in a different format, like 26-07-02 or maybe 260702 ?
cheers
-Chris

DOS is a very poor text parser, mostly a file and environment manipulator. You can try playing with the FOR command or something, but you would be much better off with a scripting language. Almost anything would do: Perl, VBScript, JScript, ASP.Net...
As I recall, VBScript and JScript are native to Win2K, so that's probably your easiest option...
rem In batch file, add something like thisECHO Blah, blah, blah
writelog.vbs "Blah, blah, blah"
'contents of writelog.vbs ...Option Explicit
Dim oArgs, fso
Dim NewLog, FileName, ForAppendingSet oArgs = WScript.Arguments
ForAppending = 8 'just a constant
FileName = Date
FileName = Replace(FileName, "/", "-")
Set fso = CreateObject("Scripting.FileSystemObject")
'open file if it exists, pass True
' to create if doesn't already
Set NewLog = fso.OpenTextFile(FileName & ".txt", ForAppending, True)NewLog.WriteLine oArgs(0)
NewLog.Close
Some lines might have wrapped in this post, so you may need to straighten them. This code will append whatever string is passed as a command to writelog.vbs, to a file named in a 26-07-02.txt format, or create that file if it doesn't already exist. If you prefer to omit the delimiters, you can callFileName = Replace(FileName, "/", "")
which will produce the string 260702.
You can goto "http://www.microsoft.com/isapi/gomsdn.asp?TARGET=/scripting/" for more details.
Cheers

The identifier "NewLog" was poorly named, as it is only new if not already existing. Perhaps "LogFile"???

![]() |
bitwise OR in C++
|
Access vs Visual Basic?
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |