Computing.Net > Forums > Programming > DOS date+filename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

DOS date+filename

Reply to Message Icon

Name: Chris-M
Date: July 26, 2002 at 07:29:53 Pacific
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff J
Date: July 27, 2002 at 08:43:02 Pacific
Reply:

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 this

ECHO Blah, blah, blah
writelog.vbs "Blah, blah, blah"


'contents of writelog.vbs ...

Option Explicit
Dim oArgs, fso
Dim NewLog, FileName, ForAppending

Set 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 call

FileName = Replace(FileName, "/", "")

which will produce the string 260702.

You can goto "http://www.microsoft.com/isapi/gomsdn.asp?TARGET=/scripting/" for more details.

Cheers


0

Response Number 2
Name: Jeff J
Date: July 27, 2002 at 08:46:31 Pacific
Reply:

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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


bitwise OR in C++ Access vs Visual Basic?



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: DOS date+filename

Filename search DOS script www.computing.net/answers/programming/filename-search-dos-script/11872.html

Batch File deleting files by date? www.computing.net/answers/programming/batch-file-deleting-files-by-date/13717.html

Batch File deleting files by date? www.computing.net/answers/programming/batch-file-deleting-files-by-date/15581.html