Skip to content

Commit

Permalink
New features: guids, lowercase. append #2 #3 (#7)
Browse files Browse the repository at this point in the history
* #5 Rename happens when file names are different, files are counted when renamed

* do not display title logo again in help

* Renaming defaults to lowercase and has flag keep-case #2

* append flag, guid filter
  • Loading branch information
rsadwick authored Jul 16, 2022
1 parent 2b0c5db commit 7f1a20c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ Cleans up file names by renaming within a given path.
- Remove all spaces and parentheses (default)
- Remove all spaces and special characters
- Remove all numbers
- Rename all files as GUIDs
- Prepend a name before the file is renamed.
- Append a name at the end of the filename.

### Usage:

| Command | Description |
|---------|-------------|
| `file-renamer-pro -p {path}` | Sets a given path. If not, you'll be able to input a path |
| `file-renamer-pro -f {0, 1, 2, 3}` | Sets a renaming filter
| `file-renamer-pro -f {0, 1, 2, 3, 4}` | Sets a renaming filter
| `file-renamer-pro -n {name}` | Prepends a given name to the file |
| `file-renamer-pro -a {name}` | Appends a given name to the file |
| `file-renamer-pro --keep-case` | This override flag will keep the case of the names of the files |

#### Basic usage with defaults:

Expand All @@ -38,13 +42,20 @@ Renames all files under myfolder\assets and renames the files by removing all sp
| `file-renamer-pro -f 1` | Removes all spaces and parentheses (default).
| `file-renamer-pro -f 2` | Removes all spaces and special characters
| `file-renamer-pro -f 3` | Removes all numbers |
| `file-renamer-pro -f 4` | Renames all files as GUIDs |

#### Prepend a name:

`file-renamer-pro -f 3 -n hi_ -p c:\myfolder\assets`

Renames all files under myfolder\assets and renames the files by removing all numbers. All filenames are prepended with "hi_". (hi_testImage.png)

#### Append a name:

`file-renamer-pro -f 3 -a _bye -p c:\myfolder\assets`

Renames all files under myfolder\assets and renames the files by removing all numbers. All filenames are appended with "_bye". (testImage_bye.png)

### Debugging in VS:Code

Within launch.json, change internalConsole to integratedTerminal:
Expand Down
33 changes: 25 additions & 8 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum FilterLevel {
SPACES_PARENTHESES = 1,
SPECIAL_CHARACTERS = 2,
NUMBERS = 3,
GUID = 4
};
private static string title = @"
_____.__.__
Expand All @@ -18,7 +19,9 @@ public enum FilterLevel {
\/ \/ \/ \/ \/ \/ |__| ";
private static string path = "";
private static string prepend = "";
private static string append = "";
private static string filter = "1";
private static bool keepCase = false;
private static void Main(string[] args) {

PaintTitleScreen();
Expand All @@ -38,7 +41,7 @@ private static void Rename() {
foreach (var info in files) {
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(info.Name);
var cleanFileName = CleanFileName(fileNameWithoutExtension, filter);
var newFilename = $"{prepend}{cleanFileName}{info.Extension}";
var newFilename = $"{prepend}{cleanFileName}{append}{info.Extension}";
var newFullFilename = Path.Combine(path, newFilename);

if(info.FullName != newFullFilename) {
Expand Down Expand Up @@ -89,20 +92,25 @@ private static string CleanFileName(string filename, string filterLevel) {

switch (filterLevelStatus) {
case FilterLevel.SPACES:
cleanFilename = filename.Replace(" ", "");
cleanFilename = (keepCase) ? filename.Replace(" ", "") : filename.Replace(" ", "").ToLowerInvariant();
break;

case FilterLevel.SPECIAL_CHARACTERS:
Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
cleanFilename = r.Replace(filename, String.Empty).Replace(" ", "");
cleanFilename = (keepCase) ? r.Replace(filename, String.Empty).Replace(" ", "") : r.Replace(filename, String.Empty).Replace(" ", "").ToLowerInvariant();
break;

case FilterLevel.NUMBERS:
cleanFilename = Regex.Replace(filename, @"[\d-]", string.Empty);
break;

case FilterLevel.GUID:
cleanFilename = Guid.NewGuid().ToString();
break;

case FilterLevel.SPACES_PARENTHESES:
default:
cleanFilename = filename.Replace(" ", "").Replace("(", "").Replace(")", "");
cleanFilename = (keepCase) ? filename.Replace(" ", "").Replace("(", "").Replace(")", "") : filename.Replace(" ", "").Replace("(", "").Replace(")", "").ToLowerInvariant();
break;
}

Expand All @@ -125,15 +133,24 @@ private static void ArgumentHandler(string[] args) {
prepend = args[i + 1];
}

if (args[i] == "-a") {
append = args[i + 1];
}

if (args[i] == "-f") {
filter = args[i + 1];
}

if(args[i] == "--keep-case") {
keepCase = true;
}

if (args[i] == "-h" || args[i] == "-help") {
Console.WriteLine(title);
Console.WriteLine("-p : path to files to be renamed.");
Console.WriteLine("-f : amount of filtering used in renaming files: 0 = removes only spaces, 1 = removes spaces and parentheses (DEFAULT), 2 = removes spaces and all special characters, 3 = removes all numbers.");
Console.WriteLine("-n : name to prepend to filename. eg. '-n hi_ for hi_filename.txt.");
Console.WriteLine("-p : path to files to be renamed.");
Console.WriteLine("-f : amount of filtering used in renaming files: 0 = removes only spaces, 1 = removes spaces and parentheses (DEFAULT), 2 = removes spaces and all special characters, 3 = removes all numbers.");
Console.WriteLine("-n : name to prepend to filename. eg. '-n hi_ for hi_filename.txt.");
Console.WriteLine("-a : name to append to filename. eg. '-a _bye for filename_bye.txt.");
Console.WriteLine("--keep-case : this flag will keep the case of the name of the file.");
return;
}
}
Expand Down

0 comments on commit 7f1a20c

Please sign in to comment.