Need help on approach

  • Thread starter Thread starter BGCSOCAL
  • Start date Start date
B

BGCSOCAL

I am not really a developer but I have to do a proof of concept. Part of the
effort involves the VB program writing a text file with a file name like
APmmddyy.ext. The extension is a sequencial number starting at 100 that
increments 1 for each additional file that is saved in this folder for the
same day. The next day the extension restarts at 100. I'm can't think of a
viable approach to determining what the extension should be on any given day.
Any ideas you would be willing to share?

I'm using Visual Studio 2005 to do this development.
 
My first question is "does the file extension have to be the part that
changes"? By having a non-standard file extension, the operating system
won't be able to recognize the file automatically. Why not have the main
file name be the changing part? This would give you more characters to play
with and therefore more naming options. The file names could simply be
based on the date and time the file was created such as:

10-1-2009-11.45.59AM.txt

-Scott
 
Yes the extension has to be the number. The file the program creates will be
fed into another program that expects that format and has existed for years.
there are two other sources of these files: one starts at 001 and one starts
at 050.
 
Yes the extension has to be the number. The file the program creates will be
fed into another program that expects that format and has existed for years.
there are two other sources of these files: one starts at 001 and one starts
at 050.

I see... Well, if the only files in the directory are these files, then you
can use System.IO.Directory.GetFiles to get all of the files in the directory and then
sort them by file name.

Once sorted, look at the date creation date on the file - if it was today,
grap it's extension with the System.IO.Path.GetExtension method. Convert the
number to an integer, increment it by one, and then use it for the extension
of the new file. If the date was not today, delete all of the files, and set
your counter to 100.
 
Thanks. What I did while you were replying was to create a little one-line
text file with the file date and the extension on the line. Then I read the
file and its one line and closed it. If the processing worked and I wrote an
output file, I created a new text file with the same name and write it out
with the new data in it.

What are the pros and cons of the two approaches? Performance is not an
issue in this case.
 
Thanks. What I did while you were replying was to create a little one-line
text file with the file date and the extension on the line. Then I read the
file and its one line and closed it. If the processing worked and I wrote an
output file, I created a new text file with the same name and write it out
with the new data in it.

What are the pros and cons of the two approaches? Performance is not an
issue in this case.

that works to. maybe even better...
 
Your approach will have much more overhead. There's no need to read and
write the file to find out that it exists.

If reading all files and sorting and checking the last is more complex than
you need, then you could sequentially examine each possibility starting at
APmmddyy.100 and incrementing the extension by one until you find a filename
that doesn't exist - that's the one to be written as the new file. Look at
the FileInfo.Exists method for details on how to check if a file with a
particular name exists in a directory.

http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
 
James Hahn said:
Your approach will have much more overhead. There's no need to read and
write the file to find out that it exists.

If reading all files and sorting and checking the last is more complex
than you need, then you could sequentially examine each possibility
starting at APmmddyy.100 and incrementing the extension by one until you
find a filename that doesn't exist - that's the one to be written as the
new file. Look at the FileInfo.Exists method for details on how to check
if a file with a particular name exists in a directory.

http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

Not to mention that Visual Studio 2005/08 includes several code snippets in
the provided library for doing these types of operations.

-Scott
 
BGCSOCAL said:
Yes the extension has to be the number. The file the program creates will be
fed into another program that expects that format and has existed for years.
there are two other sources of these files: one starts at 001 and one starts
at 050.

Is this a service creating the files, or a sporadically run app run by a
user throughout the day? It wasn't clear from the thread yet.

If it is a service, then the counter and a time just need to be kept in
memory to do this. Save the time of the last write. Increment the
counter as needed, but check to see if the last write was yesterday. If
it was, then reset the counter.

If it is a program run by a user, then the others have the usual
approach. I just think it a bit odd that the files are created this way
by an interactive user.
 
Family Tree Mike said:
Is this a service creating the files, or a sporadically run app run by a
user throughout the day? It wasn't clear from the thread yet.

If it is a service, then the counter and a time just need to be kept in
memory to do this. Save the time of the last write. Increment the
counter as needed, but check to see if the last write was yesterday. If
it was, then reset the counter.

You still need to find the next number in the sequence if ever the service
is stopped and restarted, unless you commit "next File Num" to disk at
every update, even then it's not certain that you would not have errors -
say in case of a power failure.
If it is a program run by a user, then the others have the usual approach.
I just think it a bit odd that the files are created this way by an
interactive user.

It could be a data acquisition programme, or a financial prices tracker -
this sort of thing happens a lot.
 
This is a user initiated program. As Clive suggested, this program is
creating this file with data from a new trading system. The file that this
program writes will be picked up by another program and fed to our accouting
link. The program is run maybe 8-9 times a day normally. That is why I wasn't
too worried about performance.
 
That's an intriguing idea. Thanks.
--
Bettie


Scott M. said:
Not to mention that Visual Studio 2005/08 includes several code snippets in
the provided library for doing these types of operations.

-Scott
 
Hello, Bettie,

Re:
...The program is run maybe 8-9 times a day normally. ...

If there is any possibility that the program can be run more than once
simultaneously, you will need to be careful to avoid a conflict where two
instances of the program are attempting to create a file with the same name.

Cheers,
Randy
 
Back
Top