Search code examples
c#.netvlclibvlcvlcj

VLC: Move file on end


In Windows, after double-clicking a video file, when it's finished, I want the file to be moved up a dir or 2, and any containing folder deleted.

I only want this to affect files located in C:\Users\User1\Downloads, say %x%.

There are 2 scenarios:

  1. If the file is %x%\Training.4273865.2013.avi, it should be moved to ..\Viewed\.

  2. If the file is %x%\Showcase\SomeFile.mp4, it should be moved to the same folder: ..\..\Viewed\. The Showcase folder should then be deleted. Currently, I have to close VLC (to close the file handle) before Showcase (and it's other contents) can be deleted.

A solution would be good, but I don't mind any language I can compile with or similar open-source compiler.


Solution

  • Here is an example C# application that can do what you're asking. Launch it by right clicking a video file, choosing Open With, and selecting the C# application's executable (you can check the box for "Always use the selected program to open this kind of file" to make the change permanent).

    static void Main(string[] args)
    {
        if (args.Length < 1)
            return;
    
        string vlc = @"C:\Program Files\VideoLAN\VLC\vlc.exe";
        string videoFile = args[0];
        string pathAffected = @"C:\Users\User1\Downloads";
        string destinationPath = System.IO.Directory.GetParent(pathAffected).FullName;
        destinationPath = System.IO.Path.Combine(destinationPath, @"Viewed\");
    
        Process vlcProcess = new Process();
        vlcProcess.StartInfo.FileName = vlc;
        vlcProcess.StartInfo.Arguments = "\"" + videoFile + "\"";
        vlcProcess.StartInfo.Arguments += " --play-and-exit";
        vlcProcess.Start();
        vlcProcess.WaitForExit();
    
        if (videoFile.IndexOf(pathAffected,
            StringComparison.InvariantCultureIgnoreCase) >= 0)
        {
            System.IO.File.Move(videoFile,
                System.IO.Path.Combine(destinationPath,
                System.IO.Path.GetFileName(videoFile)));
    
            if (IsSubfolder(pathAffected, 
                System.IO.Path.GetDirectoryName(videoFile)))
            {
                System.IO.Directory.Delete(
                    System.IO.Directory.GetParent(videoFile).FullName, true);
            }
        }
    
    }
    

    I found the code for IsSubfolder in this question.