locked
Renaming Multiple Files RRS feed

  • Question

  • I have multiple files named like this "firstname_lastname_48.jpg"

    I need them to be renamed to look like this "firstname lastname.jpg"

    I can remove the numbers using  Dir | Rename-Item –NewName { $_.name –replace "[0-9]","" }

    I can replace the underscores with a space using this Dir | Rename-Item –NewName { $_.name –replace "_","" }

    My problem is that second underscore gets replaced with a space and I haven't been able to figure out how to remove that second space or how to simply remove the second underscore while replacing the first one with a space. 

    Thanks for any help.



    • Edited by Without Wax Monday, October 23, 2017 4:20 PM
    Monday, October 23, 2017 4:18 PM

Answers

  • ($_.name –replace '_\d+') -replace '_',' '


    \_(ツ)_/


    • Edited by jrv Monday, October 23, 2017 4:29 PM
    • Marked as answer by Without Wax Monday, October 23, 2017 4:45 PM
    Monday, October 23, 2017 4:28 PM

All replies

  • ($_.name –replace '_\d+') -replace '_',' '


    \_(ツ)_/


    • Edited by jrv Monday, October 23, 2017 4:29 PM
    • Marked as answer by Without Wax Monday, October 23, 2017 4:45 PM
    Monday, October 23, 2017 4:28 PM
  • That took the place of both of my commands, I have no idea how its working but it worked, thanks! I have so much to learn about powershell.
    Monday, October 23, 2017 4:45 PM
  • Placing parens around the first part generates a string that is passed to the second replace and the overall result is returned.

    \_(ツ)_/

    Monday, October 23, 2017 5:16 PM