Good Morning,
I have hundreds of files in subfolders with a root directory of - "C:\Layout\PDF" The files are 7 digits with "_Email". (ie 9991234_Email.pdf) How can I get a PowerShell Script to change the file names within all of the subdirectories to be 7 digits with "_Proof" Instead? (ie 9991234_Proof.pdf)
Any assistance would be greatly appreciated!
Thank you!
$inPath = 'C:\Layout\PDF\'
$outPathRoot = 'C:\Layout\PDF\'
Get-ChildItem -Recurse -LiteralPath $inPath -Filter *_Email.pdf | ForEach-Object {
$targetDir = New-Item -Type Directory -Force ($outPathRoot)
$EmailFiles = Get-ChildItem | Where-Object {$_.Name -like "_Email*"}
ForEach($File in $EmailFiles)
{
$newname = ([String]$File).Replace("_Email","_Proof")
Rename-item -Path $File $newname
}
CodePudding user response:
Rename-Item
's parameter -NewName
accepts a scriptblock ({..}
) that allows for robust renaming. So, by referencing the current objects' in the pipeline name property ($_.Name
), you can call on .Replace()
to replace Email
, with Proof
.
Get-ChildItem -Path 'C:\Layout\PDF\' -Filter '*_Email.pdf' -Recurse |
Rename-Item -NewName { $_.Name.Replace('Email','Proof') } -WhatIf
Remove the -WhatIf
common/safety parameter when you've dictated those are the results you're after.