?
Solved

Append date and time to file name in vb.net

Posted on 2011-04-20
5
Medium Priority
?
542 Views
Last Modified: 2012-05-11
Hi
How can I append the date and time to a filename in vb.net.
i.e
error_20_04_2011_1705.log
0
Comment
Question by:sykotex
5 Comments
 
LVL 8

Expert Comment

by:jimsweb
ID: 35434294
I think it is not difficult.

Dim myfile1
dim myfile2
myfile1 = "C:\myfile_s.txt"
myfile2 = "C:\Newfile_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".txt" 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(myfile1) = true then
    If fso.FileExists(myfile2) = true then
        fso.DeleteFile(myfile2)
    End if
    fso.MoveFile myfile1, myfile2
End If 
Set FSO = Nothing

Open in new window


using the above code we can easily write the file.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35434320
Are you creating this file yourself? Got some code you can post as a starting point?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 35434820
Maybe something like?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Log As String = MakeFileName("error", DateTime.Now, ".log")
    End Sub

    Private Function MakeFileName(ByVal base As String, ByVal dt As DateTime, ByVal extension As String) As String
        Return base & dt.ToString("_dd_MM_yyyy_HHmm_") & extension
    End Function

Open in new window

0
 
LVL 12

Expert Comment

by:omegaomega
ID: 35442840
Hello, sykotex,

But please please use "_yyyy_MM-dd_HHmm" so that:

  a) these files will be sorted in a reasonable order

  b) a name like "error_02_04_2011_1705.log" won't be misinterpreted by:
        - an American that doesn't know you are European.
        - a European that doesn't know you are American.
        - the rest of us, who are constantly confused by your date convention.

Cheers,
Randy


    -
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35442872
Great advice Randy!  =)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question