943,777 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 4569
  • VB.NET RSS
Sep 19th, 2008
0

Generating unique filenames

Expand Post »
Hey Guys, im a fairly novice programmer

I would just like to hear some views / opinions on how you would go about implementing the following:

I need my program to save data to a textfile giving the file a name and adding a number to the end eg filename_1.txt. So, each time my program runs it saves the data entered to a new textfile (incrementing the number each time)

The way I was thinking of implementing this was to :

1. Create a textfile named count.txt that just contains a number (the number of files created)

2. Each time a new file needs to be created, check the count.txt file to see what the number is, then increment it by one and append it to the new textfile name, then overwrite the number that was in count.txt

3. close the program when data has been entered (creating unique filename)

I have come up with this approach beacuse I need the program to be able to keep track of the number as the user will close the application and only load it when needed.


To me it seems to be a sensible approach, but there are more than likely more efficient ways of doing this. Your comments would be much appreciated

Kind Regards

Michelle
Similar Threads
Reputation Points: 30
Solved Threads: 0
Junior Poster in Training
Elmo_loves_you is offline Offline
85 posts
since Mar 2008
Sep 19th, 2008
0

Re: Generating unique filenames

Well if you only needed a unique name then you could just use:

filename = my.Computer.FileSystem.GetTempFileName

If you want something more predictable you can always do this:

Dim i As Integer = 0
Dim FileMask As String = "MyFile_{0}.txt"
Dim Filename As String = ""

Filename = String.Format(FileMask, i)
Do While My.Computer.FileSystem.FileExists(Filename)
i += 1
Filename = String.Format(FileMask, i)
Loop

' When loop exits, you should have a unique name
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BeejCyr is offline Offline
10 posts
since Sep 2008
Sep 25th, 2008
0

Re: Generating unique filenames

Hi Guys...

Ive had a brainwave .. or at least want to try a different approach.

I want to keep a count of all new files created ... possibly in the mobile devices registry ... or by XML file ....

so my application will boot up for the first time ... realise the count is 0 ... create a new file but increment the count by 1 and attach the number 1 to the new file name ...

Therefore, the next time my application runs, it will check the number in the registry/XML file and increment it by one ---- that way the files created will constantly increment by one each time ... making filenames unque

Please could someone help point me in the right direction to do this....

Many Thanks

Elmo
Reputation Points: 30
Solved Threads: 0
Junior Poster in Training
Elmo_loves_you is offline Offline
85 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Outputting In Listbox....
Next Thread in VB.NET Forum Timeline: displaying current year in textbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC