Skip to content

Commit

Permalink
/f option added
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Oct 23, 2013
1 parent 4e6cba5 commit d4296f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ file renamer or with a complex regular expression for matching and replacement.

# Usage

RR.exe file-match search replace [/p] [/r]
RR.exe file-match search replace [/p] [/r] [/f]
/p: pretend (show what will be renamed)
/r: recursive
/f: force overwrite if the file already exists

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 Expand Up @@ -39,6 +40,7 @@ Rename files in the pattern of "````124_xyz.txt````" to "````xyz_123.txt````":
* v1.0 - 2012-01-30: Initial release
* v1.1 - 2012-12-15: Added /r option
* v1.2 - 2013-05-11: Allow /p and /r options before or after main arguments
* v1.3 - 2013-10-23: Added /f option

# Credits

Expand Down
Binary file modified bin/RR.exe
Binary file not shown.
35 changes: 29 additions & 6 deletions source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public static int Main(string[] args)
string fileMatch;
bool recursive;
bool pretend;
if (!GetArguments(args, out fileMatch, out nameSearch, out nameReplace, out pretend, out recursive))
bool force;

if (!GetArguments(args, out fileMatch, out nameSearch, out nameReplace, out pretend, out recursive, out force))
{
Usage();
return 1;
Expand Down Expand Up @@ -61,6 +63,8 @@ public static int Main(string[] args)
// rename via a regex
string fileNameAfter = Regex.Replace(fileName, nameSearch, nameReplace, RegexOptions.IgnoreCase);

bool newFileAlreadyExists = File.Exists(fileNameAfter);

// write what we changed (or would have)
if (fileName != fileNameAfter)
{
Expand All @@ -69,19 +73,30 @@ public static int Main(string[] args)
fileName :
(fileDir + @"\" + fileName).Replace(System.Environment.CurrentDirectory + @"\", String.Empty);

Console.WriteLine(@"{0} -> {1}{2}", fileNameToShow, fileNameAfter, pretendModeNotification);
Console.WriteLine(
@"{0} -> {1}{2}{3}",
fileNameToShow,
fileNameAfter,
pretendModeNotification,
newFileAlreadyExists ? @" (already exists)" : "");
}

// move file
if (!pretend && fileName != fileNameAfter)
{
{
try
{
if (newFileAlreadyExists && force)
{
// remove old file on force overwrite
File.Delete(fileNameAfter);
}

File.Move(fileDir + @"\" + fileName, fileDir + @"\" + fileNameAfter);
}
catch (IOException)
{
Console.WriteLine(@"WARNING: Could note move {0} to {1}", fileName, fileNameAfter);
Console.WriteLine(@"WARNING: Could not move {0} to {1}", fileName, fileNameAfter);
}
}
}
Expand All @@ -98,14 +113,16 @@ public static int Main(string[] args)
/// <param name="nameReplace">Replace expression</param>
/// <param name="pretend">Whether or not to only show what would happen</param>
/// <param name="recursive">Whether or not to recursively look in directories</param>
/// <param name="force">Whether or not to force overwrites</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,
out bool recursive)
out bool recursive,
out bool force)
{
// defaults
fileMatch = String.Empty;
Expand All @@ -116,6 +133,7 @@ public static int Main(string[] args)

pretend = false;
recursive = false;
force = false;

// check for all arguments
if (args == null || args.Length < 3)
Expand All @@ -142,6 +160,10 @@ public static int Main(string[] args)
{
recursive = true;
}
else if (args[i].Equals("/f", StringComparison.OrdinalIgnoreCase))
{
force = true;
}
else
{
// if not an option, the rest of the arguments are filename, search, replace
Expand Down Expand Up @@ -178,9 +200,10 @@ private static void Usage()

Console.WriteLine(@"Rename Regex (RR) v{0} by Nic Jansma, http://nicj.net", version);
Console.WriteLine();
Console.WriteLine(@"Usage: RR.exe file-match search replace [/p] [/r]");
Console.WriteLine(@"Usage: RR.exe file-match search replace [/p] [/r] [/f]");
Console.WriteLine(@" /p: pretend (show what will be renamed)");
Console.WriteLine(@" /r: recursive");
Console.WriteLine(@" /f: force overwrite if the file already exists");
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 @@ -34,8 +34,8 @@
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.2.1.0")]
[assembly: AssemblyFileVersion("1.2.1.0")]
//
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]

[assembly: CLSCompliant(true)]

0 comments on commit d4296f5

Please sign in to comment.