-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRename_ImageFile.v2.ps1
More file actions
115 lines (69 loc) · 2.78 KB
/
Rename_ImageFile.v2.ps1
File metadata and controls
115 lines (69 loc) · 2.78 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<#
Purpose: the script will rename
- ".jpeg" to ".jpg"
- based on list of pubNum, will rename the file
- for file with white space, will remove them
Create Date: 6/21/2017
Created By: Tommy Yuen
#>
<#
Section for the info of image path, pubNum list, to indicate whether filename has space
#>
# file path of the images on the network
$imageFilePath = 'c:\temp'
# where is the list of pubnum, NOT REQUIRE if vendor is CID, can leave the file as it is
$pubNumList = 'c:\temp\a.csv'
# "$" need to be infront of true or false
# $true or $false
$renameByPubNumList = $true
<#
Code to rename filename
#>
# rename image filename based on the provide pubnum list
if ($renameByPubNumList) {
# list of pubnum from upload sheet
$fileList = import-csv $pubNumList
write-host "folder: $imageFilePath"
# loop through the files on the network path
get-childitem $imageFilePath | ForEach-Object {
$fileName = $_.FullName
# for each file in the network path, check to see if the filename matches the pubNum
foreach ($file in $fileList) {
# if the pubnum matches, then it will rename it to pubnum.jpg
if ($fileName -match $file.PubNum ) {
$newFileName = $file.PubNum + '.jpg'
write-host 'current: ' $fileName
write-host 'new: ' "$imageFilePath\$newFileName"
write-host ""
Rename-Item $fileName "$imageFilePath\$newFileName"
}
}
}
}
# the code will strip off anything after finding first 'hyphen' or 'space' of the image filename
# the code is expect the first 'hyphen' is due name of the server
else {
# loop through the files on the network path
write-host "folder: $imageFilePath"
get-childitem $imageFilePath | ForEach-Object {
$fileName = Split-Path -Leaf $_.FullName
$path = Split-Path -parent $_.FullName
# split by hyphen
$splitFileName = $fileName -split '-'
$newFileName = $splitFileName[0]
$newFileName = $newFileName + '.jpg'
$newFileName = $newFileName -REPLACE '.jpg.jpg', '.jpg'
# check of white space
if ($newFileName -match "\s") {
# split by hyphen
$splitFileName = $newFileName -split "\s"
$newFileName = $splitFileName[0]
$newFileName = $newFileName + '.jpg'
$newFileName = $newFileName -REPLACE '.jpg.jpg', '.jpg'
}
write-host "current: $fileName"
write-host "new: $newFileName"
write-host ""
Rename-Item "$path\$fileName" "$path\$newFileName"
}
}