Here is the solution to automatically rename all files (.tif, .png, .jpg) with their exact dimensions in centimeters with 2 decimal places (00.00cm).
Option 1: One-Click Batch File (AutoRename_CM.bat)
Create a .bat file inside your image folder to rename everything with a single click.
1-Open Notepad.
2-Copy and paste the code below:
@echo off
powershell -Command "Add-Type -AssemblyName System.Drawing; Get-ChildItem -File | Where-Object { $_.Extension -match '^\.(png|jpg|jpeg|tif|tiff)$' } | ForEach-Object { try { $img = [System.Drawing.Image]::FromFile($_.FullName); $dpiX = if ($img.HorizontalResolution -gt 0) { $img.HorizontalResolution } else { 72 }; $dpiY = if ($img.VerticalResolution -gt 0) { $img.VerticalResolution } else { 72 }; $wCm = '{0:F2}' -f (($img.Width / $dpiX) * 2.54); $hCm = '{0:F2}' -f (($img.Height / $dpiY) * 2.54); $img.Dispose(); $baseName = $_.BaseName -replace '_\d+(\.\d+)?x\d+(\.\d+)?cm$', ''; $newName = \"${baseName}_${wCm}x${hCm}cm$($_.Extension)\"; Rename-Item -Path $_.FullName -NewName $newName -ErrorAction Stop } catch {} }"
echo រួចរាល់ហើយ! (Done!)
pause
1-Click File > Save As...
2-Set Save as type to All Files (.).
3-Name the file AutoRename_CM.bat and save it inside your image folder.
4-Double-click AutoRename_CM.bat whenever you want to rename all files.
Option 2: PowerShell Script
If you prefer to run it directly from PowerShell:
// PowerShell
Add-Type -AssemblyName System.Drawing
Get-ChildItem -File | Where-Object { $_.Extension -match '^\.(png|jpg|jpeg|tif|tiff)$' } | ForEach-Object {
try {
$img = [System.Drawing.Image]::FromFile($_.FullName)
# Detect DPI resolution (defaults to 72 DPI if missing)
$dpiX = if ($img.HorizontalResolution -gt 0) { $img.HorizontalResolution } else { 72 }
$dpiY = if ($img.VerticalResolution -gt 0) { $img.VerticalResolution } else { 72 }
# Calculate CM formatted to 2 decimal places (00.00)
$wCm = "{0:F2}" -f (($img.Width / $dpiX) * 2.54)
$hCm = "{0:F2}" -f (($img.Height / $dpiY) * 2.54)
$img.Dispose()
# Remove old cm tag if re-run
$baseName = $_.BaseName -replace '_\d+(\.\d+)?x\d+(\.\d+)?cm$', ''
$newName = "${baseName}_${wCm}x${hCm}cm$($_.Extension)"
Rename-Item -Path $_.FullName -NewName $newName -ErrorAction Stop
Write-Host "Renamed: $($_.Name) -> $newName" -ForegroundColor Green
} catch {
Write-Host "Skipped: $($_.Name)" -ForegroundColor Yellow
}
}
Result Example:
288-40.png => 288-40_80.05x120.01cm.png
289-17.tif => 289-17_350.06x270.04cm.tif
វិធីបង្កើតហ្វាលត្រឡប់ឈ្មោះដើម (UndoRename.bat)
1-បើកកម្មវិធី Notepad។
2-ចម្លង (Copy) កូដខាងក្រោមដាក់ចូលក្នុង Notepad៖
@echo off
powershell -Command "Get-ChildItem -File | ForEach-Object { $newName = $_.Name -replace '_\d+(\.\d+)?x\d+(\.\d+)?(cm)?(?=\.[^.]+$)', ''; if ($newName -ne $_.Name) { Rename-Item -Path $_.FullName -NewName $newName } }"
echo បានត្រឡប់ទៅឈ្មោះដើមវិញរួចរាល់! (Undo Completed!)
pause
1-ចុច File > Save As...
2-ត្រង់ Save as type ជ្រើសយក All Files (.)
3-ដាក់ឈ្មោះហ្វាលថា UndoRename.bat ហើយចុច Save ទុកក្នុង Folder រូបភាពរបស់អ្នក។
របៀបប្រើប្រាស់៖
នៅពេលណាដែលអ្នកចង់លុបទំហំចេញ ហើយត្រឡប់ទៅឈ្មោះដើមវិញ គ្រាន់តែ Double-Click (ចុចពីរដង) លើហ្វាល UndoRename.bat ជាការស្រេច។