Bulk renaming of files in PowerShell with sequential numeric suffixes

Brooke

I have 14,000 pictures sorted into files by year and month but taken with multiple cameras and I want the file name to reflect the date taken. For example October 16, 1998 pictures are in a folder called 1998\10 October\19981016.

I want the all the pictures to be named 19981016_0001 19981016_0002 etc. I tried multiple suggestions but it hasn't worked.

I can get to the point where it lists the folder I want to change but I'm unable to actually change it. All of my pictures are .jpg.

Attempts I created a temp file of copies in case I messed it up. I started by typing cd "C:\Documents and Settings\Brooke LastName\Desktop\Temp" then after successfully getting my file to load I used a formula I found on this forum.

ls *jpg | Foreach {$i=1} {Rename-Item _ -NewName ("$($.19981016){0:00000000#} .jpg" -f $i++) -whatif}

The error I got said

Unexpected token ' .19981016' in expression or statement.

At line:1 char:12 + $.19981016 <<<<

The error repeated several times.

I found several formulas on the web but most created files that would number with parentheses for example vacation (1).jpg. I want a four digit counter after an underscore at the end of my date, e.g. 19981016_0001

Adi Inbar

The syntax is way off. A few issues:

  • I'm not sure what $($.19981016) was intended to produce, but $( ) evaluates the expression in the parentheses and interpolates the result into the string, and you're getting the error because $.19981016is not a valid expression. The error would be repeated for each .jpg file in the directory.
  • {0:00000000#} in a formatted string will create a zero-padded number of 9 digits, but a shorter way to do that is {0:D9}. However, I thought you wanted the zero-padded number to have 4 digits, so that should be {0:0000#} or {0:D4}.
  • I'm not sure what Foreach {$i=1} { [...] is supposed to do. The keyword foreach can mean a foreach loop, or it can be shorthand for Foreach-Object. In this context, receiving input from a pipeline, it's necessarily the latter, but this syntax is incorrect either way.

This will do what you want, if I understand the description correctly:

$i = 1
Get-ChildItem *.jpg | %{Rename-Item $_ -NewName ('19981016_{0:D4}.jpg' -f $i++)}

The filenames will be 19981016_0001.jpg, 19981016_0002.jpg, 19981016_0003.jpg, etc.

A few notes:

  • You said that you want filenames like 19981016_0001, but I'm assuming you want to keep the .jpg extension.
  • You need to initialize $i, otherwise it will start from 0 if it's not yet defined, or worse yet, from some other number if $i was used previously in the same PowerShell session. For example, if you have 1,432 .jpg files in the directory, and you run the command first with -WhatIf, and then run it for real, the numbers will start from 1432.
  • $i++ increments $i by 1. However, if you're using it as a value, it increments after the value is read; that's why if $i is undefined, it will start from 0.
  • Get-ChildItem is the same as ls. I used the native PowerShell name, but they're interchangeable (ls, dir, and gci are all aliases for Get-ChildItem).
  • %{ } is shorthand for Foreach-Object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Powershell Renaming with sequential numeric suffixes

From Dev

Use PowerShell to bulk process pairs of numeric files

From Dev

Sequential renaming of files

From Dev

bulk renaming sets of image files

From Dev

Recursively renaming files with Powershell

From Dev

Renaming files with the same name using sequential numbers?

From Dev

Bulk renaming of camelcase files to include spaces

From Dev

Renaming files with unnecessary zeros in Powershell

From Dev

Renaming all files in folder in numeric order

From Dev

Bulk renaming of csv files of same name using shell script

From Dev

Renaming Bulk files and changing certain prefix and raring them

From Dev

How to specify a space as part of a wildcard string for bulk renaming of files

From Dev

renaming video files in bulk taking input from a playlist file

From Dev

Renaming files in a folder in Windows using Powershell

From Dev

Renaming multiple files with the same extension using PowerShell

From Dev

How can I bulk rename files in a photo folder with sequential numbers

From Dev

complexity of a Sequential algorithm - min suffixes

From Dev

Rename multiple files to their suffixes

From Dev

difficulty renaming batch of files with PowerShell without losing extension

From Dev

PowerShell renaming multiple files, specific part of file name

From Dev

Bulk renaming using regex in Php

From Dev

"--numeric-suffixes" does not allow an argument with "split"

From Dev

Remove files with smallest filename suffixes

From Dev

Powershell case sensitivity renaming

From Dev

Linux Bulk file renaming at specific position

From Dev

Python basic - renaming files

From Dev

PHP Transliteration and renaming files

From Dev

Issue with program for renaming files

From Dev

Renaming files based on a string

Related Related

  1. 1

    Powershell Renaming with sequential numeric suffixes

  2. 2

    Use PowerShell to bulk process pairs of numeric files

  3. 3

    Sequential renaming of files

  4. 4

    bulk renaming sets of image files

  5. 5

    Recursively renaming files with Powershell

  6. 6

    Renaming files with the same name using sequential numbers?

  7. 7

    Bulk renaming of camelcase files to include spaces

  8. 8

    Renaming files with unnecessary zeros in Powershell

  9. 9

    Renaming all files in folder in numeric order

  10. 10

    Bulk renaming of csv files of same name using shell script

  11. 11

    Renaming Bulk files and changing certain prefix and raring them

  12. 12

    How to specify a space as part of a wildcard string for bulk renaming of files

  13. 13

    renaming video files in bulk taking input from a playlist file

  14. 14

    Renaming files in a folder in Windows using Powershell

  15. 15

    Renaming multiple files with the same extension using PowerShell

  16. 16

    How can I bulk rename files in a photo folder with sequential numbers

  17. 17

    complexity of a Sequential algorithm - min suffixes

  18. 18

    Rename multiple files to their suffixes

  19. 19

    difficulty renaming batch of files with PowerShell without losing extension

  20. 20

    PowerShell renaming multiple files, specific part of file name

  21. 21

    Bulk renaming using regex in Php

  22. 22

    "--numeric-suffixes" does not allow an argument with "split"

  23. 23

    Remove files with smallest filename suffixes

  24. 24

    Powershell case sensitivity renaming

  25. 25

    Linux Bulk file renaming at specific position

  26. 26

    Python basic - renaming files

  27. 27

    PHP Transliteration and renaming files

  28. 28

    Issue with program for renaming files

  29. 29

    Renaming files based on a string

HotTag

Archive