Click here to Skip to main content
Click here to Skip to main content

File Renamer Lite v1.2

, 20 Oct 2010
Rate this:
Please Sign up or sign in to vote.
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:

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

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

    return values;
}
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.

 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;
}
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)

About the Author

Steppenwolfe
Network Administrator
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralSuggested future additions Pinmemberflector11-Oct-10 11:09 
GeneralRe: Suggested future additions Pinmemberneil.hall11-Oct-10 12:25 
GeneralRe: Suggested future additions PinmemberSteppenwolfe11-Oct-10 12:32 
GeneralRe: Suggested future additions PinmemberSteppenwolfe11-Oct-10 12:32 
it does replace strings, upper/lower case is simple, also delete at pos.. during the week probably

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 20 Oct 2010
Article Copyright 2010 by Steppenwolfe
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid