Results 1 to 6 of 6

Thread: [RESOLVED] Saving file with date and time on the name

  1. #1

    Thread Starter
    New Member etamayo's Avatar
    Join Date
    Jul 2006
    Posts
    3

    Resolved [RESOLVED] Saving file with date and time on the name

    Hello!

    I´ve been trying to save a file with date and time on the name, to be more specific:

    I need to take a ScreenShot and then save it to an especific route with the current date and time, this is what I tried:

    Private Sub Command2_Click()
    Dim Name As String
    Name = DateTime.Now
    SavePicture Picture1.Picture, "c:\mypic " + Name + ".bmp"
    'keybd_event 44, 1, 0&, 0&
    End Sub


    Any ideas?

    Thanks in advance.

    Etamayo

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Saving file with date and time on the name

    first dont use Name (use something like fName)
    second dont use + use & when combining strings

    VB Code:
    1. Private Sub Command2_Click()
    2.     Dim fName As String
    3.     fName = Format(Now, "mm_dd_yyyy_hh_mm_ss_am/pm")
    4.     SavePicture Picture1.Picture, "c:\mypic " & fName & ".bmp"
    5. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Saving file with date and time on the name

    you cannot save a picture in that format because DateTime will result in date seperated by '/' and time by ':' which are not supported by the Win OS as part of file name.

    try this:
    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim name As String
    4.     name = DateTime.Now
    5.     name = Replace(name, "/", "-")
    6.     name = Replace(name, ":", "_")
    7.     SavePicture Picture1.Picture, "C:\Documents and Settings\Administrator\Desktop\" + name + ".bmp"
    8.  
    9. End Sub
    Show Appreciation. Rate Posts.

  4. #4

    Thread Starter
    New Member etamayo's Avatar
    Join Date
    Jul 2006
    Posts
    3

    Re: Saving file with date and time on the name

    Thanks a lot!! It worked perfect!

    I´ll work on my coding

    Etamayo

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Saving file with date and time on the name

    Quote Originally Posted by Harsh Gupta
    you cannot save a picture in that format because DateTime will result in date seperated by '/' and time by ':' which are not supported by the Win OS as part of file name.
    Static code is not returning any "/" character. fName = Format(Now, "mm_dd_yyyy_hh_mm_ss_am/pm") will return something like "07_11_2006_02_30_06_am". so he is correct. Anyway Similar way
    VB Code:
    1. Private Sub Command2_Click()
    2.     SavePicture Picture1.Picture, "c:\mypic " & Format(Now, "mmddyyyyHHmmss") & ".bmp"
    3. End Sub
    CS

  6. #6
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Saving file with date and time on the name

    Quote Originally Posted by cssriraman
    Static code is not returning any "/" character. fName = Format(Now, "mm_dd_yyyy_hh_mm_ss_am/pm") will return something like "07_11_2006_02_30_06_am". so he is correct. Anyway Similar way
    VB Code:
    1. Private Sub Command2_Click()
    2.     SavePicture Picture1.Picture, "c:\mypic " & Format(Now, "mmddyyyyHHmmss") & ".bmp"
    3. End Sub
    i never said that Static's method wont work. He is using / in the format function to select either AM or PM.

    I posted according to the OP's code, that DateTime will return answer with / and : characters which is not acceptable by the OS.

    BTW, Static's post was not there when i posted
    Show Appreciation. Rate Posts.

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