0
votes

I have been trying to use Powershell to Rename folder directories in bulk.

I have many folders that are formatted ab_xxxxx_xxxxx_xxx (where x is a number) I would like it to be renamed to ab_xxxxxxx_xxxxxxx_100xxx.

I have trying using Rename Item with the Replace command and it adds the 100 after every _

Ex. Get-ChildItem -Path ab___* -Directory | ForEach-Object -Process { Rename-Item -Path $.Name -NewName ($.Name -replace "_", "_100") }

I also tried using the * wild card but it just added that to the new folder name.

Any Help would be greatly appreciated

2

2 Answers

4
votes

I hope this could help

Get-ChildItem $path "ab_*" | where {$_.PSIsContainer} | ForEach-Object { Rename-Item $_.name ($_.name.insert(($_.name.LastIndexOf("_")+1),"100")) }

This powershell command will rename folder named "ab_67890_12345_323" to "ab_67890_12345_100323". I hope this what you want.

1
votes

Is the rule, that the segments are always separated by an underscore and "100" has to be added at the beginning of the last segment? If so, you could do it like so:

$x = "ab_839232_28921_1891"
$y = $x -split '_'
$y[-1] = "100" + $y[-1]
$x = $y -join '_'

$x
ab_839232_28921_1001891