Write a new file each time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmdolcet69
    New Member
    • Sep 2007
    • 25

    #1

    Write a new file each time

    I don't know exactly how to go about doing this, however how can I write a unique file based off the date/time of the day for each readings I'm sending?

    Example: FileName1002200 7134700.txt
    where the filename is the filename
    10 02 2007 is the data
    13 47 00 is the hour (24 hour clock) minutes and seconds.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    It's actually pretty simple. Just squish the parts together into a string. For example...

    [CODE=vb]Public Function FreshFileName(B yVal Base As String) As String
    ' Return a file name including the current date and time.
    FreshFileName = Base & Format(Now,"MMD DYYYYHHNNSS")
    End Function[/CODE]

    Comment

    • cmdolcet69
      New Member
      • Sep 2007
      • 25

      #3
      Originally posted by Killer42
      It's actually pretty simple. Just squish the parts together into a string. For example...

      [CODE=vb]Public Function FreshFileName(B yVal Base As String) As String
      ' Return a file name including the current date and time.
      FreshFileName = Base & Format(Now,"MMD DYYYYHHNNSS")
      End Function[/CODE]
      Killer do you know off hand if that will create a new file each and every time i write a file. In theory i should create a new filename because it every second is unique.The only drawback would be if they write files faster then a second which is completly un realistic in this situation.

      Thanks

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by cmdolcet69
        Killer do you know off hand if that will create a new file each and every time i write a file...
        Of course, the function I wrote will produce identical filenames if given the same "base" twice within one second. Given the speed of a typical PC these days, this is certainly possible. Whether it's likely will depend on your individual circumstances. I don't think Format() function will return tenths of a second, but if you want the time to that level of precision I'm sure you can get it somewhere. I seem to recall reading about the Windows system clock "ticking" 18,000 times per second, or something.

        You could also do things to ensure (or at least increase the probability of) a unique name, such as tacking on a random character, or checking for an existing file and modifying your new name if you find one. The code provided was just a quick proof-of-concept type of thing. Feel free to build on it.

        (Oh! I forgot. The Timer function does return fractions of a second. So you can use that to pin down the time to less than a second, if necessary.)

        Comment

        • cmdolcet69
          New Member
          • Sep 2007
          • 25

          #5
          Originally posted by Killer42
          Of course, the function I wrote will produce identical filenames if given the same "base" twice within one second. Given the speed of a typical PC these days, this is certainly possible. Whether it's likely will depend on your individual circumstances. I don't think Format() function will return tenths of a second, but if you want the time to that level of precision I'm sure you can get it somewhere. I seem to recall reading about the Windows system clock "ticking" 18,000 times per second, or something.

          You could also do things to ensure (or at least increase the probability of) a unique name, such as tacking on a random character, or checking for an existing file and modifying your new name if you find one. The code provided was just a quick proof-of-concept type of thing. Feel free to build on it.

          (Oh! I forgot. The Timer function does return fractions of a second. So you can use that to pin down the time to less than a second, if necessary.)
          Killer42, the code below: will write the file and give it a name however when i look at the file name it looks like testfile10DDYYY Y13MMSS.txt

          why doesn't the Days, year, minutes and seconds appear?
          Code:
          Public _name As String = "c:\testfile"
              Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
                  Dim file As New System.IO.StreamWriter(_name & Format(Now, "MMDDYYYY") & ".txt")
                  file.Close()
              End Sub

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by cmdolcet69
            why doesn't the Days, year, minutes and seconds appear?
            I have no idea what happened to the days and years. As for the hours, minutes and seconds, they weren't included in your code. Are you showing us output from the wrong code? For instance, where did the "13" come from? I don't see anything that would have produced that.

            Um... what VB version are you using? I don't think it has been mentioned yet. Based on the procedure declaration, it's one of the .Net version. I think they handle formatting of a date/time value quite differently, which may explain the difficulties. If you look up how to format a date/time into a string in your version, that may resolve the issue. Perhaps it even allows tenths or hundredths of a second. The Format() function in VB6 doesn't, but they may have improved the available time precision/resolution since then.

            Note, that's one reason why I like to encapsulate this sort of thing in a function. Using a "black box" like that makes it easier to replace the code without affecting anything else.

            Oh, and a question. Do you actually need the date and time in the file name, or was that just a way of trying to ensure the names are unique? In other words, does it matter what the files are actually called? Because there is a Windows API function (I forget the name) that you can call to ask Windows for a temporary file name. It produces the sort of weird names you typically see in the Temp folder.

            Comment

            Working...