简体   繁体   中英

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");
    }
}

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM