none
Need to rename files based on specific string within the current name RRS feed

  • Question

  • I have a large list of image files that look like:

    78103OW-1-newcolor.jpg 

    78103OW-1-sandind.jpg 

    78103OW-2-sandind.jpg 

    78103OW-3-sandind.jpg 

    I want to move the color to the front of the string (and remove from the end).  Is there an easy way to do this?

    I was initially thinking to move each individual color doing something like:

    Get-ChildItem -Filter"*-sandind" | Rename-Item -NewName {"sandind" + $_.Name.Substring(0,$_.BaseName.length-8) + $_.Extension}

    But that doesn't work either - nothing happens.

    Any help would be appreciated!!

    Thursday, November 14, 2019 5:37 PM

All replies

  • Pipe the "get-childitem -filter *.jpg" output to a foreach loop. In the loop do $_.basename.split('-'). That will give you an array of the 3 portions of the name. Then assemble the newname as you wish. Maybe like this.

    $newname = "{0}-{1}-{2}.jpg" -f $array[2], $array[1], $array[0]

    Then rename $_ to the $newname.

    Note, that assumes that all file names are in the 78103OW-3-sandind.jpg format. You should check the $array.count value to verify that there are exactly 3 items. 


    • Edited by MotoX80 Thursday, November 14, 2019 6:17 PM
    Thursday, November 14, 2019 6:15 PM
  • Ok...so, since they are all .jpg, I don't really need the filter, right?

    Secondly, while I know some basic functions, and I understand what your suggestion is implying (i.e. create 3 arrays and reorder the arrays in the rename), I do not know the proper syntax to write what you are saying.

    Thursday, November 14, 2019 6:22 PM
  • No, it one array with 3 items. If the files are all supposed to be .jpg, then you don't need the filter. 

    There are plenty of examples on the internet. 

    https://www.poftut.com/powershell-foreach-loop-statement-tutorial-with-examples/

    https://www.poftut.com/powershell-split-string-operation-examples/

    Thursday, November 14, 2019 6:46 PM
  • Your original code would work if you used the correct filter:

    Get-ChildItem -Filter"*-sandind.jpg" | 
        Rename-Item -NewName {
                 'sandind + $_.Name.Substring(0,$_.BaseName.length-8) + $_.Extension
                   }


    \_(ツ)_/

    Thursday, November 14, 2019 6:54 PM
    Moderator
  • So, I have the following now (I used the "Write-Output" to make sure that I was going to get the right thing.  But I cannot use Rename-Item as it is asking for a path and the path is in use.

    Can't i just rewrite the filename or do i have to specify to a new folder?

    Get-ChildItem -Filter "*.jpg" |
    foreach { 
      $nameArray = $_.BaseName.Split("-")
      $newName = $nameArray[2]+$nameArray[0]+"_"+$nameArray[1] 
      Write-Output $newName
      Rename-Item -NewName ("$newName")
    }

    I get the following:

    cmdlet Rename-Item at command pipeline position 1
    Supply values for the following parameters:
    Path: 


    • Edited by josh921 Thursday, November 14, 2019 7:36 PM
    Thursday, November 14, 2019 7:30 PM
  • Shouldn't you be telling Rename-Item what file you're renaming????

    $_.Fullname should work. :-)


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, November 14, 2019 7:41 PM
  • I'm obviously a complete idiot.  Can you please help me with the syntax?

    I assumed:

    Get-ChildItem -Filter "*.jpg" |
    foreach { 
      $nameArray = $_.BaseName.Split("-")
      $newName = $nameArray[2]+$nameArray[0]+"_"+$nameArray[1] 
      Write-Output $newName
      Rename-Item -NewName $_.Fullname $newName
    }

    Thursday, November 14, 2019 7:50 PM
  • Rename-Item -Path $_.Fullname -NewName $newname

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, November 14, 2019 7:59 PM
  • Yes.  Thank you.  Just got that.

    Plus needed to add extension to the new name since we used Fullname.

    Get-ChildItem -Filter "*.jpg" |
    foreach { 
      $nameArray = $_.BaseName.Split("-")
      $newName = $nameArray[2]+$nameArray[0]+"_"+$nameArray[1]+$_.Extension
      Write-Output $newName
      Rename-Item $_.Fullname -NewName $newName
    }

    Thanks very much for your help!!!

    Thursday, November 14, 2019 8:05 PM
  • Here is my take on it:


    Get-ChildItem -Filter *.jpg | Rename-Item -NewName {
      $part1,$part2,$part3 = $_.basename -split '-'
      "$part3-$part1-$part2.jpg" } -whatif

    What if: Performing the operation "Rename File" on target "Item: C:\users\js\foo\78103OW-1-newcolor.jpg Destination: C:\users\js\foo\newcolor-78103OW-1.jpg".
    What if: Performing the operation "Rename File" on target "Item: C:\users\js\foo\78103OW-1-sandind.jpg Destination: C:\users\js\foo\sandind-78103OW-1.jpg".
    What if: Performing the operation "Rename File" on target "Item: C:\users\js\foo\78103OW-2-sandind.jpg Destination: C:\users\js\foo\sandind-78103OW-2.jpg".
    What if: Performing the operation "Rename File" on target "Item: C:\users\js\foo\78103OW-3-sandind.jpg Destination: C:\users\js\foo\sandind-78103OW-3.jpg".

    Thursday, November 14, 2019 9:20 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, December 6, 2019 9:02 AM
    Moderator