Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

File Renamer Lite v1.2

4.80/5 (7 votes)
20 Oct 2010CPOL2 min read 47.1K   1.4K  
A simplified file renamer
screen.jpg

It Is What It Is, Is What It Is..

A pearl of wisdom from the sailor man... So... the busy season, (for me that is), is near over, and I find myself tending to the various distractions of a personal life once again, (like writing these inane articles ;o) One of these favored distractions is collecting videos, TV series and the like. Now free vids are a wonder of the internet -no complaints there, but I constantly find myself needing to manually rename every file to fit a naming scheme I adopted along the way... and this was just what I was doing yesterday when I got annoyed enough to write this little renaming progy.

I have -in the past-, been guilty of kitchen sink programming, that is; throwing every conceivable option at an app, until the interface is so cluttered and complex that only an engineer could decipher it... and if you want to build on this, add regexp handling, wire it up to LINQ, custom queries and such, go for it... but I think all that stuff puts it beyond the pale for the average eu. So with a simple but flexible interface, and at a glance functionality as the template, this is what I came up with.

A Couple of Snippets

Getting the file extensions into a combobox might come in handy for y'all:

C#
 private Array EnumKeys(RegistryKey root, string subkey)
{
    RegistryKey key = root.OpenSubKey(subkey);
    string[] keys = key.GetSubKeyNames();
    key.Close();

    return keys;
}
C#
private Array EnumValues(RegistryKey root, string subkey)
{
    RegistryKey key = root.OpenSubKey(subkey);
    string[] values = key.GetValueNames();
    key.Close();

    return values;
}
C#
private ArrayList GetFileTypes()
{
    // get supported media types, missing from hkcr
    // Applications\wmplayer.exe\SupportedTypes
    Array media = EnumValues(Registry.ClassesRoot, MEDIATYPES);
    Array list = EnumKeys(Registry.ClassesRoot, "");
    ArrayList res = new ArrayList();

    foreach (string s in media)
    {
        if (s.StartsWith("."))
        {
            res.Add(s);
        }
    }

    foreach (string s in list)
    {
        if (s.StartsWith("."))
        {
            if (!res.Contains(s))
            {
                res.Add(s);
            }
        }
    }
    return res;
}

This bit is how the file names are collected, without the need for recursion.

C#
 private ArrayList GetFiles(string dir)
{
    ArrayList list = new ArrayList();
    string pattern = "*" + cbFileType.Text;
    SearchOption option = chkSubfolder.Checked ? 
	SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

    if (Directory.Exists(dir))
    {
        if (chkSubfolder.Checked)
        {
            list = GetFiles(dir, pattern);
        }
        else
        {
            try
            {
                string[] files = Directory.GetFiles
		(dir, pattern, SearchOption.TopDirectoryOnly);
                foreach (string s in files)
                {
                    list.Add(s);
                }
            }
            catch { }
        }
    }
    return list;
}
C#
private ArrayList GetFiles(string root, string pattern)
{
    Stack<string> dirs = new Stack<string>(20);
    ArrayList list = new ArrayList();
    string[] subDirs = new string[0];
    string[] files = null;

    dirs.Push(root);

    while (dirs.Count > 0)
    {
        string currentDir = dirs.Pop();

        try
        {
            subDirs = Directory.GetDirectories(currentDir);
        }
        catch (UnauthorizedAccessException e) { continue; }
        catch (DirectoryNotFoundException e) { continue; }
        catch { }

        try
        {
            files = Directory.GetFiles(currentDir, pattern);
        }
        catch (UnauthorizedAccessException e) { continue; }
        catch (DirectoryNotFoundException e) { continue; }
        catch { }

        foreach (string file in files)
        {
            list.Add(file);
        }

        foreach (string str in subDirs)
        {
            dirs.Push(str);
        }
    }
    return list;
}

Now you may wonder why I didn't just get all the files with one call to Directory.GetFiles, the problem with that approach is that if a file is removed by another app while processing, or if access is restricted, the call will fail with an error.

Features

  • Undo changes
  • Add/Remove or replace strings in various sequences
  • Change preview
  • Word case control
  • Remove existing number sequences
  • Ordered rule based processing
  • Numbering with prefix/suffix and formatting options

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)