question

LiamTielemans-8872 avatar image
0 Votes"
LiamTielemans-8872 asked AndreasBaumgarten commented

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

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered LiamTielemans-8872 commented

Hi @LiamTielemans-8872 ,

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AndreasBaumgarten ,

I tested it this way.
In my situation i would have to do it as folllowed:
Get-ChildItem -filter ".pdf" -recurse | Rename-item { $_.name -replace '.pdf*','.pdf' }

by using this command i get the following error:
Rename-Item : A positional parameter cannot be found that accepts argument ' $.name -replace '.pdf*','.pdf' '.
At line:1 char:43
+ ... r ".pdf" -recurse | Rename-item { $
.name -replace '.pdf*','.pdf' }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RenameItemCommand

What am i doing wrong?

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @LiamTielemans-8872 ,

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LiamTielemans-8872 avatar image
0 Votes"
LiamTielemans-8872 answered LiamTielemans-8872 published

@AndreasBaumgarten ,

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?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

@LiamTielemans-8872

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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered AndreasBaumgarten commented

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") }
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RichMatheisen-8856 ,

is there any advantage of using [io.path] for this?


Kind regards
Andreas Baumgarten

0 Votes 0 ·

Not really. I was just pointing out why his original code didn't work.

1 Vote 1 ·

Thank you.


Kind regards
Andreas Baumgarten

0 Votes 0 ·