{"id":38958,"date":"2025-08-09T06:50:37","date_gmt":"2025-08-09T12:50:37","guid":{"rendered":"https:\/\/sanuja.com\/blog\/?p=38958"},"modified":"2025-08-09T09:13:05","modified_gmt":"2025-08-09T15:13:05","slug":"renames-files-using-a-base-name-and-incremented-number-powershell","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/renames-files-using-a-base-name-and-incremented-number-powershell","title":{"rendered":"Renames files using a base name and incremented number &#8211; PowerShell"},"content":{"rendered":"<h3>Video demo<\/h3>\n<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/kSN0jmDaA28?si=0olnYR6XkLqHDBXM\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Link: <a href=\"https:\/\/youtu.be\/kSN0jmDaA28?si=r1xV38nauAo9wrJ1\" target=\"_blank\" rel=\"noopener\">Rename all files using a base name and incremented number &#8211; YouTube<\/a><\/p>\n<p>You can copy the script from this page and modify according to your needs.<\/p>\n<p><!--more--><\/p>\n<h3>PS script<\/h3>\n<p>This is the PowerShell script I wrote to rename files.<\/p>\n<p><code><br \/>\n# This script renames all files in a specified folder with a new base name<br \/>\n# and a consecutive, zero-padded number (e.g., \"my_file_001.txt\").<\/p>\n<p># --- USER-DEFINED VARIABLES ---<br \/>\n# Specify the path to the folder containing the files you want to rename.<br \/>\n# Example: C:\\Users\\YourUsername\\Documents\\Photos<br \/>\n$folderPath = \"C:\\Users\\administrator\\Desktop\\Files\"<\/p>\n<p># Specify the new base name for the files.<br \/>\n# Example: \"sanuja\" will result in names like \"sanuja_001.jpg\"<br \/>\n$newName = Read-Host -Prompt \"Please specify the new file base name: \"<\/p>\n<p># Specify the starting number for the sequence.<br \/>\n# This will be formatted to be zero-padded.<br \/>\n$startNumber = Read-Host -Prompt \"Specify the starting number for the sequence: \"<br \/>\n$startNumber = [int]$startNumber<\/p>\n<p># --- SCRIPT LOGIC ---<br \/>\n# Get all files in the specified folder.<br \/>\n# The `-File` switch ensures we only get files and not subdirectories.<br \/>\n$files = Get-ChildItem -Path $folderPath -File<\/p>\n<p># Check if any files were found.<br \/>\nif ($files.Count -eq 0) {<br \/>\n    Write-Host \"No files found in the specified folder: $folderPath\"<br \/>\n} else {<br \/>\n    Write-Host \"Found $($files.Count) files. Renaming...\"<\/p>\n<p>    # Loop through each file found in the folder.<br \/>\n    foreach ($file in $files) {<br \/>\n        # Create a new, zero-padded number string (e.g., 1 becomes \"001\").<br \/>\n        $paddedNumber = \"{0:D3}\" -f $startNumber<\/p>\n<p>        # Construct the new file name using the base name, padded number, and the original extension.<br \/>\n        # Example: \"sanuja_001.jpg\"<br \/>\n        $newFileName = \"${newName}_${paddedNumber}$($file.Extension)\"<\/p>\n<p>        # Define the full path for the new file.<br \/>\n        $newFilePath = Join-Path -Path $folderPath -ChildPath $newFileName<\/p>\n<p>        # Rename the file. The `-WhatIf` parameter is commented out.<br \/>\n        # UNCOMMENT `-WhatIf` to see what the script will do without making any changes.<br \/>\n        Rename-Item -Path $file.FullName -NewName $newFileName #-WhatIf<\/p>\n<p>        # Write a message to the console to show the progress.<br \/>\n        Write-Host \"Renamed $($file.Name) to $newFileName\"<\/p>\n<p>        # Increment the counter for the next file.<br \/>\n        $startNumber++<br \/>\n    }<\/p>\n<p>    Write-Host \"File renaming complete.\"<br \/>\n}<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Video demo Link: Rename all files using a base name and incremented number &#8211; YouTube You can copy the script from this page and modify according to your needs.<\/p>\n","protected":false},"author":2,"featured_media":38961,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[21],"class_list":["post-38958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","tag-cmd"],"jetpack_featured_media_url":"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2025\/08\/thumbnail_Renames-all-files-using-a-base-name-and-incremented-number-scaled.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/38958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/comments?post=38958"}],"version-history":[{"count":5,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/38958\/revisions"}],"predecessor-version":[{"id":38964,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/38958\/revisions\/38964"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media\/38961"}],"wp:attachment":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media?parent=38958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=38958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=38958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}