Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / C#

File Renamer Lite v1.2

Rate me:
Please Sign up or sign in to vote.
4.80/5 (7 votes)
20 Oct 2010CPOL2 min read 45.8K   1.4K   30   22
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)


Written By
Network Administrator vtdev.com
Canada Canada
Network and programming specialist. Started in C, and have learned about 14 languages since then. Cisco programmer, and lately writing a lot of C# and WPF code, (learning Java too). If I can dream it up, I can probably put it to code. My software company, (VTDev), is on the verge of releasing a couple of very cool things.. keep you posted.

Comments and Discussions

 
QuestionDelete characters at position Pin
Member 1397660923-Mar-19 12:16
Member 1397660923-Mar-19 12:16 
QuestionGreat tool but ask how to do specific job Pin
Asholino9-Jan-19 5:46
Asholino9-Jan-19 5:46 
GeneralMy vote of 5 Pin
majid torfi2-Nov-14 3:00
professionalmajid torfi2-Nov-14 3:00 
GeneralGreat Tool Pin
dpminusa18-Oct-10 11:18
dpminusa18-Oct-10 11:18 
GeneralRe: Great Tool Pin
John Underhill18-Oct-10 11:49
John Underhill18-Oct-10 11:49 
GeneralRe: Great Tool Pin
dpminusa18-Oct-10 13:10
dpminusa18-Oct-10 13:10 
GeneralRe: Great Tool Pin
John Underhill19-Oct-10 12:01
John Underhill19-Oct-10 12:01 
GeneralRe: Great Tool Pin
dpminusa19-Oct-10 12:55
dpminusa19-Oct-10 12:55 
GeneralRe: Great Tool Pin
John Underhill20-Oct-10 6:00
John Underhill20-Oct-10 6:00 
GeneralRe: Great Tool Pin
dpminusa20-Oct-10 7:41
dpminusa20-Oct-10 7:41 
GeneralGreat Project Pin
Anthony Daly17-Oct-10 1:51
Anthony Daly17-Oct-10 1:51 
GeneralRe: Great Project Pin
John Underhill17-Oct-10 6:29
John Underhill17-Oct-10 6:29 
GeneralRe: Great Project Pin
shakil030400319-Oct-10 5:42
shakil030400319-Oct-10 5:42 
GeneralRe: Great Project Pin
John Underhill19-Oct-10 12:02
John Underhill19-Oct-10 12:02 
GeneralDownload doesn't work Pin
AxelM11-Oct-10 21:26
AxelM11-Oct-10 21:26 
GeneralRe: Download doesn't work Pin
yroman11-Oct-10 23:48
yroman11-Oct-10 23:48 
GeneralRe: Download doesn't work Pin
AxelM12-Oct-10 0:42
AxelM12-Oct-10 0:42 
GeneralRe: Download doesn't work Pin
John Underhill12-Oct-10 1:53
John Underhill12-Oct-10 1:53 
GeneralSuggested future additions Pin
flector11-Oct-10 11:09
flector11-Oct-10 11:09 
GeneralRe: Suggested future additions Pin
NeverJustHere11-Oct-10 12:25
NeverJustHere11-Oct-10 12:25 
GeneralRe: Suggested future additions Pin
John Underhill11-Oct-10 12:32
John Underhill11-Oct-10 12:32 
GeneralRe: Suggested future additions Pin
John Underhill11-Oct-10 12:32
John Underhill11-Oct-10 12:32 

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

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