Click here to Skip to main content
15,921,203 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
I have one task in that i have to save files in same folder. If file name is already exists by that name then i want to give same name and add that new file as 1,2,3....& so on, Like Backup(1), Backup(2), Backup(3)....& so on. I want it programatically.
Posted
Updated 20-Mar-19 4:12am
Comments
Richard MacCutchan 16-Jun-11 9:57am    
And what exactly is the problem that you face in doing this?
sandeep.somvanshi83 16-Jun-11 10:13am    
My problem is i want to check wethere file name is already exists. If yes then i want to save new file with the name, which is having by old one and adding some number like 1,2... It's like versioning.
Tarun.K.S 16-Jun-11 10:00am    
How much have you tried?
sandeep.somvanshi83 16-Jun-11 10:04am    
I'm trying by afternoon.

Try to do like that, you might need to do some change, but this logic will work

do
{
    fileCount++;
}
while (System.IO.File.Exists("FileName" + (fileCount > 0 ? "(" + fileCount.ToString() + ")" : "")));

            //Not create a file
System.IO.File.Create("FileName" + (fileCount > 0 ? "(" + (fileCount + 1).ToString() + ")" : ""));
 
Share this answer
 
Comments
mendy aaron 16-Aug-13 12:22pm    
Just make sure to start fileCount at -1 and to put the extension after the parenthesis logic.
Matthias Adloff 13-Jan-15 15:44pm    
Not only that you forgot to mention the -not completely obvious- additions that mendy pointed out, your solution will stop working after the first file name replacement.
If you have saved your first replacement (2) file, it will then look for
- fileCount 0 => exists
- fileCount 1 => does not exist so write (2).

The correct solution is to leave the fileCount + 1 away.
try this code

C#
private void button1_Click(object sender, EventArgs e)
       {
           string fileName = "C:\\Backup";
           int count = 0;
           Find :
           if (File.Exists(fileName))
           {
               fileName = fileName + "(" + count.ToString() + ").txt";
               count++;
               goto Find;
           }
           else
           {
               //Add your logic here
               File.Create(fileName);
           }
       }
 
Share this answer
 
v2
Comments
sandeep.somvanshi83 16-Jun-11 10:23am    
Thanks for good response.But it creating files of 0kb. It doesnot contain any data.
ambarishtv 16-Jun-11 10:30am    
Add your logic in that commented area.
NikolaMihajlov 25-Jan-14 21:51pm    
Thanks. Very nice and easy.
Hello Dear
there is a method which determines if there is a file in a directory
System.IO.File.Exists("FilePath")

you can use it like this
MIDL
if (System.IO.File.Exists("FilePath"))
{
}
else
{
}
 
Share this answer
 
Comments
sandeep.somvanshi83 16-Jun-11 10:01am    
I can't increment the file names. It's like giving versions.
You can take this approach. This would work well if all these files are created in order. If you are expecting any gaps, you can add additional logic. However, its safe to add a check if file exist before creating the file

C#
string dirPath = @"C:\BackUp";
string fileName = "BackUp";
string[] files = Directory.GetFiles(dirPath);
int count = files.Count(file => { return file.Contains(fileName); });

string newFileName = (count == 0) ? "BackUp.bak" : String.Format("{0} ({1}).bak", fileName, count + 1);


In all the other solution disk IO depends on number of files already exists. In this case its just one.
 
Share this answer
 
v3
First add some path with file name:

string filePath = "c:\somefile.txt";

than chack if alredy exists:

FileInfo fi = new FileInfo(filePath);
if (fi.Exists)
{
//add new name, and create file
}
else
{
using (StreamWriter streamWriter == new StreamWriter(filePath, true))
{
streamWriter.WriteLine(@"some text");
streamWriter.Flush();
streamWriter.Close();
}
}
 
Share this answer
 
v4
Comments
ambarishtv 16-Jun-11 10:31am    
please check your code before posting
Try Directory.EnumerateFiles()[^], which will give you a list of files that you can check against your own template in order to decide what name you should use for the next in sequence.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900