Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The names of the files's markers in appropriate folders that have .csv addition, should move.



i have one folder.
C:\\Week
My folder includes 2 folders like A and B.
C:\\Week\\A C:\\Week\\B
And each folder includes a lot of folders.
C:\\Week\\A\\abc
And "abc" folder includes some files.
C:\\week\\A\\abc\\abcxyz
I need to move my file in subfolder to another folder that folder should has a same name with my file name.

for example
abcxyz is folder.
i need to move my abcxyz file to abcxyz folder.
C\\week\\B\\abcxyz


i need to compare name. But i don't know how to do. Please help me.
Can you check my code?

Thank you.

[Edit]See OP's code so far:
C#
public void fileMove(string folder)
       {
           const string destPath = "C:\\Optima\\Target\\";
           string[] folders = Directory.GetDirectories(folder);
           string[] destFolders = Directory.GetDirectories(destPath);

           foreach (string fArray in folders)
           {                                                // fArray
               DirectoryInfo folderInfo = new DirectoryInfo(folder);
                         FileInfo[] files = folderInfo.GetFiles("*.csv");

                        DirectoryInfo destFolderInfo = new DirectoryInfo(destPath);
                   foreach(FileInfo file in files){
             string fileName = Path.GetFileNameWithoutExtension(file.Name);
               if (Directory.Exists(destPath+fileName)){

                   string destFolder = destPath + fileName;
                   File.Move(folder + file, destPath + fileName);

                       }

                   else{
                                           Directory.CreateDirectory(destPath +fileName);
                                       }
                   }

                    fileMove(folderInfo.FullName);


           }
       }
   }
Posted
Updated 28-Aug-12 20:59pm
v9
Comments
[no name] 28-Aug-12 6:51am    
I find the File.Move usually helps move files around. What you mean by "i need to compare name" I have no idea.

Use System.IO.Path class, it has many methods to extract different parts of file name.
You can also use it to construct new path (destination) from parts extracted form source path.
 
Share this answer
 
Thank you and i know this command.

if my file name is Selin. I need to move folder of Selin.
if my file name is x . I need to move folder of x.

But i have a lot of folder and file. I have to use loop.
Please check my code.
C#
public static void fileMove(string folder)
       {
           string[] folders = Directory.GetDirectories(folder);
           foreach (string fArray in folders)
           {

               DirectoryInfo folderInfo = new DirectoryInfo(fArray);
               string[] files = Directory.GetFiles("*.csv", folderInfo.FullName);

              string[] destFolders = Directory.GetDirectories(@"C:\\Optima\\Target");

                foreach (string destFolderArray in destFolders)
               {
                    DirectoryInfo destFolderInfo = new DirectoryInfo(destFolderArray);

               }


               foreach( string file in files)
               {   FileInfo fileInfo = new FileInfo(file);

    }

               if (destFolderInfo.Name == fileInfo.Name){
                              File.Move(file, destFolder);
                                 }


               fileMove(folderInfo.FullName);

           }
       }
   }
 
Share this answer
 
v2
Comments
lukeer 28-Aug-12 7:37am    
1) Please don't post additional information as "solution". Use the "Improve question" link instead. It's just below your original question.
2) Please place code with in proper tags: <pre lang="c#">YourCodeHere()</pre> (FTFY)
3) Check the code I inserted into your question for correct intendation. I'm not really sure that everything is in the place you meant it to be. (again, use "Improve question" to correct it.)

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