Avatar of rakkad
rakkad
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Powershell to replace/rename multiple files in a directory

Hi

I have multiple files that are created in a directory called c:\cogtest which are:-

Network Detail Report - Burst-en-gb-xxx.pdf
Network Detail Report - Burst-en-gb.xyz.pdf
and so on...

I need to strip out the 'Network Detail Report - Burst-en-gb' in all the files that it generates, leaving the filename.pdf part only

e.g.

xxx.pdf
xyz.pdf

and so on...

Thanks
Powershell

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Dent

Plus one for WhatIf, sensible plan :)
oBdA

Weeeeeeell ....
Chris, sorry, but just to avoid confusion in case somebody tries to run your script, I don't entirely agree with "Both examples will work to rename items., as on closer inspection, I see two errors in your script.
I'll spoil it for you:
* You're not using a ForEach, so there's no "$_" variable
* You wrapped the new name in curly brackets (turning it into a script block) instead of round ones
Chris Dent

It's not often you're less then spotlessly perfect oBdA, but on this occasion I'm afraid I've caught you out.

When processing a pipeline commands which fill parameters from a pipeline can be fed alternate values using the technique above. It *only* works because of this:
Get-Help Rename-Item

-Path <string>

    Accept pipeline input?       true (ByValue, ByPropertyName)

Open in new window

In this situation the value supplied for a parameter in the next command in the pipeline must be described by a script block. The script block will be invoked for each iteration of the output pipeline.

Test code to play with:
New-Item C:\Temp\TestSet -ItemType Directory -Force
1..10 | ForEach-Object { New-Item "C:\Temp\TestSet\$_.txt" }
Get-ChildItem C:\Temp\TestSet -Filter *.txt | Rename-Item -NewName { "$($_.Name).new.txt" }	

Open in new window

There is no problem with the answer I've given above.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
oBdA

OK, my bad, sorry, I retract that statement, your script works just fine,
When I first tested it, it showed the expected error, so something must have been wrong, because now I can't reproduce the error anymore, of course.