Last active
September 17, 2015 02:05
-
-
Save rodmhgl/19a188c7f48848ed35f2 to your computer and use it in GitHub Desktop.
Rename Student Names to IDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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