Skip to content

Instantly share code, notes, and snippets.

@rodmhgl
Last active September 17, 2015 02:05
Show Gist options
  • Save rodmhgl/19a188c7f48848ed35f2 to your computer and use it in GitHub Desktop.
Save rodmhgl/19a188c7f48848ed35f2 to your computer and use it in GitHub Desktop.
Rename Student Names to IDs
Function Create-HashTable {
param ($filePath)
$mytable = Import-Csv -Path $filePath
$HashTable=@{}
foreach($r in $mytable)
{
$HashTable[$r.Name] = $r.ID
}
return $HashTable
}
$Dir = "C:\temp\Strickland"
$CSVFile = "C:\temp\strickland\input.csv"
$Items = Get-ChildItem -Path $dir -Filter *.jpg
$Lookup = Create-HashTable -filePath $CSVFile
foreach ($item in $items) {
[string]$BaseName = $item.BaseName
$Number = $BaseName.Substring($BaseName.Length - 1, 1)
$BaseName = $BaseName.Substring(0, $BaseName.Length - 1)
$NewName = "$($Lookup[$BaseName])-$number.jpg"
# Changed to Rename-Item (from Move-Item)
# Thanks to a tip from Terry Orsm Wrennall
rename-item -Path $item.FullName -NewName "$NewName" -WhatIf
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment