-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFunctions.vb
More file actions
386 lines (304 loc) · 12.2 KB
/
FileFunctions.vb
File metadata and controls
386 lines (304 loc) · 12.2 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
Imports System.IO
Imports System.Threading
Public Structure FolderSelect
Private FullFilePath As String
Private DriveName As String
Private FolderNameArr() As String
Private IsFilePath As Boolean
Private maxIndex As Integer
Private _CurrentIndex As Integer
Public SelectedFolder As String
Public ReadOnly initialized As Boolean
Public Sub New(fullPath As String)
FullFilePath = fullPath
DriveName = Path.GetPathRoot(FullFilePath)
Dim str As String
If DriveName.EndsWith("\") Then
str = FullFilePath.Substring(DriveName.Length)
Else
str = FullFilePath.Substring(DriveName.Length + 1)
End If
FolderNameArr = str.Split("\"c)
initialized = True
Dim longPath As String
If FullFilePath.Substring(0, 2) <> "\\" Then
longPath = "\\?\" & FullFilePath
Else
longPath = "\\?\UNC\" & FullFilePath.Remove(0, 2)
End If
If File.Exists(longPath) = True Then
IsFilePath = True
maxIndex = FolderNameArr.Length - 2
Else
IsFilePath = False
maxIndex = FolderNameArr.Length - 1
End If
_CurrentIndex = maxIndex
End Sub
Public ReadOnly Property CurrentIndex As Integer
Get
Return _CurrentIndex
End Get
End Property
Public ReadOnly Property OriginalFilePath As String
Get
Return FullFilePath
End Get
End Property
Public ReadOnly Property CurrentFolder(Optional mode As String = "[folder,fullPath,parentFolder]") As String
Get
Select Case mode
Case "[folder,fullPath,parentFolder]", "folder"
Return FolderNameArr(_CurrentIndex)
Case "fullPath"
Dim arr() As String = FolderNameArr.Take(_CurrentIndex + 1).Prepend(DriveName).ToArray
Return Path.Combine(arr)
Case "parentFolder"
Dim arr() As String
If _CurrentIndex > 0 Then
arr = FolderNameArr.Take(_CurrentIndex).Prepend(DriveName).ToArray
Else
arr = {DriveName}
End If
Return Path.Combine(arr)
Case Else
Stop
Return ""
End Select
End Get
End Property
Public ReadOnly Property FullFoldersPath As String
Get
If IsFilePath = True Then
Return Path.GetDirectoryName(FullFilePath)
Else
Return FullFilePath
End If
End Get
End Property
Public ReadOnly Property FolderArr As String()
Get
Return FolderNameArr.Take(maxIndex + 1).ToArray
End Get
End Property
Public Function SwitchFolder(mode As String) As String
Select Case mode
Case "up"
If _CurrentIndex > 0 Then
_CurrentIndex -= 1
End If
Case "down"
If _CurrentIndex < maxIndex Then
_CurrentIndex += 1
End If
Case Else
Stop
End Select
Return FolderNameArr(_CurrentIndex)
End Function
End Structure
Public Structure UndoData
Public OriginalPath As String
Public newName As String
Public mode As String
Public newList As Boolean
Public ReadOnly Property newPath As String
Get
Return Path.Combine(Path.GetDirectoryName(OriginalPath), newName)
End Get
End Property
Public ReadOnly Property OriginalName As String
Get
Return Path.GetFileName(OriginalPath)
End Get
End Property
End Structure
Module FileFunctions
Public Function GetLongFilePaths2(FolderPath As String, SearchSubFolders As Boolean, MaxPathLength As Integer, ByRef completePathArr() As String,
ByRef FileScanCount As Integer, token As CancellationToken, ByRef searchException As Exception) As String()
FileScanCount = 0
Dim filteredList As New List(Of String), fullList As New List(Of String)
Try
Dim opt As System.IO.SearchOption = If(SearchSubFolders, SearchOption.AllDirectories, SearchOption.TopDirectoryOnly)
For Each filePath As String In Directory.EnumerateFiles(FolderPath, "*", opt)
If filePath.Length > MaxPathLength Then
filteredList.Add(filePath)
End If
fullList.Add(filePath)
FileScanCount += 1
token.ThrowIfCancellationRequested()
Next
Catch ex As OperationCanceledException
'continue
Catch ex As Exception
searchException = ex
End Try
completePathArr = fullList.ToArray
Return filteredList.ToArray
End Function
Public Function GetLongFilePaths3(FolderPath As String, SearchSubFolders As Boolean, MaxPathLength As Integer, ByRef completePathArr() As String,
ByRef FileScanCount As Integer, token As CancellationToken, searchExceptionList As List(Of Exception)) As String()
FileScanCount = 0
Dim filteredList As New List(Of String)
Dim fullList As New List(Of String)
Try
EnumerateFilesRecursive(FolderPath, SearchSubFolders, MaxPathLength, filteredList, fullList, FileScanCount, token, searchExceptionList)
Catch ex As OperationCanceledException
' Cancelled by user — return whatever was collected so far
End Try
completePathArr = fullList.ToArray()
Return filteredList.ToArray()
End Function
Private Sub EnumerateFilesRecursive(folderPath As String, recurse As Boolean, MaxPathLength As Integer, filteredList As List(Of String), fullList As List(Of String),
ByRef scanCount As Integer, token As CancellationToken, searchExceptionList As List(Of Exception))
' --- Enumerate files in the current folder ---
Dim files As IEnumerable(Of String)
Try
files = Directory.EnumerateFiles(folderPath, "*", SearchOption.TopDirectoryOnly)
Catch ex As Exception
searchExceptionList.Add(ex)
Return
End Try
For Each filePath As String In files
token.ThrowIfCancellationRequested()
fullList.Add(filePath)
scanCount += 1
If filePath.Length > MaxPathLength Then
filteredList.Add(filePath)
End If
Next
If recurse = False Then Return
Dim subFolders As IEnumerable(Of String)
Try
subFolders = Directory.EnumerateDirectories(folderPath)
Catch ex As Exception
searchExceptionList.Add(ex)
Return
End Try
For Each subFolder As String In subFolders
token.ThrowIfCancellationRequested()
EnumerateFilesRecursive(subFolder, recurse, MaxPathLength, filteredList, fullList, scanCount, token, searchExceptionList)
Next
End Sub
Public Function ShowInExplorer(filePath As String) As Boolean
Dim argument As String
If filePath.Length > 259 Then
filePath = Path.GetDirectoryName(filePath)
If filePath.Length > 259 Then
Return False
Else
argument = $"/expand,""{filePath}"""
End If
Else
argument = $"/select,""{filePath}"""
End If
If IO.File.Exists(filePath) Or IO.Directory.Exists(filePath) Then
Process.Start("explorer.exe", argument)
Else
Return False
End If
Return True
End Function
Public Function ChangeFileName(filePath As String, newName As String, folderMode As Boolean) As String
If newName = "" Then
Return "File name is empty"
ElseIf newName = Path.GetFileName(filePath) Then
Return "File or folder name is not new."
End If
Dim parentFolderPath As String = Path.GetDirectoryName(filePath)
'\\?\unc\
If filePath.Substring(0, 2) <> "\\" Then
filePath = "\\?\" & filePath
parentFolderPath = "\\?\" & parentFolderPath
Else
filePath = "\\?\UNC\" & filePath.Remove(0, 2)
parentFolderPath = "\\?\UNC\" & parentFolderPath.Remove(0, 2)
End If
Dim result As String = "success"
Dim newPath As String = parentFolderPath & "\" & newName
If folderMode = False Then
Dim index As Integer = newName.IndexOfAny(Path.GetInvalidFileNameChars)
If index <> -1 Then
Dim badChar As String = newName(index)
Return "File name given contains invalid character(s): " & badChar
End If
If File.Exists(newPath) Then
result = "New name given already exists"
ElseIf File.Exists(filePath) Then
Try
File.Move(filePath, newPath)
Catch ex As Exception
If ex.HResult = -2147024891 Then 'access denied error
Dim success As Boolean = cmdMove(filePath, newPath) 'SMB paths often generate erroneus access denied errors. The cmd command often works instead.
If success = True Then
Return "success:cmdMove used."
End If
End If
result = ex.Message
End Try
Else
result = "Selected file no longer exists or failed to detect."
End If
Else 'if folderMode = True
Dim index As Integer = newName.IndexOfAny(Path.GetInvalidPathChars)
If index <> -1 Then
Dim badChar As String = newName(index)
Return "Folder name given contains invalid character(s): " & badChar
End If
If Directory.Exists(newPath) Then
result = "New name given already exists"
ElseIf Directory.Exists(filePath) Then
Try
Directory.Move(filePath, newPath)
Catch ex As Exception
If ex.HResult = -2147024891 Then 'access denied error
Dim success As Boolean = cmdMove(filePath, newPath)
If success = True Then
Return "success:cmdMove used."
End If
End If
result = ex.Message
End Try
Else
result = "Selected folder no longer exists or failed to detect."
End If
End If
Return result
End Function
Public Function cmdMove(src As String, dest As String) As Boolean
Dim cmd As String = $"move ""{src}"" ""{dest}"""
Dim psi As New ProcessStartInfo With {
.FileName = "cmd.exe",
.Arguments = "/c " & cmd,
.CreateNoWindow = True,
.UseShellExecute = False,
.RedirectStandardError = True,
.RedirectStandardOutput = True
}
Dim proc As Process = Process.Start(psi)
proc.WaitForExit()
Dim output As String = proc.StandardOutput.ReadToEnd()
Dim err As String = proc.StandardError.ReadToEnd()
If proc.ExitCode = 0 Then
Return True
Else
Return False
End If
End Function
Public Sub WriteToTextFile(txtName As String, contentStr As String) 'If full path isn't given, will write to EXE directory
Dim filePath As String
If txtName.Contains("\") = True Then
filePath = txtName
Else
Dim exeFolder As String = Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location)
filePath = Path.Combine(exeFolder, txtName)
End If
File.WriteAllText(filePath, contentStr)
End Sub
Public Sub OpenTxtFile(txtPath As String)
Dim processInfo As New Diagnostics.ProcessStartInfo(txtPath) With {
.UseShellExecute = True
}
Diagnostics.Process.Start(processInfo)
End Sub
End Module