• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

Append date and time to file name in vb.net

Hi
How can I append the date and time to a filename in vb.net.
i.e
error_20_04_2011_1705.log
0
sykotex
Asked:
sykotex
1 Solution
 
jimswebCommented:
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
 
Paul JacksonSoftware EngineerCommented:
Are you creating this file yourself? Got some code you can post as a starting point?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
omegaomegaDeveloperCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Great advice Randy!  =)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now