Click here to Skip to main content
15,891,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a Tow Directories which name is MyFolder1 and Myfolder2. MyFolder1 store lots of file just like
(1) 1452_43552388(jsop2201).Mp3
(2) 1452_43552389(jsop2202).Mp3
(3) 1452_43552390(jsop2203).Mp3
(4) 1452_43552266(Apsp2201).Mp3
(5) 1452_43552278(MNOP4281).Mp3
(6) 1452_43552297(HSOP2258).Mp3
Firstly I want to search file Name only jsop
(1) 1452_43552388(jsop2201).Mp3
(2) 1452_43552389(jsop2202).Mp3
(3) 1452_43552390(jsop2203).Mp3
and automatic copy and paste another directories(MyFolder2). Please help me........
Posted
Updated 25-Aug-14 20:38pm
v3
Comments
[no name] 25-Aug-14 9:18am    
Okay so use GetFiles and then File.Copy.
Kschuler 25-Aug-14 9:26am    
Are you wanting to copy files on the server or client side?
Ram jha 26-Aug-14 2:25am    
I want to Before search file particular just like jsop.....(above the file name) then copy to another folder.

Try:
C#
string[] files = Directory.GetFiles(pathtoMyFolder1, "*(jsop*");
foreach (string oldPath in files)
    {
    string newPath = Path.Combine(pathtoMyFolder2, Path.GetFileName(oldPath));
    File.Copy(oldPath, newPath);
    }
 
Share this answer
 
v2
Comments
OriginalGriff 26-Aug-14 3:06am    
You're welcome!
Ram jha 26-Aug-14 3:33am    
Hi.......OriginalGriff
My Full folder path is E:\\MyFolder1\\MyFolder2\\MyFolder4\\pathtoMyFolder6(pathtoMyFolder6 have contain "jsop" files)

I want to locate\set path only E:\\ and find out "jsop" file. is it possible?
OriginalGriff 26-Aug-14 4:02am    
You can...but it's not a good idea to start with root folders: windows is picky about what you are allowed to do there, and you can get access violations due to hidden special files in the root. If you restrict it to "E:\MyFolder1", you can find all files in all folder beneath that without problems though:

string[] files = Directory.GetFiles(@"E:\MyFolder1", "*jsop*", SearchOptions.AllDirectories);

Will return them with the full path.
Do make sure that this doesn't include the files you are copying to! :laugh:
You may also have to be a bit cleverer about how you handle your paths when you construct the "output path" to prevent overwriting - using string.Remove to take of the base folder and keep the rest instead of just using Path.GetFileName may be what you want. (But remember there is a limit on the maximum length of a path as well!)
Ram jha 26-Aug-14 4:22am    
You're Great Sir,
Thanks for explain.
OriginalGriff 26-Aug-14 4:23am    
You're welcome!
Here is a solution for copy files:
C#
//Copy one file:
string fileName = "test.mp3";
            string sourcePath = @"C:\dir\";
            string targetPath = @"C:\dir\subdir\";

            
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            
            System.IO.File.Copy(sourceFile, destFile, true);

Copy more files at once:
C#
//Copy selected files:
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = {"C:\\dir\\1.mp3", "C:\\dir\\2.mp3", "C:\\dir\\3.mp3"};

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
string fileName = "1.mp3";
string targetPath = @"C:\dir\subdir\";

                    string destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {

            }
 
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