CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Sep 2009
    Posts
    23

    Program To UnRAR and Sort Files

    Heya, Im working on a program to unrar and sort some files. Here is the program flow:

    - scan a folder for .rar files
    - unrar the .rar file to the directory's parent folder (ie. if the structure is "dir1\dir2\file.rar", extract "file.rar" to "dir1\")
    - open SmartRename (a file renaming program) with the extracted file to auto-rename it
    - delete "dir2"

    Here is the code I have so far (its not complete, and doesnt do everything I want yet, but its a start):
    Code:
    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
        {
            public static int j;
    
            static void Main(string[] args)
            {
                DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
                FileInfo[] rarFiles = dir.GetFiles("*.rar");
    
                // 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);
    
                // beautify things
                Console.WriteLine("Current directory is \"" + dir.ToString() + "\"");
                Console.WriteLine();
                Console.WriteLine("Total number of .rar files in this directory is " + rarFiles.Length);
                Console.WriteLine("File names are:");
                Console.WriteLine();
    
                int i = 1;
    
                // for each rar file in the list, extract it
                foreach (FileInfo f in rarFiles)
                {
                    Console.WriteLine(i + ". " + f);
                    Console.WriteLine("[iNFO] Extracting " + f + " to \\" + dir.Parent);
                    Console.WriteLine();
                    j = 0;
                    rar.StartInfo.Arguments = "e -o+ " + f + @" ..\";
                    rar.Start();
                    rar.BeginOutputReadLine();
                    rar.WaitForExit();
                    rar.CancelOutputRead();
                    Console.WriteLine();
    
                    for (int a = 0; a < Console.WindowWidth; a++)
                    {
                        Console.Write("-");
                    }
    
                    Console.WriteLine();
                    i++;
                }
                Console.ReadKey();
            }
    
            static void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                // get rid of shareware notice and clean up output a bit
                if (j > 4 && j < 9)
                    Console.WriteLine("[RAR] " + e.Data);
                j++;
            }
        }
    }
    Here is the problem I am having:


    Where Ive circled at the bottom, there should be the stdout info from rar.exe, as there is for file #1, but there isnt. For some reason, I only get stdout info from the first file, no matter how many files I have it extract. What part have I missed?

    EDIT Turns out I needed to move the new Process() and StartInfo stuff into the foreach block.
    Last edited by SpikedCola; November 2nd, 2009 at 04:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured