PowerShell Utility to Batch Rename Files with Numeric Prefix

PowerShell script to batch rename files by adding numeric prefixes with leading zeros, preserving extensions and customizable path, prefix, and padding.

Brief Introduction

PowerShell Utility to Batch Rename Files with Numeric Prefix (powershell)
DESCRIPTION:
PowerShell script that sequentially renames all files in a specified folder by adding a numeric prefix with leading zeros to maintain order.

ADDITIONAL NOTES:
The script renames only files (not folders) in the specified folder. The numeric prefix is added at the beginning of the original file name, preserving the extension. You can customize the path, prefix, and numeric padding by modifying the variables at the start of the script.

EXPLANATIONS:
- The $Path variable indicates the folder where to look for files to rename.
- The $Prefix variable allows adding text before the sequential number (e.g., 'File_').
- The $Padding variable defines the number of digits for the sequential number, with leading zeros for uniformity.
- The foreach loop iterates over all files sorted by name in the specified folder.
- The PadLeft function is used to add leading zeros to the sequential number.
- The original file extension is preserved in the new name.
- The Rename-Item command actually renames the file.
- Comments are in Italian to facilitate understanding and maintenance.

Example Code: powershell

# 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 Green
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

PowerShell 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

Similar Topics

PowerShell Utility to Generate Simple Random Passwords
PowerShell script to generate simple random passwords with user-defined length using alphanumeric characters for secure and easy password creation.
PowerShell Script to Rename Files Based on Creation Date
PowerShell script to rename files by adding creation date prefix in YYYYMMDD format for organized file management and conflict-free renaming.
PowerShell Utility to Search Files by Extension in a Folder
PowerShell script to search files by extension in a folder with full path output. Simple, reusable, and error-handled for efficient file searching.
PowerShell Script to List Running Processes and Save to CSV
PowerShell script to list running processes with details and save them to a CSV file for easy monitoring and analysis using built-in commands.
PowerShell Script to Count the Number of Files in Each Subfolder
PowerShell script to count files in each subfolder of a specified directory with clear output and error handling for non existing folders.
PowerShell Utility to Replace a String in All Text Files in a Folder
PowerShell script to search and replace strings in all text files within a folder, automating bulk text edits with error handling and no extra software.
PowerShell Script to Create a Default Folder Structure for a Project
PowerShell script to create a default folder structure for software projects, standardizing organization and speeding up setup on Windows systems.
PowerShell Script to Monitor Free Space on a Local Drive
PowerShell script to monitor free space on a local drive with customizable threshold and detailed console output for Windows PowerShell 5.1 or higher.