Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving Duplicate files, appending letters to the filenames

Status
Not open for further replies.

DCSage

Programmer
Mar 5, 2010
74
US
Hi.

I have an application that moves files to a folder every 20 minutes. My users told me that there are bound to be duplicate files and that they usually handle the duplicates by manually adding letters to the files:

Microsoft-IE8-23809-02.edi

at 10am this comes in again and becomes

Microsoft-IE8-23809-02_A.edi

at 10:20 the same file is repostd and then renamed:
Microsoft-IE8-23809-02_B.edi

I have the following code for the move function. If the file already exists in the destination folder, then it should be renamed in the source folder and then moved to the destination folder with the new name? How can I make sure that the letters are recognized and auto-incremented and then properly moved? When I add the move function, I get the error:

Code:
Process is being used by another.


Current Code

Code:
 foreach (string myfile in files)
            {
                //Grab Basic file information
                filename = Path.GetFileName(myfile);
                fu = new FileInfo(filename);
                string afile = fu.Name;

                String dest = Path.Combine(targetPathDir, filename);
                String sou = Path.Combine(sourcePathDir, filename);
                String nTarget = Path.Combine(targetPathDir, filename);
                String nDupe = Path.Combine(dupePathDir, filename);
                String nRnme = Path.Combine(renmeDir, filename);

                char alpha = 'A';
                alpha = (char)((byte)alpha + 1);
                             
                if (File.Exists(dest))
                {
                   while (true)
                       {
                            string extension = Path.GetExtension(myfile);
                            string fname = myfile.Replace(extension, String.Empty);


                            string newfile = fname + "_" + alpha.ToString() + extension; 
                            File.Copy(myfile, newfile);
                            
                            moveFile dMove = new moveFile();
                            dMove.moveAll(myfile, nRnme);                            
                   }                                      
                    
                }
                else
                {
                    moveFile nMove = new moveFile();
                    nMove.moveAll(myfile, nDupe);
                }
            }

Error

Code:
System.IO.IOException: The file 'L:\EDI_TG\Deal_18177-56_B.edi' already exists.



 
I rewrote my function:

Code:
 foreach (string myfile in files)
            {
                //Grab Basic file information
                filename = Path.GetFileName(myfile);
                fu = new FileInfo(filename);
                string afile = fu.Name;

                String dest = Path.Combine(targetPathDir, filename);
                String sou = Path.Combine(sourcePathDir, filename);
                String nTarget = Path.Combine(targetPathDir, filename);
                String nDupe = Path.Combine(dupePathDir, filename);
                String nRnme = Path.Combine(renmeDir, filename);

                char letter = 'A';
  
                for (char alpha = letter; ; alpha++)
                 {
                     if (File.Exists(nTarget))
                     {
                         string extension = Path.GetExtension(myfile);
                         string fname = myfile.Replace(extension, String.Empty);                                      string newfile = fname + "_" + alpha.ToString() + extension;
                         moveFile dMove = new moveFile();
                         dMove.moveAll(newfile, nDupe);

                     }
                     else
                     {
                         moveFile nMove = new moveFile();
                         nMove.moveAll(myfile, nDupe);
                     }
                 }
                    //moveFile nMove = new moveFile();                
                   //dMove.moveAll(myfile, nDupe);
            }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top