Generating unique filenames

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 83
Reputation: Elmo_loves_you is an unknown quantity at this point 
Solved Threads: 0
Elmo_loves_you's Avatar
Elmo_loves_you Elmo_loves_you is offline Offline
Junior Poster in Training

Generating unique filenames

 
0
  #1
Sep 19th, 2008
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
Michelle (Junior Developer)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: BeejCyr is an unknown quantity at this point 
Solved Threads: 0
BeejCyr's Avatar
BeejCyr BeejCyr is offline Offline
Newbie Poster

Re: Generating unique filenames

 
0
  #2
Sep 19th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 83
Reputation: Elmo_loves_you is an unknown quantity at this point 
Solved Threads: 0
Elmo_loves_you's Avatar
Elmo_loves_you Elmo_loves_you is offline Offline
Junior Poster in Training

Re: Generating unique filenames

 
0
  #3
Sep 25th, 2008
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
Michelle (Junior Developer)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC