Mass rename files in PowerShell with sequential numeric suffixes

I have 14,000 images sorted by file by year and month, but taken with multiple cameras, and I want the file name to reflect the date of the shooting. For example, on October 16, 1998, the images are in a folder named 1998 \ October 10 \ 19981016 .

I want all the photos to be named 19981016_0001 19981016_0002 etc. I tried a few suggestions, but that didn't work.

I can get to the point where the folders that I want to change are listed, but I cannot change them. All my photos are .jpg.

Attempts to create a temporary copy file in case I messed it up. I started by typing cd "C:\Documents and Settings\Brooke LastName\Desktop\Temp" then, having successfully downloaded my file, I used the formula found in this forum.

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

The error I received said

 Unexpected token ' .19981016' in expression or statement. At line:1 char:12 + $.19981016 <<<< 

The error was repeated several times.

I found several formulas on the Internet, but most of the created files that are numbered in brackets, for example, leave (1) .jpg . I want a four-digit counter after the underscore at the end of my date, e.g. 19981016_0001

+9
powershell
source share
2 answers

The syntax is off. A few questions:

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

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 file names will be 19981016_0001.jpg , 19981016_0002.jpg , 19981016_0003.jpg , etc.

A few notes:

  • You said you want file names such as 19981016_0001 , but I assume that you want to save the .jpg extension.
  • You need to initialize $ i , otherwise it will start at 0 if it is not already defined or, even worse, from some other number if you previously used $ i in the same PowerShell session. For example, if you have 1,432 .jpg files in a directory, and you first run the command with -WhatIf and then run it for real, the numbers start at 1432.
  • $ i ++ increases $ i by 1. However, if you use it as a value, it increases after reading the value; therefore, if $ i is undefined, it will start at 0.
  • Get-ChildItem matches ls . I used my own PowerShell name, but they are interchangeable ( ls , dir ) and gci are all aliases for Get-ChildItem ).
  • % {} is a shorthand for Foreach-Object
+29
source share

This is close to the original question. In fact, you can pass an array of script blocks to foreach-object -process as described (barely). I know this only after Bruce Payette’s book. The containing folder name is "19981016". Source file names may not go in the order you expect.

 ls *jpg | Foreach {$i=1} {Rename-Item $_ -NewName ("$($_.directory.name)_{0:000#}.jpg" -f $i++) -whatif} What if: Performing the operation "Rename File" on target "Item: C:\users\js\19981016\file1.jpg Destination: C:\users\js\19981016\19981016_0001.jpg". What if: Performing the operation "Rename File" on target "Item: C:\users\js\19981016\file10.jpg Destination: C:\users\js\19981016\19981016_0002.jpg". What if: Performing the operation "Rename File" on target "Item: C:\users\js\19981016\file2.jpg Destination: C:\users\js\19981016\19981016_0003.jpg". 
0
source share

All Articles