Last active
November 14, 2023 14:12
-
-
Save alsolovyev/5c21618a7ca58c7b76e3320c0b9dbbb9 to your computer and use it in GitHub Desktop.
Batch script to rename files in a specified directory based on a user-defined template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Batch Script: FileRenamer.bat | |
:: | |
:: Description: | |
:: This batch script renames files in a specified directory based on a user-defined template. | |
:: It allows customization of the renaming process by providing options for directory, | |
:: template, file pattern, delimiter, and starting index. | |
:: | |
:: Usage: | |
:: FileRenamer.bat [-d <directory>] [-t <template>] [-p <pattern>] [-del <delimiter>] [-i <index>] | |
:: | |
:: Options: | |
:: -d <directory> Specify the target directory. Default is the current directory. | |
:: -t <template> Specify the filename template. Default is "{i}{d}{name}.{ext}". | |
:: -p <pattern> Specify the file matching pattern using wildcards. Default is "*". | |
:: -del <delimiter> Specify the delimiter used in the new filenames. Default is a space. | |
:: -i <index> Specify the starting index for renaming files. Default is 1. | |
:: | |
:: Example: | |
:: FileRenamer.bat -d "C:\MyFiles" -t "{name}_{i}.{ext}" -p "*.txt" -del "_" -i 100 | |
:: | |
:: Script Logic: | |
:: 1. Parses command line arguments to customize the renaming process. | |
:: 2. Loops through files matching the specified pattern in the specified directory. | |
:: 3. Generates new filenames using the specified template and updates file names accordingly. | |
:: | |
:: Note: | |
:: Ensure to test the script on a small set of files before bulk renaming to avoid unintentional changes. | |
:: | |
:: Author: Solovyev Aleksey <[email protected]> | |
:: Date: 14.11.2023 | |
@echo off | |
setlocal enabledelayedexpansion | |
:: Set the console code page to UTF-8 to support Unicode characters | |
chcp 65001 > nul | |
:: Initialize variables | |
set "delimiter= " | |
set "directory=%CD%" | |
set "index=1" | |
set "pattern=*" | |
set "template={i}{d}{name}.{ext}" | |
:: Parse command line arguments | |
:parse_args | |
if "%~1"=="" goto :args_done | |
if /i "%~1"=="-d" ( | |
set "directory=%~2" | |
shift | |
) else if /i "%~1"=="-t" ( | |
set "template=%~2" | |
shift | |
) else if /i "%~1"=="-p" ( | |
set "pattern=%~2" | |
shift | |
) else if /i "%~1"=="-del" ( | |
set "delimiter=%~2" | |
shift | |
) else if /i "%~1"=="-i" ( | |
set "index=%~2" | |
shift | |
) | |
shift | |
goto :parse_args | |
:args_done | |
cd "%directory%" | |
:: Loop through files matching the specified pattern | |
for %%F in (%pattern%) do ( | |
:: Ensure the index is two digits (padded with leading zeros if needed) | |
if !index! lss 10 set index=0!index! | |
:: Generate the new name using the template and replace placeholders | |
set name=%template:{i}=!index!% | |
set name=!name:{d}=%delimiter:"=%! | |
set name=!name:{name}=%%~nF! | |
set name=!name:{ext}=%%~xF! | |
set name=!name:..=.! | |
ren "%%F" "!name!" | |
set /a "index+=1" | |
) | |
:end | |
exit /b 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment