PowerShell script to batch rename files by adding numeric prefixes with leading zeros, preserving extensions and customizable path, prefix, and padding.
# PowerShell script to batch rename files with numeric prefix
# Configuration: folder path containing the files to rename
$Path = "C:\Users\Utente\Documenti\FileDaRinominare"
# Configuration: prefix to add before the sequential number
$Prefix = "File_"
# Configuration: number of digits for the sequential number (zero padding)
$Padding = 3
# Retrieves all files in the specified folder, sorted by name
$files = Get-ChildItem -Path $Path -File | Sort-Object Name
# Initialize the numeric counter
$counter = 1
foreach ($file in $files) {
# Creates the sequential number with zero padding
$number = $counter.ToString().PadLeft($Padding, '0')
# Builds the new file name preserving the original extension
$newName = "$Prefix$number$($file.Extension)"
# Builds the full path of the new file
$newPath = Join-Path -Path $Path -ChildPath $newName
# Renames the file
Rename-Item -Path $file.FullName -NewName $newName
# Increments the counter
$counter++
}
Write-Host "Renamed $($files.Count) files in folder '$Path' with prefix '$Prefix' and padding of $Padding digits." -ForegroundColor GreenPowerShell 5.1 or higher
Folder containing the files to rename
CONFIG: $Path variable to specify the folder path
CONFIG: $Prefix variable to define the prefix to add before the number
CONFIG: $Padding variable to define the number of digits for the sequential number (e.g., 3 for 001, 002, ...)
DEPENDENCIES: No external dependencies