Skip to content

Instantly share code, notes, and snippets.

@Zolomon
Created July 25, 2011 18:56
Show Gist options
  • Select an option

  • Save Zolomon/1104871 to your computer and use it in GitHub Desktop.

Select an option

Save Zolomon/1104871 to your computer and use it in GitHub Desktop.
µTorrent Controller 1.0a
// Author: Bengt Ericsson, July 2011
//
// This tiny program will move files/dirs to the label you give them in µTorrent v3.2.
// Example: download a torrent and save it with its directory "E:/Torrents/my.example.torrent.dir/",
// specify the label "Misc/cool/stuff" and the my.example.torrent.dir/ will be moved to
// "E:/Torrents/Misc/cool/stuff/my.example.torrent.dir/" when it's finished downloading.
//
// 1) Change the variable "torrentPath" to represent the directory where you store your torrents.
// 2) Get http://www.ntwind.com/software/utilities/hstart.html - this will run the app in a none obtrusive way, skip it if you don't mind the console flashing!
// 3) Get http://commandline.codeplex.com and reference it to your solution
// 4) Set up µTorrent to use the Run Program feature when a torrent has finished
// downloading like so; "C:\hstart64.exe" /NOCONSOLE ""C:\uTorrentController.exe" -l "%L" -d "%D""
//
// Think that's it. Hope you have a more pleasant torrent experience!
//
// This is free software. You may redistribute copies of it under the terms of
// the MIT License <http://www.opensource.org/licenses/mit-license.php>.
// Usage: uTorrentController.exe -l "Misc/stuff" -d "C:\torrents\finished\example.torrent.dir\"
using System;
using CommandLine.Text;
using CommandLine;
using System.IO;
namespace uTorrentController
{
class Program
{
private static readonly string torrentPath = @"E:\Torrents\";
private static readonly HeadingInfo headingInfo = new HeadingInfo("µTorrent Controller", "1.0a");
private sealed class Options : CommandLineOptionsBase
{
#region "Standard Option Attributes"
[Option("l", "label",
Required = true,
HelpText = "Torrent's label. This will determine its destination.")]
public string Label = String.Empty;
[Option("d", "directory",
Required = true,
HelpText = "Torrent's source directory.")]
public string SourceDir = String.Empty;
#endregion
#region "Special Option Attributes"
[HelpOption(HelpText = "Display this help screen.")]
public string GetUsage()
{
var help = new HelpText(Program.headingInfo);
help.AdditionalNewLineAfterOption = true;
help.Copyright = new CopyrightInfo("Bengt Ericsson", 2011);
this.HandleParsingErrorsInHelp(help);
help.AddPreOptionsLine("This is free software. You may redistribute copies of it under the terms of");
help.AddPreOptionsLine("the MIT License <http://www.opensource.org/licenses/mit-license.php>.");
help.AddPreOptionsLine(@"Usage: uTorrentController -l ""Apps"" -d ""C:\torrents\finished\example.torrent.dir\""");
help.AddOptions(this);
return help;
}
private void HandleParsingErrorsInHelp(HelpText help)
{
string errors = help.RenderParsingErrorsText(this);
if (!string.IsNullOrEmpty(errors))
help.AddPreOptionsLine(string.Concat(Environment.NewLine, "ERROR: ", errors, Environment.NewLine));
}
#endregion
}
static void Main(string[] args)
{
var options = new Options();
ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
if (!parser.ParseArguments(args, options))
Environment.Exit(1);
DoCoreTask(options);
Environment.Exit(0);
}
private static void DoCoreTask(Program.Options options)
{
using (var log = new StreamWriter(@"log.txt", true))
{
log.AutoFlush = true;
Console.WriteLine(options.Label);
Console.WriteLine(options.SourceDir);
options.Label = options.Label.Replace("/", @"\");
log.WriteLine();
log.WriteLine();
log.WriteLine(DateTime.Now);
log.WriteLine(options.Label);
log.WriteLine(options.SourceDir);
var destination = Directory.CreateDirectory(torrentPath);
if (destination.Exists)
{
destination = Directory.CreateDirectory(Path.Combine(torrentPath, options.Label));
if (!destination.Exists)
{
log.WriteLine("Directory {0} didn't exist, so we created it.", destination.FullName);
destination.Create();
}
}
else
{
log.WriteLine("ERROR: 2; Couldn't find target destination.");
Environment.Exit(2);
}
var srcDir = Directory.CreateDirectory(options.SourceDir);
if (srcDir.Exists)
{
try
{
log.WriteLine("Moving file: {0} to {1}", srcDir.FullName, Path.GetFileName(srcDir.FullName));
srcDir.MoveTo(Path.Combine(destination.FullName, Path.GetFileName(srcDir.FullName)));
log.WriteLine("Finished file: {0}", srcDir.FullName);
}
catch (Exception e)
{
log.WriteLine("ERROR 7; Something went wrong when moving file: {0}\t{1}", Environment.NewLine, e.ToString().Replace("\n", "\n\t"));
}
}
else
{
log.WriteLine("ERROR: 3; Couldn't find source destination.");
Environment.Exit(3);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment