Bulk renaming of files in PowerShell with sequential numeric suffixes -


i found several answers on site renaming files in powershell i'm sorry if repeat. i've spent 5 hours on google , various websites trying figure out. have tried explain well. not programmer meticulous filing.

i have 14,000 pictures sorted files year , month taken multiple cameras , want file name reflect date taken. example october 16, 1998 pictures in folder called 1998\10 october\19981016. want pictures named 19981016_0001 19981016_0002 etc. tried multiple suggestions hasn't worked. due lack of programmer knowledge haha. me out , give me dummy instructions? can point lists folder want change i'm unable change it. of pictures .jpg. tried attach screenshot of did unfortunately can't since i'm new here. in advance!

i created temp file of copies in case messed up. started typing cd "c:\documents , settings\brooke lastname\desktop\temp" after getting file load used formula found on forum.

ls *jpg | foreach {$i=1} {rename-item _ -newname ("$($.19981016){0:00000000#} .jpg" -f $i++) -whatif} 

the error got said

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

the error repeated several times.

i found several formulas on web created files number parentheses example vacation (1).jpg. want 4 digit counter after underscore @ end of date, e.g. 19981016_0001

the syntax way off. few issues:

  • i'm not sure $($.19981016) intended produce, $( ) evaluates expression in parentheses , interpolates result string, , you're getting error because $.19981016is not valid expression. error repeated each .jpg file in directory.
  • {0:00000000#} in formatted string create zero-padded number of 9 digits, shorter way {0:d9}. however, thought wanted zero-padded number have 4 digits, should {0:0000#} or {0:d4}.
  • i'm not sure foreach {$i=1} { [...] supposed do. keyword foreach can mean foreach loop, or can shorthand foreach-object. in context, receiving input pipeline, it's latter, syntax incorrect either way.

this want, if understand description correctly:

$i = 1 get-childitem *.jpg | %{rename-item $_ -newname ('19981016_{0:d4}.jpg' -f $i++)} 

the filenames 19981016_0001.jpg, 19981016_0002.jpg, 19981016_0003.jpg, etc.

a few notes:

  • you said want filenames 19981016_0001, i'm assuming want keep .jpg extension.
  • you need initialize $i, otherwise start 0 if it's not yet defined, or worse yet, other number if $i used in same powershell session. example, if have 1,432 .jpg files in directory, , run command first -whatif, , run real, numbers start 1432.
  • $i++ increments $i 1. however, if you're using value, increments after value read; that's why if $i undefined, start 0.
  • get-childitem same ls. used native powershell name, they're interchangeable (ls, dir, , gci aliases get-childitem).
  • %{ } shorthand foreach-object

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo