Share via

How to rename files sequentially at a number other than 1

Anonymous
2019-05-14T16:49:47+00:00

Hey gang,

I've got a bunch of files (~300) that need to be renamed sequentially. I used the F2 command to change the names from "image1.png" to "M00000(1).png", etc. Is there a switch or something in PowerShell or the command prompt to start at a number other than 1 and remove the parentheses? In this case, I need them to start at "M000214.png".

I know the PowerShell command is supposed to start with this: dir | Rename-Item -NewName {$_.name replace "M?????" ""}, but my Google-fu has failed me after that. Can any of you fabulously good-looking, ridiculously smart folks help me out?

Thanks in advance!

Windows for home | Windows 10 | Files, folders, and storage

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2019-05-14T19:29:42+00:00

    In PowerShell, after cd'ing to the proper folder, try the follwoing command --- the 'whatif' parameter means nothing will happen, but it will tell you what it would do. If the output indicates the results you want, remove the '-whatif' and re-run the command. If it seems off, post back & we can refine the command.

    $i = 214

    foreach ($file in gci *.png) {

    rename-item $file ('M{0:000000}.png' -f $i++) -whatif

    }

    Or:

    $i = [ref]214

    gci *.png | rename-item -NewName {'M{0:000000}.png' -f $i.value++} -whatif

    Keith

    6 people found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2019-05-15T14:35:55+00:00

    In PowerShell, after cd'ing to the proper folder, try the follwoing command --- the 'whatif' parameter means nothing will happen, but it will tell you what it would do. If the output indicates the results you want, remove the '-whatif' and re-run the command. If it seems off, post back & we can refine the command.

    $i = 214

    foreach ($file in gci *.png) {

    rename-item $file ('M{0:000000}.png' -f $i++) -whatif

    }

    Or:

    $i = [ref]214

    gci *.png | rename-item -NewName {'M{0:000000}.png' -f $i.value++} -whatif

    Keith

    Hi Keith,

    Thanks for the prompt reply. I banged heads with one of our scripting boffins and we came up with the following:

    $i=214

    Get-ChildItem *.png | %{Rename-Item $_ -NewName ('M{0:D6}.png' -f $i++)}

    It worked perfectly except for appending 110 to the first 25 filenames (i.e., M000111 instead of M000001). Rage-making, right? Closer examination revealed the culprit: PowerShell and File Explorer approach numbering from opposing directions: File Explorer lists files by 1,2,3...10,11,12, whereas PowerShell handles files by 1,10,11,2,20,21...

    Because the original files were named image1, image2, image3... instead of image001, image002, image003, etc., PowerShell renamed them according to its own internal logic. Adding leading zeroes to the original file names fixed the issue when executing the script. Good gravy, what a pain! But as Grandpa Orca was fond of saying, "Experience is what you get when you don't get what you want." You can bet we added this to the company wiki tout de suite!

    Thanks again for your help--it really put us on the right track!

    -terry-

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2019-05-15T18:51:28+00:00

    You're welcome. Thanks for the feedback & marking my reply as 'Answer'.

    I forgot about 'smart sorting' (search term: NoStrCmpLogical) in Explorer v.s. the sorting in PS. Here's a bit of PS code to sort a file collection that have filenames like 'file(#).ext':

    gci |

    select fullname, @{N='SortBy'; E={Void; [Int]$matches[1]}} | sort SortBy

    Keith

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2019-05-15T19:13:01+00:00

    Thanks, Keith! You are awesome and a genius! (Clearly, I need to take a class on PowerShell scripting.)

    -Orca-

    0 comments No comments
  4. Vijay A. Verma 104.8K Reputation points Volunteer Moderator
    2019-05-14T17:08:04+00:00

    Hi StLOrca

    Greetings! I am Vijay, an Independent Advisor. I am here to work with you on this problem.

    Best way to do is through any file renamer utility. Microsoft Store has many free file renaming utilities. Just launch Microsoft Store and type rename and you get many options.

    If you need a recommendation, you can download below from Microsoft Store

    https://www.microsoft.com/store/productId/9NBLG...

    Do let me know if you have any more question or require further help.

    0 comments No comments