Skip to content

Commit

Permalink
v1.1: Added /r recursive option
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Dec 15, 2012
1 parent 90f2e0b commit 85b12e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ file renamer or with a complex regular expression for matching and replacement.
Usage
------------

RR.exe file-match search replace [/p]
RR.exe file-match search replace [/p] [/r]
/p: pretend (show what will be renamed)
/r: recursive

You can use [.NET regular expressions](http://msdn.microsoft.com/en-us/library/hs600312.aspx) for the search and
replacement strings, including [substitutions](http://msdn.microsoft.com/en-us/library/ewy2t5e0.aspx) (for example,
Expand Down
Binary file added bin/RR.exe
Binary file not shown.
56 changes: 41 additions & 15 deletions source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ public static class Program
/// <returns>0 on success</returns>
public static int Main(string[] args)
{
// get command-line arguments
string nameSearch;
string nameReplace;
string fileMatch;
bool recursive;
bool pretend;
if (!GetArguments(args, out fileMatch, out nameSearch, out nameReplace, out pretend))
if (!GetArguments(args, out fileMatch, out nameSearch, out nameReplace, out pretend, out recursive))
{
Usage();
return 1;
}

// enumerate all files
string[] files = Directory.GetFiles(System.Environment.CurrentDirectory, fileMatch);
string[] files = Directory.GetFiles(
System.Environment.CurrentDirectory,
fileMatch,
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

if (files.Length == 0)
{
Expand All @@ -59,7 +64,12 @@ public static int Main(string[] args)
// write what we changed (or would have)
if (fileName != fileNameAfter)
{
Console.WriteLine(@"{0} -> {1}{2}", fileName, fileNameAfter, pretendModeNotification);
// show the relative file path if not the current directory
string fileNameToShow = (System.Environment.CurrentDirectory == fileDir) ?
fileName :
(fileDir + @"\" + fileName).Replace(System.Environment.CurrentDirectory + @"\", String.Empty);

Console.WriteLine(@"{0} -> {1}{2}", fileNameToShow, fileNameAfter, pretendModeNotification);
}

// move file
Expand Down Expand Up @@ -89,20 +99,22 @@ public static int Main(string[] args)
/// <param name="pretend">Whether or not to only show what would happen</param>
/// <returns>True if argument parsing was successful</returns>
private static bool GetArguments(
string[] args,
out string fileMatch,
out string nameSearch,
out string nameReplace,
out bool pretend)
string[] args,
out string fileMatch,
out string nameSearch,
out string nameReplace,
out bool pretend,
out bool recursive)
{
// defaults
fileMatch = String.Empty;
nameSearch = String.Empty;
nameReplace = String.Empty;
pretend = false;
recursive = false;

// check for all arguments
if (args == null || args.Length < 3 || args.Length > 4)
if (args == null || args.Length < 3)
{
return false;
}
Expand All @@ -113,9 +125,22 @@ public static int Main(string[] args)
nameReplace = args[2];
pretend = false;

if (args.Length == 4 && args[3].Equals("/p", StringComparison.OrdinalIgnoreCase))
//
// Optional arguments:
// /p: pretend (show what will be renamed)
// /r: recursive
//
for (int i = 3; i < args.Length; i++)
{
pretend = true;
if (args[i].Equals("/p", StringComparison.OrdinalIgnoreCase))
{
pretend = true;
}

if (args[i].Equals("/r", StringComparison.OrdinalIgnoreCase))
{
recursive = true;
}
}

return true;
Expand All @@ -127,14 +152,15 @@ public static int Main(string[] args)
private static void Usage()
{
// get the assembly version
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.ProductVersion;
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.ProductVersion;

Console.WriteLine(@"Rename Regex (RR) v{0} by Nic Jansma, http://nicj.net", version);
Console.WriteLine();
Console.WriteLine(@"Usage: RR file-match search replace [/p]");
Console.WriteLine(@"Usage: RR.exe file-match search replace [/p] [/r]");
Console.WriteLine(@" /p: pretend (show what will be renamed)");
Console.WriteLine(@" /r: recursive");
return;
}
}
Expand Down
6 changes: 3 additions & 3 deletions source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RenameRegex")]
[assembly: AssemblyCopyright("Copyright © Nic Jansma 2012")]
[assembly: AssemblyCopyright("Copyright © Nic Jansma 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -35,7 +35,7 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

[assembly:CLSCompliant(true)]

0 comments on commit 85b12e9

Please sign in to comment.