-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUDF_FileTools.bas
More file actions
278 lines (267 loc) · 10.4 KB
/
UDF_FileTools.bas
File metadata and controls
278 lines (267 loc) · 10.4 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Attribute VB_Name = "UDF_FileTools"
'''=============================================================================
''' VBA FileTools
''' ---------------------------------------------
''' https://github.com/cristianbuse/VBA-FileTools
''' ---------------------------------------------
''' MIT License
'''
''' Copyright (c) 2012 Ion Cristian Buse
'''
''' Permission is hereby granted, free of charge, to any person obtaining a copy
''' of this software and associated documentation files (the "Software"), to
''' deal in the Software without restriction, including without limitation the
''' rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
''' sell copies of the Software, and to permit persons to whom the Software is
''' furnished to do so, subject to the following conditions:
'''
''' The above copyright notice and this permission notice shall be included in
''' all copies or substantial portions of the Software.
'''
''' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
''' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
''' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
''' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
''' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
''' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
''' IN THE SOFTWARE.
'''=============================================================================
Option Explicit
'*******************************************************************************
''Excel File Tools Module
''Functions in this module make it easy for the user to work with file paths
'' within the Excel interface using the FileTools library
''Functions below are capable to 'spill' on newer Excel versions (Office 365)
'*******************************************************************************
''Important!
'*******************************************************************************
''This module is intended to be used in Microsoft Excel only!
''Call the User-Defined-Functions (UDFs) in this module from Excel Ranges only
'' DO NOT call these functions from VBA! If you need any of the functions below
'' directly in VBA then use their equivalent from the LibFileTools module
'*******************************************************************************
''Requires:
'' - LibFileTools: library module
''Exposed Excel UDFs:
'' - IS_FILE
'' - IS_FOLDER
'' - LOCAL_PATH
'' - REMOTE_PATH
'*******************************************************************************
'Turn the below compiler constant to True if you are using the LibUDFs library
'https://github.com/cristianbuse/VBA-FastExcelUDFs
#Const USE_LIB_FAST_UDFS = False
'*******************************************************************************
Public Function IS_FILE(ByRef filePaths As Variant) As Variant
Application.Volatile False
#If USE_LIB_FAST_UDFS Then
LibUDFs.TriggerFastUDFCalculation
#End If
'
'Only accept 1-Area Ranges. This could alternatively be changed to ignore
' the extra Areas by arr = arr.Areas(1).Value2 instead of the 2 lines
If TypeName(filePaths) = "Range" Then
If filePaths.Areas.Count > 1 Then GoTo FailInput
filePaths = filePaths.Value2
End If
'
On Error GoTo ErrorHandler
If Not IsArray(filePaths) Then
IS_FILE = LibFileTools.IsFile(CStr(filePaths))
Exit Function
End If
'
Dim res() As Boolean
Dim i As Long
Dim j As Long
'
Select Case GetArrayDimsCount(filePaths)
Case 1
ReDim res(LBound(filePaths) To UBound(filePaths))
For i = LBound(filePaths) To UBound(filePaths)
res(i) = LibFileTools.IsFile(CStr(filePaths(i)))
Next i
Case 2
ReDim res(LBound(filePaths, 1) To UBound(filePaths, 1) _
, LBound(filePaths, 2) To UBound(filePaths, 2))
For i = LBound(filePaths, 1) To UBound(filePaths, 1)
For j = LBound(filePaths, 2) To UBound(filePaths, 2)
res(i, j) = LibFileTools.IsFile(CStr(filePaths(i, j)))
Next j
Next i
Case Else
GoTo FailInput
End Select
'
IS_FILE = res
Exit Function
ErrorHandler:
FailInput:
IS_FILE = CVErr(xlErrValue)
End Function
Public Function IS_FOLDER(ByRef folderPaths As Variant) As Variant
Application.Volatile False
#If USE_LIB_FAST_UDFS Then
LibUDFs.TriggerFastUDFCalculation
#End If
'
'Only accept 1-Area Ranges. This could alternatively be changed to ignore
' the extra Areas by arr = arr.Areas(1).Value2 instead of the 2 lines
If TypeName(folderPaths) = "Range" Then
If folderPaths.Areas.Count > 1 Then GoTo FailInput
folderPaths = folderPaths.Value2
End If
'
On Error GoTo ErrorHandler
If Not IsArray(folderPaths) Then
IS_FOLDER = LibFileTools.IsFolder(CStr(folderPaths))
Exit Function
End If
'
Dim res() As Boolean
Dim i As Long
Dim j As Long
'
Select Case GetArrayDimsCount(folderPaths)
Case 1
ReDim res(LBound(folderPaths) To UBound(folderPaths))
For i = LBound(folderPaths) To UBound(folderPaths)
res(i) = LibFileTools.IsFolder(CStr(folderPaths(i)))
Next i
Case 2
ReDim res(LBound(folderPaths, 1) To UBound(folderPaths, 1) _
, LBound(folderPaths, 2) To UBound(folderPaths, 2))
For i = LBound(folderPaths, 1) To UBound(folderPaths, 1)
For j = LBound(folderPaths, 2) To UBound(folderPaths, 2)
res(i, j) = LibFileTools.IsFolder(CStr(folderPaths(i, j)))
Next j
Next i
Case Else
GoTo FailInput
End Select
'
IS_FOLDER = res
Exit Function
ErrorHandler:
FailInput:
IS_FOLDER = CVErr(xlErrValue)
End Function
Public Function LOCAL_PATH(ByRef fullPaths As Variant _
, Optional ByVal returnInputOnFail As Boolean = False) As Variant
Application.Volatile False
#If USE_LIB_FAST_UDFS Then
LibUDFs.TriggerFastUDFCalculation
#End If
'
'Only accept 1-Area Ranges. This could alternatively be changed to ignore
' the extra Areas by arr = arr.Areas(1).Value2 instead of the 2 lines
If TypeName(fullPaths) = "Range" Then
If fullPaths.Areas.Count > 1 Then GoTo FailInput
fullPaths = fullPaths.Value2
End If
'
On Error GoTo ErrorHandler
If Not IsArray(fullPaths) Then
LOCAL_PATH = LibFileTools.GetLocalPath(CStr(fullPaths), , returnInputOnFail)
If LenB(LOCAL_PATH) = 0 Then LOCAL_PATH = CVErr(xlErrNA)
Exit Function
End If
'
Dim res() As String
Dim i As Long
Dim j As Long
'
Select Case GetArrayDimsCount(fullPaths)
Case 1
ReDim res(LBound(fullPaths) To UBound(fullPaths))
For i = LBound(fullPaths) To UBound(fullPaths)
res(i) = LibFileTools.GetLocalPath(CStr(fullPaths(i)), , returnInputOnFail)
If LenB(res(i)) = 0 Then res(i) = CVErr(xlErrNA)
Next i
Case 2
ReDim res(LBound(fullPaths, 1) To UBound(fullPaths, 1) _
, LBound(fullPaths, 2) To UBound(fullPaths, 2))
For i = LBound(fullPaths, 1) To UBound(fullPaths, 1)
For j = LBound(fullPaths, 2) To UBound(fullPaths, 2)
res(i, j) = LibFileTools.GetLocalPath(CStr(fullPaths(i, j)), , returnInputOnFail)
If LenB(res(i, j)) = 0 Then res(i, j) = CVErr(xlErrNA)
Next j
Next i
Case Else
GoTo FailInput
End Select
'
LOCAL_PATH = res
Exit Function
ErrorHandler:
FailInput:
LOCAL_PATH = CVErr(xlErrValue)
End Function
Public Function REMOTE_PATH(ByRef fullPaths As Variant _
, Optional ByVal returnInputOnFail As Boolean = False) As Variant
Application.Volatile False
#If USE_LIB_FAST_UDFS Then
LibUDFs.TriggerFastUDFCalculation
#End If
'
'Only accept 1-Area Ranges. This could alternatively be changed to ignore
' the extra Areas by arr = arr.Areas(1).Value2 instead of the 2 lines
If TypeName(fullPaths) = "Range" Then
If fullPaths.Areas.Count > 1 Then GoTo FailInput
fullPaths = fullPaths.Value2
End If
'
On Error GoTo ErrorHandler
If Not IsArray(fullPaths) Then
REMOTE_PATH = LibFileTools.GetRemotePath(CStr(fullPaths), , returnInputOnFail)
If LenB(REMOTE_PATH) = 0 Then REMOTE_PATH = CVErr(xlErrNA)
Exit Function
End If
'
Dim res() As String
Dim i As Long
Dim j As Long
'
Select Case GetArrayDimsCount(fullPaths)
Case 1
ReDim res(LBound(fullPaths) To UBound(fullPaths))
For i = LBound(fullPaths) To UBound(fullPaths)
res(i) = LibFileTools.GetRemotePath(CStr(fullPaths(i)), , returnInputOnFail)
If LenB(res(i)) = 0 Then res(i) = CVErr(xlErrNA)
Next i
Case 2
ReDim res(LBound(fullPaths, 1) To UBound(fullPaths, 1) _
, LBound(fullPaths, 2) To UBound(fullPaths, 2))
For i = LBound(fullPaths, 1) To UBound(fullPaths, 1)
For j = LBound(fullPaths, 2) To UBound(fullPaths, 2)
res(i, j) = LibFileTools.GetRemotePath(CStr(fullPaths(i, j)), , returnInputOnFail)
If LenB(res(i, j)) = 0 Then res(i, j) = CVErr(xlErrNA)
Next j
Next i
Case Else
GoTo FailInput
End Select
'
REMOTE_PATH = res
Exit Function
ErrorHandler:
FailInput:
REMOTE_PATH = CVErr(xlErrValue)
End Function
'*******************************************************************************
'Returns the Number of dimensions for an input array
'Returns 0 if array is uninitialized or input not an array
'Note that a zero-length array has 1 dimension! Ex. Array() bounds are (0 to -1)
'*******************************************************************************
Private Function GetArrayDimsCount(ByRef arr As Variant) As Long
Const MAX_DIMENSION As Long = 60 'VB limit
Dim dimension As Long
Dim tempBound As Long
'
On Error GoTo FinalDimension
For dimension = 1 To MAX_DIMENSION
tempBound = LBound(arr, dimension)
Next dimension
FinalDimension:
GetArrayDimsCount = dimension - 1
End Function