-
November 3rd, 2009, 08:05 AM
#1
Beautifying Output From CLI Application
I am working on a program that will be called like this:
AfterDownload "C:\Episodes\Family Guy\Family.Guy.S01E05.Episode.Name-GRP\"
The program will go to the directory, un-rar the first .rar file it finds (using WinRAR's command-line app, rar.exe), pass it through SmartRename (which queries an online database and renames the file to the syntax I want), moves it up one directory (in this case, to "Family Guy"), and optionally deleting the working folder. I have it working as I want, but now I would like to beautify it.
Here is a screenshot of the output:
As you can see, the [RAR] stuff looks very messy. Ideally, I would like to pull just the percentage output from the rar.exe application. How would I go about looking for it, and displaying just that (in real-time, of course)?
Here is the code I have come up with so far:
Code:
//#define USEDEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace AfterDownload
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("AfterDownload v1.0");
Console.WriteLine("The syntax is: AfterDownload.exe [Path to directory]");
}
else
{
// clear the screen
Console.Clear();
DirectoryInfo dir = new DirectoryInfo(args[0]);
FileInfo[] rarFiles = dir.GetFiles("*.rar");
if (rarFiles.Length != 1)
{
Console.WriteLine("AfterDownload v1.0");
Console.WriteLine("This download doesnt contain a .rar file to extract,");
Console.WriteLine(" or contains more than one!");
Console.WriteLine("Exiting...");
}
else
{
// process info
Process rar = new Process();
rar.StartInfo.FileName = @"C:\Program Files\WinRAR\rar.exe";
rar.StartInfo.UseShellExecute = false;
rar.StartInfo.RedirectStandardOutput = true;
rar.StartInfo.RedirectStandardError = true;
rar.StartInfo.CreateNoWindow = true;
rar.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
// debug text - display args, directory, etc
#if USEDEBUG
Console.WriteLine("Console opened with args: " + args[0]);
Console.WriteLine();
Console.WriteLine("Working directory is \"" + args[0] + "\"");
Console.WriteLine();
Console.WriteLine("File name is:");
Console.WriteLine(rarFiles[0]);
#endif
// extract .rar to current directory
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[iNFO] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("AfterDownload v1.0");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[iNFO] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Extracting:");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" --> " + dir.ToString() + "\\" + rarFiles[0]);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(" to:");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" --> " + dir.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
rar.StartInfo.Arguments = "e -o+ \"" + args[0] + "\\" + rarFiles[0] + "\" \"" + dir.ToString() + "\\\"";
#if USEDEBUG
Console.WriteLine("Args: " + rarArgs);
#endif
// make -----'s to fill the screen width
for (int a = 0; a < Console.WindowWidth; a++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("-");
Console.ForegroundColor = ConsoleColor.Gray;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[RAR] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Starting rar.exe with the arguments:");
Console.WriteLine(" \"" + rar.StartInfo.Arguments + "\"");
// do extracting now!
rar.Start();
rar.BeginOutputReadLine();
rar.WaitForExit();
rar.Close();
rar.CancelOutputRead();
// make -----'s to fill the screen width
for (int a = 0; a < Console.WindowWidth; a++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("-");
Console.ForegroundColor = ConsoleColor.Gray;
}
// run smartrename on the avi file
FileInfo[] aviFiles = dir.GetFiles("*.avi");
Process sr = new Process();
sr.StartInfo.FileName = @"C:\Program Files\SmartRename\SmartRename.exe";
sr.StartInfo.Arguments = "\"" + dir + "\\" + aviFiles[0] + "\"";
sr.StartInfo.UseShellExecute = false;
sr.StartInfo.CreateNoWindow = true;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[SmartRename] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Renaming " + sr.StartInfo.Arguments);
sr.Start();
System.Threading.Thread.Sleep(500);
Console.Write(" .");
System.Threading.Thread.Sleep(500);
Console.Write(".");
System.Threading.Thread.Sleep(500);
Console.WriteLine(".");
// wait for original file to disappear and signify smartrename renamed the file
while (File.Exists(dir + "\\" + aviFiles[0].ToString()))
System.Threading.Thread.Sleep(1); // something useless
sr.Kill();
sr.Close();
// "renamed from ... to ..."
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[SmartRename] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Renaming done!");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" --> " + aviFiles[0]);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(" renamed to: ");
Console.ForegroundColor = ConsoleColor.Green;
aviFiles = dir.GetFiles("*.avi");
Console.WriteLine(" --> " + aviFiles[0]);
Console.ForegroundColor = ConsoleColor.Gray;
// make -----'s to fill the screen width
for (int a = 0; a < Console.WindowWidth; a++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("-");
Console.ForegroundColor = ConsoleColor.Gray;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[Move] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Moving \"" + aviFiles[0] + "\" up one level");
File.Move(dir.ToString() + "\\" + aviFiles[0].ToString(), dir.Root.ToString() + dir.Parent.ToString() + "\\" + aviFiles[0]);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[Move] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Moving Done!");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" --> " + dir.ToString() + "\\" + aviFiles[0]);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(" moved to:");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" --> " + dir.Root + dir.Parent + "\\" + aviFiles[0]);
Console.ForegroundColor = ConsoleColor.Gray;
// make -----'s to fill the screen width
for (int a = 0; a < Console.WindowWidth; a++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("-");
Console.ForegroundColor = ConsoleColor.Gray;
}
/*
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[Delete] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Deleting \"" + dir.ToString() + "\"");
// delete directory
Directory.Delete(dir.ToString(), true);
System.Threading.Thread.Sleep(500);
Console.Write(" .");
System.Threading.Thread.Sleep(500);
Console.Write(".");
System.Threading.Thread.Sleep(500);
Console.WriteLine(".");
// wait for folder to disappear so we can move on
while (Directory.Exists(dir.ToString()))
System.Threading.Thread.Sleep(1); // something useless
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[Delete] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\"" + dir.ToString() + "\" deleted!");
// make -----'s to fill the screen width
for (int a = 0; a < Console.WindowWidth; a++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("-");
Console.ForegroundColor = ConsoleColor.Gray;
}
*/
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[iNFO] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("All done!");
Console.ReadKey();
}
}
}
static void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[RAR] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(e.Data);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|