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()
{
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
string
s in various sequences - Change preview
- Word case control
- Remove existing number sequences
- Ordered rule based processing
- Numbering with prefix/suffix and formatting options