forked from iBug/vbsGadgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoManager.vbs
213 lines (189 loc) · 6.03 KB
/
PhotoManager.vbs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
' Author: iBug - https://stackoverflow.com/user/5958455/ibug
Option Explicit
Const ProgramName = "iBug PhotoManager"
Const Version = "1.0"
Const VersionNumber = 2
Dim Title : Title = ProgramName & " v" & Version & "." & VersionNumber
Dim Shell, Fso
Set Shell = CreateObject("WScript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")
Dim I_ConfigFileName, I_ValidExtensions, I_NumberLength, I_TargetPattern, I_LastNumber
I_ConfigFileName = "photo.ini"
I_ValidExtensions = Array("jpg", "raw", "jpeg", "jpe")
I_NumberLength = 8
I_TargetPattern = "IMG_%"
I_LastNumber = -1
Dim ConfigFile
Dim C_In, C_Out, C_Count, F_In, F_Out
''''''''''''''''''''''''''''''
GetConfig I_ConfigFileName
SearchFolder F_In, F_Out
''''''''''''''''''''''''''''''
Sub CreateDefaultConfig(ByVal ConfigFileName)
Dim ConfigFile
Set ConfigFile = Fso.OpenTextFile(ConfigFileName, 2, True)
ConfigFile.WriteLine "In="
ConfigFile.WriteLine "Out="
ConfigFile.WriteLine "Extension=jpg,jpeg,raw"
ConfigFile.WriteLine "Pattern=IMG_%"
ConfigFile.Close
End Sub
Sub ReadConfig(ConfigFileName)
Dim ConfigFile, Config
Set ConfigFile = Fso.OpenTextFile(ConfigFileName, 1, False)
Do Until ConfigFile.AtEndOfStream
Config = Split(ConfigFile.ReadLine(), "=", 2)
If UBound(Config) <> 1 Then
' Do nothing
ElseIf Mid(LTrim(Config(0)), 1, 1) <> ";" Then
Select Case LCase(Trim(Config(0)))
Case "in"
C_In = StripSlashes(Trim(Config(1)))
Case "out"
C_Out = StripSlashes(Trim(Config(1)))
Case "extension"
I_ValidExtensions = Split(Config(1), ",")
Dim i
For i = 0 To UBound(I_ValidExtensions)
I_ValidExtensions(i) = Trim(I_ValidExtensions(i))
Next
Case "pattern"
If UBound(Split(Config(1), "%")) = 1 Then
I_TargetPattern = Config(1)
Else
MsgBox "Invalid pattern!", vbExclamation, Title
End If
Case Else
MsgBox "Unknown option """ & Config(0) & """", vbExclamation, Title
' And... Do nothing
End Select
End If
Loop
ConfigFile.Close
End Sub
Function ValidateConfig()
ValidateConfig = True
If Not Fso.FolderExists(C_In) Then
MsgBox "The source folder """ & C_In & """ does not exist!", 16, Title
ValidateConfig = False
Exit Function
End If
If Not Fso.FolderExists(C_Out) Then
MsgBox "The target folder """ & C_Out & """ does not exist!", 16, Title
ValidateConfig = False
Exit Function
End If
End Function
Sub GetExistingConfig(ByVal ConfigFileName, ByVal DeleteAfter)
' Read and apply the config
ReadConfig ConfigFileName
If DeleteAfter Then
Fso.DeleteFile ConfigFileName
End If
' Validate config
If Not ValidateConfig Then
WScript.Quit 1
End If
Set F_In = Fso.GetFolder(C_In)
Set F_Out = Fso.GetFolder(C_Out)
End Sub
Sub GetConfig(ByVal ConfigFileName)
If Not Fso.FileExists(ConfigFileName) Then
CreateDefaultConfig ConfigFileName
Shell.Run ConfigFileName,, True
End If
GetExistingConfig ConfigFileName, False
C_Count = 0
End Sub
Sub PatchFile(ByRef File)
If LCase(Fso.GetExtensionName(File.Path)) = "jpeg" Then
File.Move GetFilenameWithoutExtension(File.Path) & ".jpg"
End If
End Sub
Sub MoveFile(ByRef File, ByRef Target)
PatchFile File
File.Move Target.Path & "\" & FindNextAvailableName(F_Out) & "." & Fso.GetExtensionName(File.Path)
End Sub
Sub SearchFolder(ByRef Folder, ByRef Target)
Dim Item
For Each Item In Folder.Files
If InArray(I_ValidExtensions, LCase(Fso.GetExtensionName(Item.Path))) Then
MoveFile Item, Target
End If
Next
For Each Item In Folder.Subfolders
SearchFolder Item, Target
Next
End Sub
Function FileIDExists(ByVal Number, ByVal Extension)
FileIDExists = Fso.FileExists(C_Out & "\" & FormatFileName(Number) & Extension)
End Function
Function FormatNumber(ByVal Pattern, ByVal Number, ByVal NLength)
Dim Filler, SNum
Filler = "0000000000000000"
SNum = CStr(Number)
SNum = Mid(Filler, 1, NLength - Len(SNum)) & SNum
FormatNumber = Replace(Pattern, "%", SNum)
End Function
Function UnformatNumber(ByVal Pattern, ByVal Format)
If Len(Format) < Len(Pattern) Then
UnformatNumber = -1
Exit Function
End If
Dim LPattern, RPattern, S
S = Split(Pattern, "%")
LPattern = S(0)
RPattern = S(UBound(S))
If Left(Format, Len(LPattern)) = LPattern _
And Right(Format, Len(RPattern)) = RPattern Then
UnformatNumber = CLng(Mid(Format, 1+Len(LPattern), Len(Format)-Len(LPattern)-Len(RPattern)))
Else
UnformatNumber = -1
End If
End Function
Function GetNextAvailableNumber(ByRef Folder)
If I_LastNumber < 0 Then
Dim Num, Item
I_LastNumber = 0
Num = 0
For Each Item In Folder.Files
Num = UnformatNumber(I_TargetPattern, Fso.GetBaseName(Item.Path))
If InArray(I_ValidExtensions, LCase(Fso.GetExtensionName(Item.Path))) And _
Num > I_LastNumber Then I_LastNumber = Num
Next
End If
I_LastNumber = I_LastNumber + 1
GetNextAvailableNumber = I_LastNumber
End Function
Function FindNextAvailableName(ByRef Folder)
FindNextAvailableName = FormatNumber(I_TargetPattern, GetNextAvailableNumber(Folder), I_NumberLength)
End Function
Function StripSlashes(ByVal Str)
Dim cutPoint
cutPoint = Len(Str)
Do While cutPoint > 0
If Not Mid(Str, cutPoint, 1) = "/" Then Exit Do
cutPoint = cutPoint - 1
Loop
StripSlashes = Left(Str, cutPoint)
End Function
Function GetFilenameWithoutExtension(ByVal FileName)
Dim Result, i, j
Result = FileName
i = InStrRev(FileName, ".")
j = InStrRev(FileName, "\")
If i > 0 And i > j Then
Result = Mid(FileName, 1, i - 1)
End If
GetFilenameWithoutExtension = Result
End Function
Function InArray(ByRef Arr, ByRef Match)
Dim Item
For Each Item In Arr
If Item = Match Then
InArray = True
Exit Function
End If
Next
InArray = False
End Function