1

I am trying to rename several hundred TIFF files to follow a new naming convention. I've now hit a situation where I want to remove or change the 4th, 5th or 6th character in the file names.

The characters that I want to change are not necessarily unique in the file name.

For example:

APADA00010.tif

5th character = A
6th character = 0

Is there a way to specify that I want to change every 5th character to a hyphen, or to remove every 6th character?

I did find a similar query by another user but it was for insert instead of replace and so it didn't work:

get-childitem -Path .\ |
Rename-Item -NewName {$_.BaseName.insert(5,"-") + $_.Extension}

I'm very new to Powershell (just started fiddling with it last week) so I'd be very grateful if you included an explanation of how your solution works and what parts of your code mean.

user65218
  • 13
  • 2
  • 1
    Do you want to remove the 5th **AND** 6th character? Or remove the 5th **OR** the 6th character – techguy1029 Jul 17 '19 at 17:30
  • What you have right now works. Insert and replace work fine. Is there some sort of error that happens? If you want to try it a different way, try this: [Replace Part of File Name Powershell](https://stackoverflow.com/questions/22182409/replace-part-of-file-name-powershell) or this: [Powershell - Rename filename by removing the last few characters](https://stackoverflow.com/questions/33255834/powershell-rename-filename-by-removing-the-last-few-characters) – techguy1029 Jul 17 '19 at 17:39
  • @techguy1029 Thanks for replying. Either remove 5th or 6th character. It will depend on the directory that I'm working in and the batch of files I want to edit. – user65218 Jul 17 '19 at 17:50
  • @techguy1029 I did try the above but the problem is that this will insert a hypen into the fifth space rather than replace it: **APADA-00010.tif**. However, I'd like it to replace the 5th character and look like this: **APAD-00010.tif**. – user65218 Jul 17 '19 at 17:55

1 Answers1

2

You can do something like this to remove or replace specific sequenced characters:

$string = "APADA00010.tif"
$string.Remove("5", "1")

$string = "APADA00010.tif"
$string.Remove("4", "1").Insert(4, "-")

Be aware that the index starts counting at 0 rather than 1.

Skuld
  • 123
  • 1
  • 6
  • Thanks, I've given it a go but managed to botch it. Can I ask what Y means in **Remove("X","Y")** ? I pasted the code you wrote and this came out in powershell, which is the format I am looking for. `$string = "APADA00010.tif" >> $string.Remove("5", "1") APADA0010.tif` Does Y mean remove 1 character? – user65218 Jul 17 '19 at 18:35
  • (Comment part 2) I ran it on some files which follow the format APA0001.tif, APA0002.tif, hoping to remove the 4th character: `Get-ChildItem | Where-Object -Property Name -like "APA*" | foreach-object {rename-item $_ -newname $_.name.remove("4","1")}` Unfortunately, I've botched it and the files were renamed to **APA01.tif**, **APA02.tif** etc. – user65218 Jul 17 '19 at 18:40
  • Yes, Y means number of characters in that case. [See the docs](https://learn.microsoft.com/en-us/dotnet/api/system.string.remove?view=netframework-4.8#System_String_Remove_System_Int32_System_Int32_) – Robert Dyjas Jul 17 '19 at 18:41