Skip to content

This script will rename one or more files either by adding new text or replacing text in the file names.

License

Notifications You must be signed in to change notification settings

JDHatten/Batch-File-Renamer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 

Repository files navigation

Batch File Renamer by JDHatten

If you ever had a need to reorganize a large amount of files by renaming them this is the script to accomplish that task quickly.

This script will rename one or more files either by adding new text or replacing text in the file names. Adding text can be placed at the start, end, or both sides of either matched text or the entire file name itself. Replacing text will replace the first or all instances of matched text in a file name including the extension. Renaming will just rename the entire file, but an iterating number or some other modify option should be used.

Extra Features:

  • Update any text based files that that have links to the renamed files to prevent broken links in whatever apps that use the those files.
  • Revert any file name changes by dropping the generated log file back into the script.
  • Sort groups of files before renaming using file meta data.
  • Insert file meta data into file names.

How To Use:

  • To Rename Files: Simply drag and drop one or more files or directories onto the script. Create your own custom presets for more complex renaming tasks.
  • To Revert File Renames: Drag and drop one or more of the generated log files back into the script.
  • To Update Links Only: Drag and drop a file fitting the criteria below.

File Name (Exactly): 'x:\same\path\as\this\script\find-replace-links.txt'
File Contents (Example):

find = ['x:\path\to\old\file\link.jpg', ...]
replace = ['x:\path\to\new\file\link-01.jpg', ...]
links = ['x:\path\to\file\with\links.xml', ...]
Note: Use either single or double quotes (stick with one set of quotes) and there is no need to escape characters (double slashes not necessary)


Requirements:

Matching and inserting meta data from files requires the ffmpeg-python and filetype packages. Also in order to read and write a larger variety of linked files, chardet is required to detect file encodings. These are optional features.

pip install ffmpeg-python
pip install filetype
pip install chardet

Presets:

EDIT_TYPE : ADD, REPLACE or RENAME [Required]

ADD simply adds new text to a file name.
REPLACE finds and replace text in a file name.
RENAME renames the entire file name.


MATCH_FILE_NAME : "Text"
-or-
MATCH_FILE_NAME : { TEXT : "Text", OPTIONS : ["Search Option 1",2,3,...] }

TEXT is the text to search for and match before editing a file name. If a match is not made then that file name will not be changed.

OPTIONS are used to further customize search criteria. All current search options are listed below:

MATCH_CASE means searches are case-sensitive. [Default]
NO_MATCH_CASE is used for non-case-sensitive searches.
FULL_MATCH is used when a complete or perfect full match is needed of a file name.
SEARCH_FROM_RIGHT simply starts searching from right to left opposed from the default left to right.
MATCH_LIMIT is used to find and replace a limited number of text matches per file name. The default is ( MATCH_LIMIT, NO_LIMIT ).
SAME_MATCH_INDEX is used in combination with the INSERT_FILE_NAME List, i.e. ["Text", "Text",...]. When a match is made from the MATCH_FILE_NAME List, the same index from the INSERT_FILE_NAME List will be chosen. Useful when making a long lists of specific files to find and rename.
MATCH_ALL_INDEXES is used to match all text in a list, else any match will do. Note: SAME_MATCH_INDEX takes precedent.
REGEX will allow the use of regular expression to search text. Use raw (r) strings, example: r'[R]\s*[E]'
REGEX_GROUP is an option to be used together with REGEX to make sure the matched (groups) are sourced from "this" matched list. [Default] Example Regex: r'(group1)(group2)' --> r'\1\2'. Note: Group text will be taken from the last match made. Use MATCH_LIMIT and/or SEARCH_FROM_RIGHT to select which match to use.
EXTENSION if used in search options will only match the file extension and only an exact match. For example, ‘.doc’ will match ‘.doc’ but will not match ‘.docx’. Only use if you need this exact match use-case.


IGNORE_FILE_NAME : "Text"
-or-
IGNORE_FILE_NAME : { TEXT : "Text", OPTIONS : ["Search Option 1",2,3,...] }

TEXT to search for and if found in a file name skip that file renaming, effectively renaming every other file not matched.

OPTIONS are used to further customize search criteria. All current search options are listed below:

MATCH_CASE means searches are case-sensitive. [Default]
NO_MATCH_CASE is used for non-case-sensitive searches.
FULL_MATCH is used when a complete or perfect full match is needed of a file name.
MATCH_ALL_IGNORE_INDEXES is used to match all text in ignore list in order to skip a rename.
REGEX will allow the use of regular expression to search text. Use raw (r) strings, example: r'[R]\s*[E]'
EXTENSION if used in search options will only match the file extension and only an exact match. For example, ‘.doc’ will match ‘.doc’ but will not match ‘.docx’. Only use if you need this exact match use-case.


MATCH_FILE_CONTENTS : "Text"
-or-
MATCH_FILE_CONTENTS : { TEXT : "Text", OPTIONS : ["Search Option 1",2,3,...] }

TEXT is the text in a file's contents to search for and match before editing a file name. If a match is not made then that file name will not be changed. Note: Only text files can be opened and searched. Other files will be ignored.

OPTIONS are used to further customize search criteria. All current search options are listed below:

MATCH_CASE means searches are case-sensitive. [Default]
NO_MATCH_CASE is used for non-case-sensitive searches.
SEARCH_FROM_RIGHT simply starts searching from right to left opposed from the default left to right.
MATCH_LIMIT is used to find and replace a limited number of text matches per file name. The default is ( MATCH_LIMIT, NO_LIMIT ).
SAME_MATCH_INDEX is used in combination with the INSERT_FILE_NAME List, i.e. ["Text", "Text",...]. When a match is made from the MATCH_FILE_CONTENTS List, the same index from the INSERT_FILE_NAME List will be chosen. Useful when making a long lists of specific files to find and rename.
MATCH_ALL_INDEXES is used to match all text in a list, else any match will do. Note: SAME_MATCH_INDEX takes precedent.
REGEX will allow the use of regular expression to search text. Use raw (r) strings, example: r'[R]\s*[E]'
REGEX_GROUP is an option to be used together with REGEX to make sure the matched groups are sourced from "this" matched list. Example Regex: r'(group1)(group2)' --> r'\1\2'. Note: Group text will be taken from the last match made. Use MATCH_LIMIT and/or SEARCH_FROM_RIGHT to select which match to use.


MATCH_FILE_META : A FILE_META_TYPE or 'Text' to match a FILE_META_MIME
-or-
MATCH_FILE_META : { META : [ { 'Specific Meta' : 'How To Match', 'What To Match' : 'Data', ... }, {}, ... ], OPTIONS : ["Search Option 1",2,3,...] }

META is the file meta data to search for and match before editing a file name. If a match is not made then that file name will not be changed.

TYPE_APPLICATION, TYPE_AUDIO, TYPE_FONT, TYPE_IMAGE, TYPE_MESSAGE, TYPE_MODEL, TYPE_MULTIPART, TYPE_TEXT, TYPE_VIDEO TYPE_ARCHIVE, TYPE_DOCUMENT
FILE_META_SIZE, FILE_META_ACCESSED, FILE_META_MODIFIED, FILE_META_CREATED(Windows Only), FILE_META_METADATA(UNIX), FILE_META_TYPE, FILE_META_MIME, FILE_META_FORMAT, FILE_META_FORMAT_LONG, FILE_META_HEIGHT, FILE_META_WIDTH, FILE_META_LENGTH, FILE_META_BIT_DEPTH, FILE_META_VIDEO_BITRATE, FILE_META_VIDEO_FRAME_RATE, FILE_META_AUDIO_BITRATE, FILE_META_AUDIO_SAMPLE_RATE, FILE_META_AUDIO_CHANNELS, FILE_META_AUDIO_CHANNEL_LAYOUT, FILE_META_AUDIO_TITLE, FILE_META_AUDIO_ALBUM, FILE_META_AUDIO_ARTIST, FILE_META_AUDIO_YEAR, FILE_META_AUDIO_GENRE, FILE_META_AUDIO_PUBLISHER, FILE_META_AUDIO_TRACK
EXACT_MATCH, LOOSE_MATCH, SKIP_EXACT_MATCH, SKIP_LOOSE_MATCH LESS_THAN, MORE_THAN, BEFORE, AFTER, WITHIN_THE_PAST, OLDER_THAN
YEAR, MONTH, DAY, HOUR, MINUTE SECOND, MILLISECOND, MICROSECOND, TIMESTAMP, BYTES, KB, MB, GB, IN_BYTES_ONLY

OPTIONS are used to further customize search criteria. All current search options are listed below:

MATCH_CASE means searches are case-sensitive. [Default]
NO_MATCH_CASE is used for non-case-sensitive searches.
SAME_MATCH_INDEX is used in combination with the INSERT_FILE_NAME List, i.e. ["Text", "Text",...]. When a match is made from the MATCH_FILE_META List, the same index from the INSERT_FILE_NAME List will be chosen.


INSERT_TEXT : "Text" [Required]
-or-
INSERT_FILE_NAME : { TEXT : "Text", OPTIONS : ["Modify Options 1",2,3,...], PLACEMENT : (PLACE, OF_) }

TEXT is the text to insert into file names.

OPTIONS are used to further modify text. All current modify options are listed below:

COUNT is a way to dynamically add an iterating number to a file name. TEXT : ( "Starting Text", ( Starting Number, Ending Number ), "Ending Text" ). Note: The ending number is optional and count resets after each directory change.
COUNT_TO is the max amount of renames to make before stopping and moving onto the next TEXT in a List or the next directory. Is similar to COUNT's ending number without adding an iterating number to a file name. TEXT : ( ‘Text’, Number )
MINIMUM_DIGITS is the minimum number or digits used for any dynamic text. For example, OPTIONS : ( MINIMUM_DIGITS, 3 ) will turn ‘7’ into ‘007’.
RANDOM_NUMBERS will generate random numbers that are added to file names. Note: All random generators can be used together.
RANDOM_LETTERS will generate random letters that are added to file names.
RANDOM_SPECIALS will generate random special characters that are added to file names.
RANDOM_OTHER will generate random other (uncommon, unique, or foreign) characters that are added to file names.
RANDOM_SEED is the starting seed number to use in random generators. Default: (RANDOM_SEED, None)
REPEAT_TEXT_LIST will repeat a text list once the end of a text list is reached. The length of a text list is treated as a soft rename limit unless this option is used. TEXT must be dynamic if used, [COUNT, RANDOM, etc.].
EXTENSION if used in modify options and EDIT_TYPE : ADD or REPLACE only the extension will be replaced or added on to the END. If used with EDIT_TYPE : RENAME the entire file name may be rewritten including the extension, but only if a "." is in TEXT. Note: You don't need to use EXTENSION in all cases where you wish to match or modify the extension.
INSERT_META_DATA will retrieve specific meta data from a file and add it to the file name. ('Text', File Meta Data, 'Text', File Meta Data, 'Text', ...)
NO_ADD_DUPES will avoid adding duplicate text in the same PLACEMENT (only when using ADD).
REGEX will allow the use of regular expression to insert matched groups into text. See REGEX_GROUP above.
CUSTOM is when you need to write/code your own unique custom file renaming procedure. Search script for: def getCustomText

PLACEMENT when using EDIT_TYPE : ADD this signifies where to place text. For example, ( START, OF_FILE_NAME ). All current placement options are listed below:

START to place at the start of...
LEFT to place at the left of...
END to place at the end of... [Default]
RIGHT to place at the right of...
BOTH to place at both sides of...
BOTH_ENDS to place at both ends of...
OF_FILE_NAME to be placed at file name minus extension. [Default]
OF_MATCH to be placed at one or more matches found.


SOFT_RENAME_LIMIT : 0 to NO_LIMIT

Max number of file renames to make per directory or group of individual files dropped. Default: NO_LIMIT


HARD_RENAME_LIMIT : 0 to NO_LIMIT

Hard limit on how many files to rename each time script is ran, no mater how many directories or group of individual files dropped. Default: NO_LIMIT


LINKED_FILES : ["://File//Path",...]

Files that need to be updated of any file name changes to prevent broken links in apps. Make sure to use double slashes "//".


IDENTICAL_FILE_NAMES : "Paths"
-or-
IDENTICAL_FILE_NAMES : { LINKS : "Paths", OPTIONS : ["Search Option 1",2,3,...] }

LINKS are the paths to directories to find identical file names (to those already renamed) and rename them as well.

OPTIONS are used to further customize search criteria. All current search options are listed below:

MATCH_CASE means searches are case-sensitive. [Default]
NO_MATCH_CASE is used for non-case-sensitive searches.
SEARCH_SUB_DIRS is used to search sub directories within the provided directory paths.
EXTENSION will include file name + extension when comparing file names, else only the file name will be matched.


INCLUDE_SUB_DIRS : True or False

If a directory is used or dropped choose whether or not to search through sub-directories. Default: False


PRESORT_FILES : { "File Meta Data" : ASCENDING or DESCENDING }

Sort before renaming files using the file's meta data. Default: None
All current sort options are listed below:

ASCENDING sort in order of 0-9, A-Z [Default]
DESCENDING sort in order of 9-0, Z-A
ALPHABETICALLY or FILE_NAME sort file names in alphabetical order. [Default]
FILE_META_SIZE, FILE_META_ACCESSED, FILE_META_MODIFIED, FILE_META_CREATED(Windows Only), FILE_META_METADATA(UNIX), FILE_META_TYPE, FILE_META_MIME, FILE_META_FORMAT, FILE_META_FORMAT_LONG, FILE_META_HEIGHT, FILE_META_WIDTH, FILE_META_LENGTH, FILE_META_BIT_DEPTH, FILE_META_VIDEO_BITRATE, FILE_META_VIDEO_FRAME_RATE, FILE_META_AUDIO_BITRATE, FILE_META_AUDIO_SAMPLE_RATE, FILE_META_AUDIO_CHANNELS, FILE_META_AUDIO_CHANNEL_LAYOUT, FILE_META_AUDIO_TITLE, FILE_META_AUDIO_ALBUM, FILE_META_AUDIO_ARTIST, FILE_META_AUDIO_YEAR, FILE_META_AUDIO_GENRE, FILE_META_AUDIO_PUBLISHER, FILE_META_AUDIO_TRACK


Dynamic Text Format

Tuple: ("Starting Text", Integer/Tuple, "Ending Text")
List: ["Text1", "Text2", ...]
Tuples can go inside of Lists. [ "Text", ("Text-[", (1,10), "]") ]


Example Presets:

preset13 = {
  EDIT_TYPE        : REPLACE,
  MATCH_FILE_NAME  : { TEXT    : " (U)",
                       OPTIONS : [ (MATCH_LIMIT, 1), SEARCH_FROM_RIGHT ] },
  INSERT_FILE_NAME : { TEXT    : "" },
  LINKED_FILES     : [ "X:\\Path\\To\\File_With_Links.xml" ],
  INCLUDE_SUB_DIRS : True
}

This preset searches for file names with a " (U)" and match the first one found starting from the right. Then replace that single match with an empty string, which effectively removes the " (U)" from the file name. Afterwards if a file was successfully renamed then attempt to find any links in the provided XML file and update them. Lastly, if a directory is being searched also search any sub-directories.

Example File Renames:

  • "some file (U).bin" will change to "some file.bin"
  • "file (U) (U).doc" will change to "file (U).doc "

preset19 = {
  EDIT_TYPE        : ADD,
  MATCH_FILE_NAME  : { TEXT      : [ ".jpg", ".png" ],
                       OPTIONS   : [ NO_MATCH_CASE, EXTENSION, SAME_MATCH_INDEX ] },
  INSERT_FILE_NAME : { TEXT      : [ ("-", (1,200), ""), ("-", (1001,2200), "") ],
                       OPTIONS   : [ COUNT, (MINIMUM_DIGITS, 4) ],
                       PLACEMENT : ( END, OF_FILE_NAME ) }
}

This preset searches for image files with the extension ".jpg" or ".png" ignoring case. Depending on whether a match is ".jpg" or ".png" add an incrementing number starting a 0001 or 1000 respectively to the end of the file name.

Example File Renames:

  • "image_file.jpg" will change to "image_file-0001.jpg"
  • "image_file.png" will change to "image_file-1001.png"

preset23 = {
  EDIT_TYPE        : RENAME,
  IGNORE_FILE_NAME : { TEXT     : [ 'skip' ],
                       OPTIONS  : [ NO_MATCH_CASE ] },
  INSERT_FILE_NAME : { TEXT     : [ ('RandomS-(', 4, ')'), ('RandomL-[', (7), ']') ],
                       OPTIONS  : [ RANDOM_NUMBERS, RANDOM_LETTERS, (RANDOM_SEED, 167), REPEAT_TEXT_LIST ] }
}

This preset searches for all files except those that have the string "skip" in their file name. Then rename those files alternating between RandomS-(####) and RandomL-[#######] because of the REPEAT_TEXT_LIST option. The #’s could be any number or letter. The random seed chosen means the random #’s will always start and continue with the same pattern every time a new rename task is ran including directory and file group changes.

Example File Renames:

  • "a_file.pdf" will change to "RandomS-(1py7).pdf"
  • "skip_file.doc" will not be renamed.
  • "some_file.txt" will change to "RandomL-[jjc124z].txt"

About

This script will rename one or more files either by adding new text or replacing text in the file names.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages