1

The problem is I need to rename files.

Original name:

Patient Data - John Smith - John Varner

I have tried this code:

Get-ChildItem -Recurse |
    Rename-Item -NewName { $_.Name -replace "John ","Dr. " }

But the problem is it also replaces the other name to Dr. that looks like this:

Patient Data - Dr. Smith - Dr. Varner

But I need it to look like this:

Patient Data - Dr. Smith - John Varner

Oh just to let you know I'm a noob when it comes to Powershell I just copy codes from the internet. So I dont have any idea what code to use. Please help me with the exact code I'm gonna use. Thank you so much

  • Are all file names in this format? : Patient Data - Dr Name - Another Name ? – jrider Sep 17 '19 at 12:03
  • Hi yes. they are all in the same format Patient Data - Dr. Name - Name I just want 1 occurrence when renaming. I dont want powershell to rename also the patient's name. – Gabriel Balenton Sep 17 '19 at 12:15
  • It looks like this question has been closed. This is not quite a duplicate, you have a little more going on than what was in the dupe. But this should work for you: `Get-ChildItem -Recurse | % { $drName = (($_ -split "-")[1]).Trim() $nameReplace = $drName -replace "$(($drName -split " ")[0])", "Dr." Rename-Item -NewName $nameReplace }` – jrider Sep 17 '19 at 12:30
  • Hi I've tried your code. Nothing happens The thing is, The file name starts with a name not Dr. Name - Patient Name Example John Smith - John Doe should be like this: Dr. Smith - John Doe Is there any code that I could use to just have one occurence of renaming for the whole file name. get-childitem -recurse | rename-item -newname { $_.name -replace "John ","Dr." } that is the code I am using – Gabriel Balenton Sep 17 '19 at 12:38
  • @jrider I fail to see how this question would not be a duplicate, since in both cases only the first occurrence of a particular substring should be replaced. – Ansgar Wiechers Sep 17 '19 at 12:39
  • @AnsgarWiechers The problem is I dont know how to apply the answered question to my coding. I'm sorry I'm a noob when it comes to this – Gabriel Balenton Sep 17 '19 at 12:43
  • @AnsgarWiechers Respectfully, the answer is to replace a static word in a string. I think that is a big difference for a beginner. With that point, if you still see it as a dupe, I can respect that. – jrider Sep 17 '19 at 12:46
  • It's `-replace '(.*?)SEARCH_TERM(.*)', '${1}REPLACEMENT_TERM${2}'`, so in your case: `-replace '(.*?)John(.*)', '${1}Dr.${2}'`. – Ansgar Wiechers Sep 17 '19 at 12:47
  • 1
    If you want to replace varying names after the first hyphen, then the code would have to look somewhat like this: `-replace '(.*?)- \w+(.*)', '${1}- Dr.${2}'`. – Ansgar Wiechers Sep 17 '19 at 12:49
  • 1
    @jrider Yes, I still think it's a duplicate, since the only difference is in what substring to match. My previous comment should address that. – Ansgar Wiechers Sep 17 '19 at 12:51
  • @AnsgarWiechers I can agree with your point, it should be a duplicate. Sorry for the confusion... Next time, more coffee first. – jrider Sep 17 '19 at 12:52
  • I've tried this: get-childitem -recurse | rename-item -newname $_.name -replace '(.*?)John(.*)', '${1}Dr.${2}' and this: -replace '(.*?)John(.*)', '${1}Dr.${2}' Still not working or does not do anything. Also tried this: -replace '(.*?)- \w+(.*)', '${1}- Dr.${2}' – Gabriel Balenton Sep 17 '19 at 12:58
  • The argument to the parameter `-NewName` needs to be in curly brackets, otherwise it won't work. – Ansgar Wiechers Sep 17 '19 at 13:07
  • `get-childitem -recurse | rename-item -newname { $_.name -replace '(.*?)- \w+(.*)', '${1}- Dr.${2} }` Is this correct? – Gabriel Balenton Sep 17 '19 at 13:17
  • @GabrielBalenton There's a single quote missing after `${2}`, but other than that yes. – Ansgar Wiechers Sep 17 '19 at 13:18
  • @AnsgarWiechers OMG thank you soooo much for the help!! Now my only problem is how do I incorporate this code without the extra spaces. Because after the "Dr. " there's an extra space. – Gabriel Balenton Sep 17 '19 at 13:25

0 Answers0