File creation and naming increment
-
I'd start with checking if the folder exists, if so I would count the number of txt files in there (frankly I would use a different extension, simply because other people can drop .txt files in there) and I would count how many exist. Then I would take that number, add a 1 to it and create my "Text" & i.ToString & ".ext" and close it up. I use i to count. Enjoy :)
-
Dont use the count, cuz if a file is deleted, the 'count + 1' will result in a file name already in use. Get the fileinfos for the files, then find which is the most recent, then get the number from that and then increment
-
Hi, For my web application, I am creating a text file that is stored in a shared network folder. This file needs to have a generic name (ie Text.txt) and each subsequent file will be named with incrementing numbers(Text2.txt then Text3.txt, etc). How do I create the function that will open the target folder, look to see what the last created file was and then create the new file with the appropriate number added to the filename? I also need to pass the created file name to my second webform where it will be monitored. I want to execute the creation of the text file on a button click. Thanks in advance for any help given!
Store the last number used in a database or the config file or something, that will allow you to manage it.
-
Hi, For my web application, I am creating a text file that is stored in a shared network folder. This file needs to have a generic name (ie Text.txt) and each subsequent file will be named with incrementing numbers(Text2.txt then Text3.txt, etc). How do I create the function that will open the target folder, look to see what the last created file was and then create the new file with the appropriate number added to the filename? I also need to pass the created file name to my second webform where it will be monitored. I want to execute the creation of the text file on a button click. Thanks in advance for any help given!
You could use recursion. steps: Check if file text.txt exist --> if so ad a number to the name and redo check (exacute untill check is false (increase the number everytime) --> if not create/save the file with the current name If you want I have some vb.net code that does this somewhere but can't look it up at the moment since I'm not at the office.
-
Hi, For my web application, I am creating a text file that is stored in a shared network folder. This file needs to have a generic name (ie Text.txt) and each subsequent file will be named with incrementing numbers(Text2.txt then Text3.txt, etc). How do I create the function that will open the target folder, look to see what the last created file was and then create the new file with the appropriate number added to the filename? I also need to pass the created file name to my second webform where it will be monitored. I want to execute the creation of the text file on a button click. Thanks in advance for any help given!
-
- Get all filenames in a directory 2) Sort the list of names using a custom comparer to put your files ("TextNN.txt") with the highest number first 3) Take the first file after the sort and extract the number regards
Hi, if the number embedded in the filename uses a fixed format with leading zeroes the alphabetical order is the numeric order too, so no special comparer would be needed. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Good riddance W
modified on Friday, June 10, 2011 12:08 PM
-
Hi, For my web application, I am creating a text file that is stored in a shared network folder. This file needs to have a generic name (ie Text.txt) and each subsequent file will be named with incrementing numbers(Text2.txt then Text3.txt, etc). How do I create the function that will open the target folder, look to see what the last created file was and then create the new file with the appropriate number added to the filename? I also need to pass the created file name to my second webform where it will be monitored. I want to execute the creation of the text file on a button click. Thanks in advance for any help given!
You are creating a web form to monitor a file on a network share? Everyone else has addressed your questions adequately so I will just pose two alternatives which may be easier, if sequence is not important but uniqueness is use a GUID, if both are important a time stamp is also a good option.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego. -
- Get all filenames in a directory 2) Sort the list of names using a custom comparer to put your files ("TextNN.txt") with the highest number first 3) Take the first file after the sort and extract the number regards
Thank you all for your posts. It's been helpful. I'm still a little stuck, but I have the general idea. The files may get deleted, so I will need to take that into account. Also, I'm not using any database and for this application wont be using one. This is what I have so far. Please let me know if there are better methods:
public void filecount(string file)
{
int x;
int y;
string file;int filepaths = Directory.GetFiles(@"C:\Desktop\Request", "*txt").Length;
x = filepaths +1;StreamWriter sw;
sw = File.CreateText(@"C:\Desktop\Request\Text" +y +".txt");
file = Path.GetFileName(@"C:\Desktop\Request\Text" +y +".txt").ToString;
sw.Close();return file;
}public void btn_Click(object sender, EventArgs e)
{
filecount(file);
}Thanks again for helping a newbie out!
-
You are creating a web form to monitor a file on a network share? Everyone else has addressed your questions adequately so I will just pose two alternatives which may be easier, if sequence is not important but uniqueness is use a GUID, if both are important a time stamp is also a good option.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.The webform is used for more than just monitoring the text file. I need to have a text file as it is being used by another program. I originally thought of adding a timestamp into the filename to differentiate however, I thought an incrementing integer would be easier.
-
The webform is used for more than just monitoring the text file. I need to have a text file as it is being used by another program. I originally thought of adding a timestamp into the filename to differentiate however, I thought an incrementing integer would be easier.
Incrementing the integer requires the following steps: 1) Find the highest file 2) Attempt to create the next number 3) Repeat on failure A time stamp requires: 1) Attempt to create the file 2) Repeat on failure Unfortunately, only the Win32 API allows you to do this without a try catch loop which I find poor practice.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego. -
Hi, if the number embedded in the filename uses a fixed format with leading zeroes the alphabetical order is the numeric order too, so no special comparer would be needed. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Good riddance W
modified on Friday, June 10, 2011 12:08 PM
Luc Pattyn wrote:
if the number embedded in the filename uses a fixed format with leading zeroes the alphabetical order is the numeric order too, so no special comparer would be needed.
True, but this requires two things: 1) the number has leading zeros 2) the "TextNN" files are the only files in this folder, no other files like "AAA" and "ZZZ" which would put the "TextNN" files to the middle of the list. Duh, forgot you can use a filter pattern to get the filenames :) With a custom comparer you're on the safe side :)
-
Thank you all for your posts. It's been helpful. I'm still a little stuck, but I have the general idea. The files may get deleted, so I will need to take that into account. Also, I'm not using any database and for this application wont be using one. This is what I have so far. Please let me know if there are better methods:
public void filecount(string file)
{
int x;
int y;
string file;int filepaths = Directory.GetFiles(@"C:\Desktop\Request", "*txt").Length;
x = filepaths +1;StreamWriter sw;
sw = File.CreateText(@"C:\Desktop\Request\Text" +y +".txt");
file = Path.GetFileName(@"C:\Desktop\Request\Text" +y +".txt").ToString;
sw.Close();return file;
}public void btn_Click(object sender, EventArgs e)
{
filecount(file);
}Thanks again for helping a newbie out!
That's how I'd do it:
string[] logfiles = Directory.GetFiles(@"../Files/", @"Text*.txt");
Array.Sort(logfiles, CompareFileNumbers);int highestNumber = int.Parse(Regex.Match(logfiles[0], @"(\d+)").Value);
with the custom comparer being:
public static int CompareFileNumbers(string fileA, string fileB)
{
Regex regexNumber = new Regex(@"(\d+)");int fileNumberA = int.Parse(regexNumber.Match(fileA).Value); int fileNumberB = int.Parse(regexNumber.Match(fileB).Value); return -fileNumberA.CompareTo(fileNumberB); // the miuns sign will sort descending
}
Tested and it works. You need to add error checking though in case the filenames don't have numbers (the regexes will fail) and other cases. regards
-
Incrementing the integer requires the following steps: 1) Find the highest file 2) Attempt to create the next number 3) Repeat on failure A time stamp requires: 1) Attempt to create the file 2) Repeat on failure Unfortunately, only the Win32 API allows you to do this without a try catch loop which I find poor practice.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.You can check to see if a file with that name already exists.
-
You can check to see if a file with that name already exists.
Doesn't work in concurrent systems as the check for existence and creation have to be the same act, otherwise File.Exists then File.Create can fail.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego. -
Doesn't work in concurrent systems as the check for existence and creation have to be the same act, otherwise File.Exists then File.Create can fail.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.I know. I don't think there's any way around a try/catch. I would still opt for a database.
-
I know. I don't think there's any way around a try/catch. I would still opt for a database.
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx[^]
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego. -
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx[^]
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.Didn't the original poster mention the API?
-
Didn't the original poster mention the API?
??
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego. -
??
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.