Introduction
This is a simple console program that batch renames files in a directory. There are some command line switches:
//
// --dir=c:/images/
// Operate on specified directory
//
// --prepend=text
// Rename file by prepending specified
// text. If not specified,
// the app will prepend a random number
// from 0 to 100000.
// This is typically sufficient;
// if there is a collision it will
// log a warning.
//
// --pattern=*.jpg
// Only operate on files matching specified pattern
Code Listing
using System;
using System.Collections;
using System.IO;
namespace Renamer
{
public class RenameArgKeys
{
protected RenameArgKeys() { }
public const string dir = "--dir";
public const string prepend = "--prepend";
public const string pattern = "--pattern";
}
public class Rename
{
protected string[] _args = null;
public Rename(string[] args)
{
try
{
_args = args;
ParseArgs();
}
catch(Exception e)
{
Diag.LogException(e);
}
}
protected Hashtable _hash = null;
protected Hashtable Hash
{
get
{
if (_hash == null)
{
_hash = new Hashtable();
}
return _hash;
}
}
protected void ParseArgs()
{
try
{
if ((_args == null) || (_args.Length < 1)) return;
foreach (string arg in _args)
{
char[] sep = { '=' };
string[] keyval = arg.Split(sep, 2);
if ((keyval == null) || (keyval.Length < 1)) continue;
string key = keyval[0];
string val = "";
if (keyval.Length >= 2) val = keyval[1];
if (Hash.ContainsKey(key))
{
Hash.Remove(key);
}
Hash.Add(key, val);
}
}
catch(Exception e)
{
Diag.LogException(e);
}
}
protected string SearchPattern
{
get
{
if (!(Hash.Contains(RenameArgKeys.pattern)))
return "*.*";
return Hash[RenameArgKeys.pattern] as string;
}
}
protected string Dir
{
get
{
if (!(Hash.Contains(RenameArgKeys.dir)))
return Directory.GetCurrentDirectory();
return Hash[RenameArgKeys.dir] as string;
}
}
protected Random _rand = null;
protected Random Random
{
get
{
if (_rand == null)
{
_rand = new Random();
}
return _rand;
}
}
protected const int _max_rand = 100000;
protected string DefaultPrepend
{
get
{
string num = Random.Next(_max_rand).ToString();
string built = num + "_";
return built;
}
}
protected string Prepend
{
get
{
if (!(Hash.Contains(RenameArgKeys.prepend)))
return DefaultPrepend;
return Hash[RenameArgKeys.prepend] as string;
}
}
protected string GenDestName(string source)
{
try
{
string dir = Path.GetDirectoryName(source);
string source_fname = Path.GetFileName(source);
string targ_fname = Prepend + source_fname;
string target = Path.Combine(dir, targ_fname);
return target;
}
catch(Exception e)
{
Diag.LogException(e);
return null;
}
}
public void RenameIt(string source)
{
try
{
if (!(File.Exists(source))) return;
string dest = GenDestName(source);
if (File.Exists(dest))
{
Console.WriteLine("Cannot rename " + source + " to " +
dest + ": Destination file exists");
return;
}
File.Move(source, dest);
}
catch(Exception e)
{
Diag.LogException(e);
}
}
public void Go()
{
try
{
string[] files = Directory.GetFiles(Dir, SearchPattern);
if ((files == null) || (files.Length < 1)) return;
foreach(string file in files)
{
RenameIt(file);
}
}
catch(Exception e)
{
Diag.LogException(e);
}
}
}
public class Diag
{
protected Diag() {}
public static void LogException (Exception e)
{
if (e == null) return;
string msg = e.Message;
Console.WriteLine(msg);
}
}
class EntryPoint
{
[STAThread]
static void Main(string[] args)
{
try
{
Rename rename = new Rename(args);
rename.Go();
}
catch(Exception e)
{
Diag.LogException(e);
}
}
}
}
Per Anderson is the founder of Sunfrog Technologies LLC, http://sunfrog-tech.com .