Home > database >  Copy file and if exist create new extension
Copy file and if exist create new extension

Time:09-13

I have a problem, i am trying to write a program that will copy a file with the same name from A to B and i want to change the exension from bk2 to bk3,4,5,6,7 or just rename the filename so the program can copy the file even if the same name allready exist.

I am very beginner in C# everything is dificult now.

Below the code

enter code here


static void Main(string[] args)
{
    //path of file
    string pathToOriginalFile = @"D:\C#\LOGS_PROG\SystemLog.bk2";
    string extension = ".bk2";


    //duplicate file path 
    string PathForDuplicateFile = @"D:\C#\Backup\Systemlog";

    //provide source and destination file paths
    File.Copy(pathToOriginalFile, PathForDuplicateFile   extension);

   

}

}

CodePudding user response:

There is few tips. You can use System.IO.Path.

  • path of file

         string pathToOriginalFile = @"D:\C#\LOGS_PROG\SystemLog.bk2";
    
  • Where the file is located (D:\C#\LOGS_PROG) be careful, there is no \ at the end

         string where = Path.GetDirectoryName(pathToOriginalFile);
    
  • THe name of the wile without its extension (SystemLog)

         string name = Path.GetFileNameWithoutExtension(pathToOriginalFile);
    
  • its extension (.bk2)

         string extension = Path.GetExtension(pathToOriginalFile);
    
  • The name and the extension (SystemLog.bk2)

         string nameAndExtension = Path.GetFileName(pathToOriginalFile);
    

Now, if your purpose is to duplicate the file :

 //path of file
 string pathToOriginalFile = @"D:\C#\LOGS_PROG\SystemLog.bk2";

 for (int i = 1; ;   i)
 {
      // For example : D:\C#\LOGS_PROG\SystemLog (9).bk2 if there is the original   8 copy
      string pathToNewFile = Path.Combine(Path.GetDirectoryName(pathToOriginalFile), Path.GetFileNameWithoutExtension(pathToOriginalFile)   " ("   i   ")"   Path.GetExtension(pathToOriginalFile));

      if (!File.Exists(pathToNewFile))
      {
           File.Copy(pathToOriginalFile, pathToNewFile);
           break;
      }
 }

I separated the structure so you can see how it is built but you can use already known variables.

CodePudding user response:

try this:

    static void Main(string[] args)
    {
        //path of file
        string pathToOriginalFile = @"D:\C#\LOGS_PROG\SystemLog.bk2";
        string extension = ".bk2";
        //duplicate file path 
        string PathForDuplicateFile = @"D:\C#\Backup\Systemlog";

        //rename fileName if Exists
        FixFileName(ref PathForDuplicateFile, extension);

        //provide source and destination file paths
        File.Copy(pathToOriginalFile, PathForDuplicateFile   extension);
    }

    static void FixFileName(ref string PathForDuplicateFile, string extention)
    {
        int i = 0;
        while (true)
        {
            i  ;
            if (File.Exists(PathForDuplicateFile   extention))
                PathForDuplicateFile  = i;
            else
                break;
        }
    }
  • Related