none
C# Recursive Scan For Specific File Types Only RRS feed

  • Question

  • I am utilizing the below code which will move all files regardless of extension to the new directory, i want to only move .txt, .png, and .avi files to the new directory and ignore all other file extensions.  

    How should this be modified to achieve such?

    static void Main(string[] args)
    {
    	String directoryName = "D:\\__Completed";
    	DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
    	if (dirInfo.Exists == false)
    		Directory.CreateDirectory(directoryName);
    
    	List<String> MyMusicFiles = Directory
    					   .GetFiles("D:\\__IPT", "*.*", SearchOption.AllDirectories).ToList();
    
    	foreach (string file in MyMusicFiles)
    	{
    		FileInfo mFile = new FileInfo(file);
    		// to remove name collisions
    		if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
    		{
    			mFile.MoveTo(dirInfo + "\\" + mFile.Name);
    		}
    	}
    }

    Sunday, April 15, 2018 6:45 AM

Answers

  • You can do this operation while getting the files itself, so that you dont get the unecessary files from the folder. Use the following code,

      List<String>MyMusicFiles = Directory.GetFiles("D:\\__IPT", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".txt") || s.EndsWith(".png") || s.EndsWith("avi ")).ToList();
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (kokkisajee) or Facebook (sajeetharan) profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by IndigoMontoya Sunday, April 15, 2018 6:55 AM
    Sunday, April 15, 2018 6:54 AM

All replies

  • You can do this operation while getting the files itself, so that you dont get the unecessary files from the folder. Use the following code,

      List<String>MyMusicFiles = Directory.GetFiles("D:\\__IPT", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".txt") || s.EndsWith(".png") || s.EndsWith("avi ")).ToList();
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (kokkisajee) or Facebook (sajeetharan) profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by IndigoMontoya Sunday, April 15, 2018 6:55 AM
    Sunday, April 15, 2018 6:54 AM
  • I am utilizing the below code which will move all files regardless of extension to the new directory, i want to only move .txt, .png, and .avi files to the new directory and ignore all other file extensions.  

    How should this be modified to achieve such?

    static void Main(string[] args)
    {
    	String directoryName = "D:\\__Completed";
    	DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
    	if (dirInfo.Exists == false)
    		Directory.CreateDirectory(directoryName);
    
    	List<String> MyMusicFiles = Directory
    					   .GetFiles("D:\\__IPT", "*.*", SearchOption.AllDirectories).ToList();
    
    	foreach (string file in MyMusicFiles)
    	{
    		FileInfo mFile = new FileInfo(file);
    		// to remove name collisions
    		if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
    		{
    			mFile.MoveTo(dirInfo + "\\" + mFile.Name);
    		}
    	}
    }

    Here's the kind of thing I'd do:

    1. By avoiding ToList() it will run much more smoothly (it won't need to create a possibly HUGE list)

    2. By grouping on the file name part there's no risk of collisions.

    using System;
    using System.IO;
    using System.Linq;
    
    namespace ConsoleApp9
    {
        class Program
        {
            static void Main(string[] args)
            {
                String directoryName = "D:\\__Completed";
    
                DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
    
                if (dirInfo.Exists == false)
                    Directory.CreateDirectory(directoryName);
    
                var MyMusicFiles = Directory.EnumerateFiles("D:\\__IPT", "*.*", SearchOption.AllDirectories)
                    .Where(f => MeetsCriteria(f))
                    .GroupBy(f => Path.GetFileName(f).ToLower())
                    .Select(f => f.First());
    
                foreach (string file in MyMusicFiles)
                {
                    FileInfo mFile = new FileInfo(file);
                    mFile.MoveTo(dirInfo + "\\" + mFile.Name);
                }
            }
    
            private static bool MeetsCriteria (string Filename)
            {
                var ext = Path.GetExtension(Filename).ToLower();
    
                return (ext == "txt" || ext == "png" || ext == "avi");
            }
        }
    }

    Sunday, May 6, 2018 8:47 PM