473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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: FileName10022007134700.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.
Oct 2 '07 #1
5 1470
Killer42
8,435 Expert 8TB
It's actually pretty simple. Just squish the parts together into a string. For example...

Expand|Select|Wrap|Line Numbers
  1. Public Function FreshFileName(ByVal Base As String) As String
  2. ' Return a file name including the current date and time.
  3. FreshFileName = Base & Format(Now,"MMDDYYYYHHNNSS")
  4. End Function
Oct 3 '07 #2
It's actually pretty simple. Just squish the parts together into a string. For example...

Expand|Select|Wrap|Line Numbers
  1. Public Function FreshFileName(ByVal Base As String) As String
  2. ' Return a file name including the current date and time.
  3. FreshFileName = Base & Format(Now,"MMDDYYYYHHNNSS")
  4. End Function
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
Oct 3 '07 #3
Killer42
8,435 Expert 8TB
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.)
Oct 4 '07 #4
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 testfile10DDYYYY13MMSS.txt

why doesn't the Days, year, minutes and seconds appear?
Expand|Select|Wrap|Line Numbers
  1. Public _name As String = "c:\testfile"
  2.     Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  3.         Dim file As New System.IO.StreamWriter(_name & Format(Now, "MMDDYYYY") & ".txt")
  4.         file.Close()
  5.     End Sub
  6.  
Oct 4 '07 #5
Killer42
8,435 Expert 8TB
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.
Oct 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java...
7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
1
by: Merennulli | last post by:
Ok, did some seriously ugly meddling with javascript so that my user can do a level of formatting which gets shunted into a hidden input field as raw HTML code. That code is then passed back and...
5
by: Eric Cadwell | last post by:
Is there a faster way to write the last 100K of a large file? This code takes almost two minutes to run on my machine. int buffer = 100000; int length = 2000000000; string file =...
9
by: andy.z | last post by:
If 2 people try to access the same text file at the same time to write to it - what happens in PHP ? What I mean is - presumably the first will be ok - But what will the second person actually...
16
by: Hans Fredrik Nordhaug | last post by:
I'm trying to write to a file in the current directory - no remote files. The subject says it all - I can add that both the directory and the file is wordwritable. This happens on a (quite good)...
1
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how...
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.