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

File Renamer Lite v1.2

By , 20 Oct 2010
 
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGreat Toolmemberdpminusa18-Oct-10 11:18 
GeneralRe: Great ToolmemberSteppenwolfe18-Oct-10 11:49 
GeneralRe: Great Toolmemberdpminusa18-Oct-10 13:10 
GeneralRe: Great ToolmemberSteppenwolfe19-Oct-10 12:01 
GeneralRe: Great Toolmemberdpminusa19-Oct-10 12:55 
GeneralRe: Great ToolmemberSteppenwolfe20-Oct-10 6:00 
GeneralRe: Great Toolmemberdpminusa20-Oct-10 7:41 
GeneralGreat ProjectmemberAnt210017-Oct-10 1:51 
GeneralRe: Great ProjectmemberSteppenwolfe17-Oct-10 6:29 
GeneralRe: Great Projectmembershakil030400319-Oct-10 5:42 
GeneralRe: Great ProjectmemberSteppenwolfe19-Oct-10 12:02 
GeneralDownload doesn't workmemberAxelM11-Oct-10 21:26 
GeneralRe: Download doesn't workmemberyroman11-Oct-10 23:48 
GeneralRe: Download doesn't workmemberAxelM12-Oct-10 0:42 
GeneralRe: Download doesn't workmemberSteppenwolfe12-Oct-10 1:53 
GeneralSuggested future additionsmemberflector11-Oct-10 11:09 
GeneralRe: Suggested future additionsmemberneil.hall11-Oct-10 12:25 
GeneralRe: Suggested future additionsmemberSteppenwolfe11-Oct-10 12:32 
GeneralRe: Suggested future additionsmemberSteppenwolfe11-Oct-10 12:32 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 20 Oct 2010
Article Copyright 2010 by Steppenwolfe
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid