1

I have a foler which contains a few thousand PDFs named like XXX_CON.100.1.pdf, XXX_CON.101.1.pdf etc. I just want to remove the characters 'XXX_' from all filenames but I simply cannot get this to work. I have tried the following commands:

Get-ChildItem | Rename-Item -NewName {$.Name -replace "XXX", "" }

Get-ChildItem -Filter 'XXX_*' | Rename-Item -NewName {$.name -replace 'XXX','' }

Get-ChildItem *.pdf | Rename-Item -NewName {$.name.substring(4,$.BaseName.length-4) + $_.Extension}

Get-ChildItem .pdf | Rename-Item -NewName { $_.Name -replace 'XXX_.pdf','*.pdf' }

ls | Where {$.FullName -match '(XXX)'} | Rename-Item -NewName { $_ -replace ("XXX_","") }

Every single command gives me the Powershell error "Rename-Item : The parameter is incorrect."

e.g:

Rename-Item : The parameter is incorrect. At line:1 char:23 + Get-ChildItem *.pdf | Rename-Item -NewName {$.name.substring(4,$.BaseName.leng ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (E:\TempPE\pdf\XXX_CON.99.1.pdf:String) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

The crazy thing is if I run these with a -Whatif at the end they all appear to work... but when I take that parameter off they all fail again.

Anyone know what the issue is here? TBH I googled to get the examples above but I'm sure they should work yet none of them do and I'm not enough of a Powershell guru to know how to get this to work. It seems it just doesn't like Rename-Item no matter what I try which is really strange. Could it be because all the files have more than dot in them?

nzmike
  • 578
  • 1
  • 7
  • 25
  • OK, so I think is a system issue to do with renaming a file starting with 'CON.' because if I try to rename the files individually I get the error 'The specified device name is invalid'. I assume is some ancient windows thing to do with output device of Console. I don't know how to get around this - they have to be named CON.xxx.x.pdf because they are loaded into an accounting system that needs to search for the exact filename... anyone got any ideas? – nzmike May 07 '20 at 05:40
  • 1
    `get-childitem xxx_* | rename-item -NewName { $_.name -replace 'xxx_','' }` works for me with PS 5.1 and the same `xxx_con_*.*` filenames. – dxiv May 07 '20 at 05:42
  • Thanks dxiv, but you are adding an extra _ at the end of the 'CON' which does not work for me - they have to be called CON.xxx.x.pdf not CON_.xxx.x.pdf – nzmike May 07 '20 at 05:45
  • This issue can be replicated - in any folder try to create a new text file called 'CON.111.1.txt' and Windows will give you the 'specified device name is invalid' error. – nzmike May 07 '20 at 05:48
  • Right, it was working because of that extra `_`. Without it it fails, but at least I am getting a (slightly) more intelligible error "*Cannot create a file when that file exists ... WriteError*". – dxiv May 07 '20 at 05:51
  • Well I'm floored TBH. This is one of the wierdest windows issues I've ever hit and I've been using it snce Windows 3.1! Seems I have a real issue because as this page points out there are a whole pile of things you can't name a file or folder: https://answers.microsoft.com/en-us/windows/forum/windows_7-files/why-cant-i-create-a-folder-with-name/23c86662-4988-4c7d-9c2d-3e33d4413de3 – nzmike May 07 '20 at 05:53
  • Funny thing is that I can copy such a file at the cmd prompt with `copy "\\?\%cd%\xxx_con.a.b" "\\?\%cd%\con.a.b"` but it seems I can't just rename it. – dxiv May 07 '20 at 06:01
  • P.S. I'll take some of that back, the file can be created that way but programs will get confused when trying to access it. – dxiv May 07 '20 at 06:06
  • More P.S. What makes this even sillier is that by Windows rules a complete filename of `CON.A.B` has an extension of `B` and a name of `CON.A` so it doesn't even match the [reserved name](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file) `CON`. Someone at MS must have misunderstood their own rules. – dxiv May 07 '20 at 06:18

0 Answers0