PowerShell - Rename huge qty of files name

Anonymous
2021-08-22T17:34:10.32+00:00

Hello,
I am discovering PowerShell and I want to rename 7000+ jpg file names by deleting the first 5 characters.
This is what I wrote :
$fichiers = Get-Item "*.jpg"
$Name = $fichiers.name
$New = $Name.substring(5)
Until this point, it works
Get-ChildItem –recurse | Rename-Item –NewName {$_.Name –replace $Name, $New}
This last line does not work ...
Thank you to give me a hand !
Ph Goublin

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andreas Baumgarten 130K Reputation points MVP Volunteer Moderator
    2021-08-22T18:15:03.383+00:00

    Hi anonymous user ,

    please try this:

    Get-ChildItem -Path "Junk/3/*.jpg" | ForEach-Object {
        $Name = ($_.Name).Substring(5)
        $_ | Rename-Item -NewName $Name
    }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 48,036 Reputation points
    2021-08-22T18:59:21.457+00:00

    Yes, those three lines of code do works, but not the way you seem to expect them to.

    Each of the variables $fichiers, $Name, and $New is an array. However, the code in your Replace-Item is using a regular expression and the $Name and $New variables must be scalars (e.g. a string).

    Your code also makes a risky assumption that all the file names (excluding the suffix and the "." that separates the name from the suffix) exceed five characters in length. You run the risk of renaming a file like "123.jpg" to just "pg".

    Another risk is that you use Get-Item first and then Get-ChildItem, assuming that 1) the results will be returned in the same order, and 2) that nothing will ever change between the execution of the two cmdlets. It may be unlikely to happen, but it doesn't hurt to code defensively.

    I'd suggest amending the code in the answer given by @Andreas Baumgarten to:

    Get-ChildItem -Path Junk/3/*.jpg |   
        ForEach-Object {  
            if ($_.BaseName.Length -gt 5){  
                $Name = ($_.Name).Substring(5)  
                $_ | Rename-Item -NewName $Name  
            }  
        }  
    

    Now you'll at least have a usable file name, even if it's only one character long. Which leads to another problem . . . by shortening the file name it's possible that you try to rename a file to one that already exists.

    If you're just now learning PowerShell, I suggest you download this PDF and read at least the 1st half of the book. Windows-PowerShell-4

    0 comments No comments

  3. Andreas Baumgarten 130K Reputation points MVP Volunteer Moderator
    2021-08-22T21:45:22.343+00:00

    Hi anonymous user ,

    to prevent renaming a file with the name of an already existing file, like Rich described above, maybe this helps:

    Get-ChildItem -Path "Junk/3/*.jpg" | ForEach-Object {
        if ($_.BaseName.Length -gt 5) {
            $fileName = $_.Name
            $newName = ($_.Name).Substring(5) 
            $fileCheck = Get-ChildItem -Path "Junk/3/$newName" -ErrorAction SilentlyContinue
            if (!$fileCheck) {
                $_ | Rename-Item -NewName $newName
            }
            else { Write-Output "File $fileName couldn't be renamed because $newName already exists" }
        }
    }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.