Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell File Renaming: How to Reorder Files Sequentially Using a CSV Map

I recently received a task to rename a bunch of files by changing their numbering in the filename while working on a PowerShell DevOps project. I had multiple Markdown files with numbers at the start of their names, and I needed to change these numbers with the filename based on a mentioned in CSV file. Here’s how I solved it with a simple PowerShell script.

In my project folder, I had file names exampled like  01-file.md, 02-doc.md, 03-notes.md, and so on. The task was to rename them by swapping their number prefixes according to a CSV file. For example, change 01-... to 05-..., 02-... to 01-..., and so forth. Manually doing task was very hectic and time consuming.

$fileExtension = "*.md"
$numbering = Import-Csv -Path "$PSScriptRoot\re-numbering.csv"
$files = Get-ChildItem -Path "$PSScriptRoot\files" -File -Recurse -Include $fileExtension  #-Exclude '*.csv','*.zip','*.ps1'
$files

foreach ($file in $files)
{
    $extractedOldNumber = $file.Name.Substring(0,2)
    #[regex]::Match($file.Name, '^\d+') # Regex example to extract leading number
    $newNumber = $numbering | Where-Object {[int]$($_.OldNumber.Trim()) -eq [int]$extractedOldNumber.Trim()} | Select-Object -ExpandProperty NewNumber
    #$oldName = $file.Name
    $newName = $file.name -replace '^\d{1,2}', $newNumber
    #oldName
    #newName
  $file | Rename-Item -NewName $newName
}

Write-Host "Renaming Completed!"

Get-ChildItem -Path "$PSScriptRoot\files" -File -Recurse -Include $fileExtension  #-Exclude '*.csv','*.zip','*.ps1'

Download this script rename-file.ps1.zip here or it is also available on github.com.

To solve the task I created a CSV file named re-numbering.csv with two columns: OldNumber and NewNumber. as per below example table:

OldNumberNewNumber
15
21
32
......

This table tells the script which old number to replace with which new number. And Below is the output screenshot.

Example CSV mapping table with columns OldNumber and NewNumber, showing how file prefixes are mapped (e.g., old 1 maps to new 5, old 2 maps to new 1) for PowerShell file renaming.

This script saved me a lot of time. Instead of manually renaming each file, I just updated the CSV table and let PowerShell handle the task. It’s a simple, reusable solution for anyone needing to batch-rename files based on a numbering pattern. In case if you have numbers in between the text you can use regex to find the number and replace it.

[regex]::Replace($file.Name, '\d*')

Useful Articles
Convert PowerShell ps1 to EXE using native windows tool iexpress
PowerShell WPF XAML simple data binding datacontext example - Part 1
PowerShell WPF XAML control to control data binding datacontext example - Part 2
PowerShell WPF XAML DataGrid control databinding datacontext example - Part 3
PowerShell WPF XAML multibinding datacontext template example - Part 4
PowerShell HTML Server Racks Cabinet Live Diagram maker Demo
PowerShell HTML based Live Ping Monitor Demo
Automate chrome browser with PowerShell using selenium
Azure Rest API connect with Powershell and create resources
Monitoring Dell Server Health Using RACADM CLI: Sensor Info from iDRAC
PowerShell Create and Export Self-Signed RSA Certificates (PFX, CER, PEM)
How to create byte array in PowerShell

Go Back

Comment

Protected by Mathcha

Blog Search

Page Views

1 4 6 7 6 2 9 7

Archive

Follow me on Blogarama