0

Thanks in advance, i am totally new to PowerShell but i know it can rename files etc. so i'm hoping with your help it can be done with minimal fuss... I have been asked to rename circa 200 folders by adding in a 2nd reference to the folder name.

The current folder name example "11111_a1" and I want to rename it "11111_a1 12345678". I have created a CSV file with two columns, the first column has a partial match to the existing folder name being "20519" the second column has the additional reference that needs to be added "30534400"

The list is located in a Temp folder "C:\temp\FolderNameList.csv"

The target folders are located "O:\folder1\folder 2\0 - Sites"

I haven't tried anything yet but my searches have not been successful.

PeterC
  • 1
  • 1
  • You cannot rename a folder if it is being used by an app (including file explorer). You can always use the cmd command ren (or rename). – jdweng Jan 30 '23 at 17:21
  • Search for: [PowerShell rename csv](https://stackoverflow.com/search?q=Powershell+rename+csv). Also check [how to ask](https://stackoverflow.com/help/how-to-ask) – iRon Jan 30 '23 at 17:25

1 Answers1

0
$BaseFolder = 'O:\folder\whatever'

# An array, one item for each data row in your CSV
$Rows = Import-Csv 'C:\temp\FolderNameList.csv'

foreach ($Row in $Rows)
{
    $Name = $Row.Col1
    # replace 'Col1' with whatever the column header is
    $Path = Join-Path $BaseFolder $Name

    $Suffix = $Row.Col2  # or whatever

    $NewName = "$Name $Suffix"
    Rename-Item $Path $NewName
}

That should fix you.

However, before you run the script on a lot of files, change foreach ($Row in $Rows) to foreach ($Row in $Rows[0]). Then it will only operate on the first file. Check it does what you expect!

If it works, take the [0] back out (and delete the first row of the CSV if you don't want error text, since that first row has now been done).

FSCKur
  • 920
  • 7
  • 16