user2725402
user2725402

Reputation: 4649

Bulk Rename of Files - read old and new name from list

I have a big list of files that I need to rename. I need to somehow do this by reading the CURRENT NAME and NEW NAME from a file (currently in Excel but can obviously change to CSV or whatever).

To explain this better, some current names have account numbers and some have application numbers and some have ID number and some have names and surnames.

Using UDFs and formulas in Excel I managed to extract enough data from each filename to match it to our DB and now I have each person's ID number - I need to name the file to the ID number so we can upload it to our system and it can properly be indexed.

So I'll need PS to read the file_list.txt, then find the file based on the CURRENT NAME in the list, and rename it to the NEW NAME in the list.

I know how to bulk rename files by just assigning a standard name and sequence numbers (many such posts on this site), but have no idea how to read the names from the file.

Upvotes: 1

Views: 3184

Answers (3)

henrycarteruk
henrycarteruk

Reputation: 13227

Using a CSV with two columns for Path and NewName:

Path,NewName
"C:\folder\ABC123.txt","ID001.txt"
"C:\another_folder\RandomFile001.txt","ID002.txt"

(the column names can be anything so long as you use matching names in Powershell)

You can use a foreach loop to go through the items in the CSV and rename them:

Import-Csv "C:\folder\file_list.csv" | foreach { Rename-Item -Path $_.Path -NewName $_.NewName }

Upvotes: 4

Zach Olinske
Zach Olinske

Reputation: 557

I have been looking into this as well, but I always like to make things better. What this short sciprt does is grabs all the file in a directory and sub-directories and renames thems.

Create global variables that can be used for all the functions.

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}

Create a list of Files/Directories that we want to rename. We then let PowerShell open up Excel and allow you to edit the CSV. Once closed the script will start again.

Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}

Now with the edited List we can now setup what we want to rename the structure.

Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}

Now with the Rename list completed we can now rename the files we wanted.

Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}

Call the functions

Function GOGO{
Variables
CreateList
CreateRenameList
Rename
}
GOGO

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 58991

If you use the Get-ChildItem cmdlet to retrieve your files, you will find the filename within the Name property:

Get-ChildItem | Rename-Item -NewName { # use $_.Name to get the new name from your list }

You can read the file_list.txt using the Get-Content cmdlet or Import-CSV if its a CSV file.

Upvotes: 0

Related Questions