Append date and time to file name in

Posted on 2011-04-20
Last Modified: 2012-05-11
How can I append the date and time to a filename in
Question by:sykotex
    LVL 8

    Expert Comment

    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
        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.
    LVL 29

    Expert Comment

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

    Accepted Solution

    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

    LVL 12

    Expert Comment

    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.


    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Great advice Randy!  =)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now