Results 1 to 15 of 15

Thread: How to save file with date and time stamp?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    How to save file with date and time stamp?

    I know there are other examples, but using this code, is it possible to add the date and time to Output.txt filename, so I don't overwrite my file every time I save? Thx.

    This is just a single command button that grabs the context of a text box and exports..

    Code:
    Private Sub CmdExport_Click()
    Dim FF As Long
    Dim FilePath As String
    
    FF = FreeFile()
    FilePath = "C:\Reports\Output.txt"
    Open FilePath For Output As #FF
      Print #FF, txtSql.Text & Date & "  " & Time
    Close #FF
    
    End Sub

  2. #2
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to save file with date and time stamp?

    You open it for append

    Open FilePath For Append As #FF

    Edit: Do you want that Output.txt has Date-Time name instead of Output.txt?

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: How to save file with date and time stamp?


    Edit: Do you want that Output.txt has Date-Time name instead of Output.txt?
    Yep!

    Both actually.... each time I hit save, the Reports directory would show:
    Output12182008_345pm
    Output12182008_346pm
    etc...
    A nicer looking date time format would be better.. not sure how to make that look best either.. though I can find that somewhere. Thx!
    Last edited by jolene_keagy; Dec 18th, 2008 at 04:59 PM.

  4. #4
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to save file with date and time stamp?

    Well, date and time formats contain special characters you cannot use in filenames. You have to make a String variable containing Date and Time, and then replace those special characters with some other character, for example with - .

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: How to save file with date and time stamp?

    Quote Originally Posted by Dungeon Keeper
    Well, date and time formats contain special characters you cannot use in filenames. You have to make a String variable containing Date and Time, and then replace those special characters with some other character, for example with - .

    I tried that too.. created strDate as String and populated it, but still couldn't concantenate it to save file name.

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: How to save file with date and time stamp?

    I really don't even care if time date stamp is in the save file name. I just want it to create a new file every time I hit save.. and not overwrite the old one.

  7. #7
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to save file with date and time stamp?

    This code will replace the ":", "/" and empty spaces in string with "-" and "_" try the code

    Code:
    Dim MyFileName As String
    MyFileName = Date & "_" & Time
    For i = 1 To Len(MyFileName)
        If Mid$(MyFileName$, i, 1) = " " Then Mid$(MyFileName$, i, 1) = "_"
        If Mid$(MyFileName$, i, 1) = "/" Or Mid$(MyFileName$, i, 1) = ":" Then _
        Mid$(MyFileName$, i, 1) = "-"
    Next i
    You can now use MyFileName As the name for your output file, + you have to add an extension.

    Code:
    MyFileName = MyFileName & ".EXT"
    Edit, oh, i didnt see that you want both:

    Code:
    MyFileName = "Output" & MyFileName & ".EXT"
    Last edited by Dungeon Keeper; Dec 18th, 2008 at 05:08 PM.

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: How to save file with date and time stamp?

    I did it!!
    Naturally.. after you wrote the solution. :P

    It looks like I'm basically doing the same thing though.

    Code:
    Private Sub CmdExport_Click()
    Dim FF As Long
    Dim FilePath As String
    Dim strDate As String
    strDate = Format(Date, "MMMM") & Format(Date, "DDDD") & Format(Date, "YYYY") & Format(Time, "HH") & Format(Time, "NN") & Format(Time, "SS") & ".txt"
    
    FF = FreeFile()
    FilePath = "C:\Reports\Report" & strDate
    Open FilePath For Output As #FF
      Print #FF, txtSql.Text & Date & "  " & Time
    Close #FF
    
    End Sub
    Thx for your time, DK. I really appreciate it!

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: How to save file with date and time stamp?

    OK.. yours does look much neater. I like! Thx again!

  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to save file with date and time stamp?

    You don't have to run all your formats separately. This would work.
    Code:
    strDate = Format(Now, "MMMMDDDDYYYYHHNNSS")  & ".txt"

  11. #11
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to save file with date and time stamp?

    Quote Originally Posted by jolene_keagy
    I did it!!
    Naturally.. after you wrote the solution. :P

    It looks like I'm basically doing the same thing though.

    Code:
    Private Sub CmdExport_Click()
    Dim FF As Long
    Dim FilePath As String
    Dim strDate As String
    strDate = Format(Date, "MMMM") & Format(Date, "DDDD") & Format(Date, "YYYY") & Format(Time, "HH") & Format(Time, "NN") & Format(Time, "SS") & ".txt"
    
    FF = FreeFile()
    FilePath = "C:\Reports\Report" & strDate
    Open FilePath For Output As #FF
      Print #FF, txtSql.Text & Date & "  " & Time
    Close #FF
    
    End Sub
    Thx for your time, DK. I really appreciate it!
    You are welcome

  12. #12

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: How to save file with date and time stamp?

    Quote Originally Posted by MarkT
    You don't have to run all your formats separately. This would work.
    Code:
    strDate = Format(Now, "MMMMDDDDYYYYHHNNSS")  & ".txt"
    Your code created the following file name:
    ReportFalse

  13. #13

    Re: How to save file with date and time stamp?

    Just a suggestion and don't know if it's useful or not but if you change your date format around so that it's stored with

    Filename YYYY MM DD HH MM SS

    Then your users can sort by filename and they'll be in date order.

    Not a coding suggestion, but it's all I can offer right now for what it's worth ;-)

  14. #14
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to save file with date and time stamp?

    Quote Originally Posted by jolene_keagy
    Your code created the following file name:
    ReportFalse
    If you run this does it return a false?

    Msgbox Format(Now, "MMMMDDDDYYYYHHNNSS") & ".txt"

    The complete code should be
    Code:
    Private Sub CmdExport_Click()
    Dim FF As Long
    Dim FilePath As String
    Dim strDate As String
    
    strDate = Format(Now, "MMMMDDDDYYYYHHNNSS")  & ".txt"
    
    FF = FreeFile()
    FilePath = "C:\Reports\Report" & strDate
    Open FilePath For Output As #FF
      Print #FF, txtSql.Text & Date & "  " & Time
    Close #FF
    
    End Sub

  15. #15
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to save file with date and time stamp?

    Usually I use this format so the files will be sorted in correct order:
    Code:
    FilePath = "C:\Reports\Report " & Format(Now, "yyyymmdd_hhnnss") & ".txt"
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width