Description
Batch rename files in a directory under Windows, MacOS and Linux.
Usage
Command Prompt Command (used for Windows)
1 | ren * *.jpg |
ren
is short for “rename”, it’s the command used to rename files.*
represents a wildcard character that matches any sequence of characters in a file name.*.jpg
specifies the new name pattern for the files. In this case, it renames all files in the current directory by changing their extensions to “.jpg”.
batchRename.bat
1 | @echo off |
@echo off
: This command turns off the command echoing, so it doesn’t display each command in the batch file as it’s executed.setlocal enabledelayedexpansion
: This command enables delayed environment variable expansion, which allows you to use variables within a loop.set i=1
: This initializes a variablei
with a value of 1.for %%A in (*.mp4) do (...)
: This loop iterates over each file in the current directory that ends with.mp4
.ren "%%A" "Tom_And_Jerry.!i!.mp4"
: This renames each file, appending a prefix “Tom_And_Jerry.” followed by the current value ofi
and “.mp4” extension.set /a i+=1
: This increments the value ofi
by 1 for the next iteration of the loop.
Bash Command (used for Unix-liked systems MacOS/Linux)
1 | i=1; for file in *.mp4; do mv "$file" "Tom_And_Jerry.$i.mp4"; ((i++)); done |
i=1;
initializes a variablei
with a value of 1.for file in *.mp4;
is a loop that iterates over each file in the current directory that ends with.mp4
.do
marks the beginning of the loop body.mv "$file" "Tom_And_Jerry.$i.mp4";
renames each file, appending a prefix “Tom_And_Jerry.” followed by the current value ofi
and “.mp4” extension.((i++));
increments the value ofi
by 1.done
marks the end of the loop.
Reference
About this Post
This post is written by Andy, licensed under CC BY-NC 4.0.