0

I already create code for copy but I don't know how to combine copy code if the filename are not in jpeg,bmp,png or gif it will copy to different folder name(C:\Dump) but if filename extension exists file will be copy into (C:\Destionation).

public static void CopyFile( string[] args ) { CopyFolder( @"C:\source", @"C:\Destination" ); Console.ReadLine(); } static public void ProcessDirectory(DirectoryInfo directory) { foreach (FileInfo file in directory.EnumerateFiles("*.jpg,*.bmp,*.png,*.gif,*.jpeg")) { //how to combin process directory info with copy folder statement// } } static public void CopyFolder( string sourceDir, string destFolder ) { if (!Directory.Exists( destFolder )) Directory.CreateDirectory( destFolder ); string[] files = Directory.GetFiles( sourceDir ); foreach (string file in files) { string name = Path.GetFileName( file ); string dest = Path.Combine( destFolder, name ); File.Copy( file, dest); } } 
1
  • 1
    Try to enumerate all files (Enumerate("*.*") then switch destination according to extension file.Extension.Equals(".jpg") or - better - simply keep a list of them. A case insensitive search for that extension will give tell you if you have to copy in dump" or destination". Commented Apr 22, 2014 at 13:29

2 Answers 2

1

You can compare the extension of the files with:

Path.GetExtension(myFilePath);

For example, you can add in your code:

foreach (string file in files) { string name = Path.GetFileName( file ); string dest = Path.Combine( destFolder, name ); if(Path.GetExtension(myFilePath) != "jpg" && Path.GetExtension(myFilePath) != "bmp" && Path.GetExtension(myFilePath) != "png" && Path.GetExtension(myFilePath) != "gif" && Path.GetExtension(myFilePath) != "jpeg" ){ File.Copy( file, dest); } } 

You can have a reference in the Path.GetExtension Method documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 This would work but Adriano's method in the question comments is a bit simpler and would be how I would choose to solve the problem.
1

The FileInfo has a property to get the file Extension.

You can enumerate all the files and copy depending on the Extension.

public static void Main(string[] args) { var extensions = new[] { "jpg", "jpeg", "bmp", "png", "gif" }; var source = @"C:\Source"; var destination = @"C:\Destination"; var dump = @"C:\Dump"; CopyFolder(source, destination, dump, extensions); Console.ReadLine(); } public static void CopyFolder( string source, string destination, string dump, string[] extensionsForDestination ) { if (!Directory.Exists(destination)) { Directory.CreateDirectory(destination); } if (!Directory.Exists(dump)) { Directory.CreateDirectory(dump); } var directory = new DirectoryInfo(source); ProcessDirectory(directory, destination, dump, extensionsForDestination); } public static void ProcessDirectory( DirectoryInfo directory, string destination, string dump, string[] extensionsForDestination ) { foreach (FileInfo file in directory.EnumerateFiles()) { // Check if extension matches if(extensionsForDestination.Contains(file.Extension)) { // Copy file to Destination file.CopyTo(destination); } else { // Copy file to Dump file.CopyTo(dump); } } } 

3 Comments

but how if directory also not exist for Dump
Then create it like you are doing for the destination.
thanks for the information..now i will try for delete source folder after copy all files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.