Clean up application

I'm assuming these exist already but I wrote my own. It cleans out the bin, obj, SVN directories so you don't have to. It also removes the resharper directory since no one wants that. If you don't have resharper, i suggest getting it.

XML Code

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="folderPatterns" value=".svn|_Resharper*|bin|obj"/>
    <add key="filePatterns" value="*.resharper|*.resharper.user|*.suo"/>
  </appSettings>
</configuration>

Application

private void button1_Click(object sender, EventArgs e)
{
    if (folderBrowser.ShowDialog() == DialogResult.OK &&
        folderBrowserBackup.ShowDialog() == DialogResult.OK)
    {
        DirectoryInfo di = new DirectoryInfo(folderBrowser.SelectedPath);
        DirectoryInfo backupDir = new DirectoryInfo(
            Path.Combine(folderBrowserBackup.SelectedPath, di.Name));

        textBox1.Text = string.Empty;
        CopyAll(di, backupDir,
            RegexFilePattern(ConfigurationManager.AppSettings[
                "filePatterns"]),
            RegexFilePattern(ConfigurationManager.AppSettings[
                "folderPatterns"]));
    }
}

public static string RegexFilePattern(string input)
{
    return Regex.Replace(input, "\*", "[\w\W]*");
}

public delegate void CopyAllMethod(object state);
public void CopyAll(DirectoryInfo source, DirectoryInfo target,
    string deleteFilePatterns, string deleteFolderPattern)
{
    if (InvokeRequired)
    {
        CopyAllMethod method = delegate {
            CopyAll(source, target, deleteFilePatterns, deleteFolderPattern); };
        BeginInvoke(method, null);
    }
    else
    {
        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        // Copy each file into it's new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            Console.WriteLine(@"Copying {0}{1}", target.FullName, fi.Name);
            if (!Regex.IsMatch(
                fi.Name, deleteFilePatterns, RegexOptions.IgnoreCase))
            {
                fi.CopyTo(
                    Path.Combine(target.ToString(), fi.Name), true);
            }
            else
            {
                textBox1.Text += fi.Name + Environment.NewLine;
            }
        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            if (!Regex.IsMatch(diSourceSubDir.Name, deleteFolderPattern,
                RegexOptions.IgnoreCase))
            {
                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir,
                    deleteFilePatterns, deleteFolderPattern);
            }
            else
            {
                textBox1.Text += diSourceSubDir.Name +
                    Environment.NewLine;
            }
        }
    }
}

Ian May 21, 2007 @ 12:05 PM

# 
I can't wait to see pictures!! Did you mean Jimmy Kimmel?

Cell May 24, 2007 @ 3:05 AM

# 
Of course it was a hard 3 days, but it was also substantial 3 days. I expect your production.
Good luck.

Jonathan Moore May 21, 2007 @ 8:05 PM

# 
Hay I built almost exactly the same machine but without the PC:
http://0x0000.org/2007/02/external_combustion_engine_rob_1.html

Good luck on your project.

Post a Comment

Please add 5 and 6 and type the answer here: