Move Specific File Extensions From Dir To Dir

I am attempting to execute this syntax to move files from my scandir to my movedir the code compiles fine and when run throws no errors - but the files are not moved?

I am running this on a UNIX machine through monodev.

class MainClass
{
    private const string scandir = "/home/owner/Downloads/";
    private const string movedir = "/home/owner/Documents/Videos/";

    private static void Main(string[] args)
    {
        MoveFiles();
    }

    private static void MoveFiles()
    {
        var MyFiles = Directory.EnumerateFiles(scandir, "*.*", SearchOption.AllDirectories)
            .Where(f => MeetsCriteria(f))
            .GroupBy(f => Path.GetFileName(f).ToLower())
            .Select(f => f.First());

        foreach (string file in MyFiles)
        {
            FileInfo mFile = new FileInfo(file);
            mFile.MoveTo(movedir + "\\" + mFile.Name);
        }
    }
    private static bool MeetsCriteria(string Filename)
    {
        var ext = Path.GetExtension(Filename).ToLower();

        return (ext == "avi" || ext == "mp4" || ext == "m4v" || ext == "mkv");
    }
}

1 answer

  • answered 2018-10-11 19:36 adrianbanks

    Path.GetExtension() returns the file extension including the leading dot. Your MeetsCriteria method is therefore never going to match an extension.

    Add leading dots to your file extensions:

    return (ext == ".avi" || ext == ".mp4" || ext == ".m4v" || ext == ".mkv");