-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenameImages.ps1
74 lines (67 loc) · 3.82 KB
/
renameImages.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#Author: Jerrod LaVassor
#Description: This script checks a csv file for image names, finds the image in the specified directory, and renames it to a new directory with the name based on the same CSV file
#generic var's
$results = @()
$datetime = Get-date -Format MMddyyyyHHmmss
#file/folder paths
$dirWithImages = (Read-Host -Prompt "Enter the directory or UNC path that contains the source images").trimend('\') #where the current images are
$dirWithRenamedImages = (Read-Host -Prompt "Enter the directory or UNC path of where you want to save the renamed images (not the same as the source..)").trimend('\') #where to put the renamed ones, we don't want to delete/overwrite the existing ones. Make this a different directory than the existing images...
$imageFilePath = Read-Host -Prompt "Enter the full path to the file that contains your CSV of the new and old image names" #csv file location
$outputLogFileName = "$dirWithRenamedImages\log\ImageRenameLog$datetime.csv"
if (test-path $outputLogFileName) {
remove-item $outputLogFileName
}
<# THis is for testing with hardcoded paths.
$dirWithImages = "D:\work\NDC\images" #where the current images are
$dirWithRenamedImages = "D:\work\NDC\images\renamed" #where to put the renamed ones, we don't want to delete/overwrite the existing ones. Make this a different directory than the existing images...
$imageFilePath = "D:\work\NDC\images\xref\ImageTest1.csv" #csv file location
$outputLogFileName = "D:\work\NDC\images\log\ImageRenameLog$datetime.csv"
if (test-path $outputLogFileName) {
remove-item $outputLogFileName
}
#>
#import the CSV that contains the image name and new image name. must include column headers named: ImageFileName & ProposedNewName
$Imagexref = Import-Csv -Path $imageFilePath
#iterate each row in the csv, find the image name from the file. if we can find the image, we try to make a copy of it with a new name in the dir defined
ForEach ($Image in $Imagexref) {
#local vars
$currentImageName = $Image.ImageFileName
$newImageName = $Image.ProposedNewName
Write-Host "Looking for image $currentImageName"
if (!(test-path -Path "$dirWithImages\$currentImageName")) {
Write-Host "$currentImageName - Image Not Found in source directory"
$details = @{
imagename = $currentImageName
ImagePath = "$currentImageName\$dirWithImages"
message = "Image Not Found"
}
$results += New-Object PSObject -Property $details
}
else {
Write-Host "Found image $currentImageName. Renaming..."
try {
Copy-Item -Path "$dirWithImages\$currentImageName" -Destination "$dirWithRenamedImages\$newImageName"
$details = @{
imagename = $currentImageName
ImagePath = "$dirWithImages\$currentImageName"
message = "New Image: $dirWithRenamedImages\$newImageName"
}
$results += New-Object PSObject -Property $details
Write-Host "Successfully renamed."
}
catch {
$details = @{
imagename = $currentImageName
ImagePath = "$currentImageName\$dirWithImages"
message = "Something went wrong"
}
$results += New-Object PSObject -Property $details
Write-Host "Something went wrong with renaming/saving $currentImageName"
}
}
}
if (!(test-path -Path "$dirWithRenamedImages\log")){
New-Item -Path "$dirWithRenamedImages\log" -ItemType Directory
}
$results | Export-Csv -Path $outputLogFileName -NoTypeInformation
$response = Read-Host -Prompt "Renaming is complete. You can review the log file here: $outputLogFileName. Press any key to close this window..."