Skip to content

Commit

Permalink
v1.7.0: /fr option to use RegEx for file matches
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Feb 2, 2022
1 parent c16d25e commit e843a63
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012 Nic Jansma, http://nicj.net
Copyright (c) 2022 Nic Jansma, http://nicj.net

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2021 Nic Jansma
Copyright (c) 2022 Nic Jansma
[http://nicj.net](http://nicj.net)

# Introduction
Expand All @@ -18,6 +18,7 @@ file renamer or with a complex regular expression for matching and replacement.
/files: include only files
/dirs: include only directories
(default is to include files only, to include both use /files /dirs)
/fr: use regex for file name matching instead of Windows glob matching
```

You can use [.NET regular expressions](http://msdn.microsoft.com/en-us/library/hs600312.aspx) for the search and
Expand All @@ -42,14 +43,18 @@ Rename files in the pattern of "`124_xyz.txt`" to "`xyz_123.txt`":

RR.exe *.txt "([0-9]+)_([a-z]+)" "$2_$1"

Rename directories (only)
Rename directories (only):

RR * "-" "_" /dirs

Rename files and directories
Rename files and directories:

RR * "-" "_" /files /dirs

Apply a regular expression to the glob pattern files and directories:

RR a_\d.txt "a_" "a_0" /fr

# Version History

* v1.0 - 2012-01-30: Initial release
Expand All @@ -60,6 +65,7 @@ Rename files and directories
* v1.5 - 2020-07-02: Added support for directories, added length-check (via Alec S. @Synetech)
* v1.6 - 2021-05-22: Added `/c` support for case insensitivity (via Alec S. @Synetech)
* v1.6.1 - 2021-06-12: Fix `/r` for sub-dirs
* v1.7- 2022-02-01: Added `/fr` option to apply a regex to file matches (instead of Windows glob pattern)

# Credits

Expand Down
Binary file modified bin/RR.exe
Binary file not shown.
78 changes: 67 additions & 11 deletions source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static int Main(string[] args)
bool force;
bool preserveExt;
int includeMask;
bool fileMatchRegEx;

if (!GetArguments(
args,
Expand All @@ -60,7 +61,8 @@ public static int Main(string[] args)
out caseInsensitive,
out force,
out preserveExt,
out includeMask))
out includeMask,
out fileMatchRegEx))
{
Usage();

Expand All @@ -75,9 +77,14 @@ public static int Main(string[] args)
{
string[] files = Directory.GetFiles(
Environment.CurrentDirectory,
fileMatch,
fileMatchRegEx ? "*" : fileMatch,
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

if (fileMatchRegEx)
{
files = ApplyFileRegex(files, fileMatch);
}

allItems.AddRange(files);
}

Expand All @@ -86,9 +93,14 @@ public static int Main(string[] args)
{
string[] dirs = Directory.GetDirectories(
Environment.CurrentDirectory,
fileMatch,
fileMatchRegEx ? "*" : fileMatch,
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

if (fileMatchRegEx)
{
dirs = ApplyFileRegex(dirs, fileMatch);
}

allItems.AddRange(dirs);
}

Expand Down Expand Up @@ -193,8 +205,31 @@ public static int Main(string[] args)
}

return 0;
}

}

/// <summary>
/// Matches a list of files/directories to a regular expression
/// </summary>
/// <param name="list">List of files or directories</param>
/// <param name="fileMatch">Regular expression to match</param>
/// <returns>List of files or directories that matched</returns>
private static string[] ApplyFileRegex(string[] list, string fileMatch)
{
List<string> matching = new List<string>();

Regex regex = new Regex(fileMatch);

for (int i = 0; i < list.Length; i++)
{
if (regex.IsMatch(list[i]))
{
matching.Add(list[i]);
}
}

return matching.ToArray();
}

/// <summary>
/// Gets the program arguments
/// </summary>
Expand All @@ -207,6 +242,7 @@ public static int Main(string[] args)
/// <param name="force">Whether or not to force overwrites</param>
/// <param name="preserveExt">Whether or not to preserve file extensions</param>
/// <param name="includeMask">Whether to include directories, files or both</param>
/// <param name="fileMatchRegEx">Whether to use a RegEx for the file match. If false, Windows glob patterns are used.</param>
/// <returns>True if argument parsing was successful</returns>
private static bool GetArguments(
string[] args,
Expand All @@ -218,7 +254,8 @@ private static bool GetArguments(
out bool caseInsensitive,
out bool force,
out bool preserveExt,
out int includeMask)
out int includeMask,
out bool fileMatchRegEx)
{
// defaults
fileMatch = String.Empty;
Expand All @@ -227,12 +264,13 @@ private static bool GetArguments(

bool foundNameReplace = false;

pretend = false;
recursive = false;
force = false;
pretend = false;
recursive = false;
force = false;
caseInsensitive = false;
preserveExt = false;
includeMask = 0;
preserveExt = false;
includeMask = 0;
fileMatchRegEx = false;

// check for all arguments
if (args == null || args.Length < 3)
Expand Down Expand Up @@ -279,6 +317,10 @@ private static bool GetArguments(
{
includeMask |= IncludeDirs;
}
else if (args[i].Equals("/fr", StringComparison.OrdinalIgnoreCase))
{
fileMatchRegEx = true;
}
else
{
// if not an option, the rest of the arguments are filename, search, replace
Expand All @@ -298,6 +340,19 @@ private static bool GetArguments(
}
}

if (fileMatchRegEx)
{
try
{
Regex regex = new Regex(fileMatch);
}
catch (ArgumentException e)
{
Console.WriteLine("ERROR: File match is not a regular expression!\n");
return false;
}
}

return !String.IsNullOrEmpty(fileMatch)
&& !String.IsNullOrEmpty(nameSearch)
&& foundNameReplace;
Expand Down Expand Up @@ -325,6 +380,7 @@ private static void Usage()
Console.WriteLine(@" /files: include files (default)");
Console.WriteLine(@" /dirs: include directories");
Console.WriteLine(@" (default is to include files only, to include both use /files /dirs)");
Console.WriteLine(@" /fr: use regex for file name matching instead of Windows glob matching");
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 2021")]
[assembly: AssemblyCopyright("Copyright © Nic Jansma 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

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

[assembly: CLSCompliant(true)]

0 comments on commit e843a63

Please sign in to comment.