Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have problém with file name. I would like to put following number to file name. My code didn´t work.

What I have tried:

 int value = 0;
for (int i = 1; i < 10; i++)
   {
   value = i;
   };
            filePath_output = Path.GetFileName(filePath_input);
            filePath_output = Path.GetDirectoryName(filePath_input) + "\\" + "Vystup"+ value);
            
            this.textBox2.Text = filePath_output;
Posted
Updated 30-Sep-20 2:33am
Comments
Pete O'Hanlon 30-Sep-20 8:11am    
What do you mean when you say it doesn't work? What are you expecting to see when you run it?
dejf111 30-Sep-20 8:16am    
I would like to see "filename n" n=series of numbers depends on the number of saves
but this do nothing beacuse program expected some diffrent thing
dejf111 30-Sep-20 8:21am    
I´m idiot :D so sorry I see it
Pete O'Hanlon 30-Sep-20 8:34am    
You're not an idiot. Sometimes it just takes explaining a problem to someone else to see it.

Your for loop doesn't actually do anything - you might as well assign 9 to the value variable to start with.

You'll need to extract the original filename from the path. You'll probably also want to put the number before the file extension.
C#
public static string AppendFileNumber(string filePath, int fileNumber)
{
    string directory = Path.GetDirectoryName(filePath);
    string fileName = Path.GetFileNameWithoutExtension(filePath);
    string extension = Path.GetExtension(filePath);
    return Path.Combine(directory, fileName + " " + fileName + extension);
}

...

int value = 9;
this.textBox2.Text = AppendFileNumber(filePath_input, value);
 
Share this answer
 
If what you are after is to get something like the following:
c:\myfolder\file.doc
c:\myfolder\file1.doc
c:\myfolder\file2.doc
then your logic needs quite a bit of work. First, break your problem down.

Step 1. Does the file already exist?
C#
public string GetUniqueFilename(string file)
{
  if (File.Exists(file))
  {
    // The file already exists, so get a new filename
  }
  return file;
}
The next stage is to split the file name, extension and path into their most basic parts:
C#
string folder = Path.GetDirectoryName(file);
string fileName = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
What you now need to do is use a simple loop to iterate and create the new filename.
C#
int fileCounter = 1;
while (File.Exists(file))
{
  file = Path.Combine(folder, $"{fileName}{fileCounter}{extension}");
  fileCounter++;
}
Now that loop will keep going until it finds a unique file name at which point it will finish. As an optimisation, you will probably notice that you can drop the original File.Exists check and leave this entirely down to the while loop.
 
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