Powershell: Cannot rename item at '...' does not exist.

Liam Tielemans 1 Reputation point
2021-03-18T18:41:45.127+00:00

Hello,

I have a big folder with loads of folders in them and some folders contain corrupted extensions, they look like the following: .pdf___________... or .xls_________...
I'm trying to change the extensions because when i manually change the extension the files seem to work perfectly fine.

I've tried the following lines:

Get-ChildItem -Recurse -filter ".xls_" | Rename-Item -NewName { [io.path]::ChangeExtension($.name, ".xls") }
Get-ChildItem -Recurse -filter "*.pdf
*" | Rename-Item -NewName { [io.path]::ChangeExtension($_.name, ".pdf") }

but i keep on getting the same error:
Cannot rename item at '...' does not exist.

I've just done some googling trying to match the pieces of the puzzle. I am not familiar with using Powershell.
Can someone help me find a solution?

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

5 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-03-18T20:01:17.587+00:00

    Hi @Liam Tielemans ,

    please try this:

    # Modify path  
    $files = Get-ChildItem -Path Junk -filter "*.pdf*" -recurse  
    foreach ($file in $files)  
    {  
        $filename = $file.Name  
        $ext = $filename.Substring($filename.Length - 4)  
        $newfilename = $filename.Replace("$ext","pdf")  
        $file | Rename-Item -NewName $newfilename  
    }  
    

    ----------

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

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-03-18T18:51:46.897+00:00

    Hi @Liam Tielemans ,

    maybe this helps. At least it looks like it's exactly like what you are looking for:
    https://devblogs.microsoft.com/scripting/use-powershell-to-rename-files-in-bulk/

    # From the website linked above  
      
    Get-ChildItem -Filter “*current*” -Recurse | Rename-Item -NewName {$_.name -replace ‘current’,’old’ }  
    

    ----------

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

    Regards
    Andreas Baumgarten


  3. Liam Tielemans 1 Reputation point
    2021-03-18T20:15:39.717+00:00

    @Andreas Baumgarten ,

    This script does not do the job either sadly.

    when i try:
    Get-ChildItem -recurse -filter .pdf_

    it perfectly runs and shows all the corrupted PDF-files with as extension .pdf_______________________________...

    Once i try to add the pipeline:
    | Rename-Item -NewName { [io.path]::ChangeExtension($_.name, ".pdf") }

    I get the error:
    Rename-Item : Cannot rename because item at 'Microsoft.PowerShell.Core\FileSystem::C:\Contractmanagement\1941ZO185_bijlagen.pdf___________________________________________________________________________________________________________' does not exist.
    At line:1 char:41

    • ... er .pdf | Rename-Item -NewName { [io.path]::ChangeExtension($_.name ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    • FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

    Is there any possible fix?

    0 comments No comments

  4. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-03-18T20:42:56.967+00:00

    @Liam Tielemans

    Just use this script the way it is and it should do the job. Just modify the path in the first line to your needs.

    Get-ChildItem -Path Junk -filter "*.pdf*" -recurse | ForEach-Object {  
            $ext = ($_.Name).split('.')[1]  
            $_ | Rename-Item -NewName ($_.name).Replace("$ext","pdf")}  
    

    It's working here like required. Maybe it's not the shortest solution but it works.

    If you still like to go with your approach with the [io.path]::ChangeExtension($_.name, ".pdf") it's fine, enjoy ;-)

    ----------

    (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

  5. Rich Matheisen 47,901 Reputation points
    2021-03-18T21:19:48.707+00:00

    The [io.path]'s ChangeExtension method require the complete path to the file, not just the file's name.

    Get-ChildItem c:\junk -filter *.pdf_ | 
        Rename-Item -NewName { [io.path]::ChangeExtension($_.fullname, ".pdf") }
    

Your answer

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