-
Notifications
You must be signed in to change notification settings - Fork 1
/
modFileIO.bas
686 lines (600 loc) · 22.4 KB
/
modFileIO.bas
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
Attribute VB_Name = "modFileIO"
Option Explicit
Private Declare Function GetTempPathA Lib "kernel32.dll" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetTempFileNameA Lib "kernel32.dll" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32.dll" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
Private Declare Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Long) As Long
Private Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As Long, ByVal lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function VerQueryValue Lib "Version.dll" Alias "VerQueryValueA" (pBlock As Any, ByVal lpSubBlock As String, lplpBuffer As Any, puLen As Long) As Long
Private Declare Function GetFileVersionInfoSize Lib "Version.dll" Alias "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, lpdwHandle As Long) As Long
Private Declare Function GetFileVersionInfo Lib "Version.dll" Alias "GetFileVersionInfoA" (ByVal lptstrFilename As String, ByVal dwhandle As Long, ByVal dwlen As Long, lpData As Any) As Long
Private Declare Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function SHGetSpecialFolderPath Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" (ByVal hWnd As Long, ByVal lpszPath As String, ByVal nFolder As Integer, ByVal fCreate As Boolean) As Boolean
Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const MAX_PATH As Long = 260
Private Const UNIQUE_NAME = &H0
Private Const STD_INPUT_HANDLE = -10&
Public Const CSIDL_DESKTOP = &H0
Public Const CSIDL_INTERNET = &H1
Public Const CSIDL_PROGRAMS = &H2
Public Const CSIDL_CONTROLS = &H3
Public Const CSIDL_PRINTERS = &H4
Public Const CSIDL_PERSONAL = &H5
Public Const CSIDL_FAVORITES = &H6
Public Const CSIDL_STARTUP = &H7
Public Const CSIDL_RECENT = &H8
Public Const CSIDL_SENDTO = &H9
Public Const CSIDL_BITBUCKET = &HA
Public Const CSIDL_STARTMENU = &HB
Public Const CSIDL_MYDOCUMENTS = &HC
Public Const CSIDL_MYMUSIC = &HD
Public Const CSIDL_MYVIDEO = &HE
Public Const CSIDL_DESKTOPDIRECTORY = &H10
Public Const CSIDL_DRIVES = &H11
Public Const CSIDL_NETWORK = &H12
Public Const CSIDL_NETHOOD = &H13
Public Const CSIDL_FONTS = &H14
Public Const CSIDL_TEMPLATES = &H15
Public Const CSIDL_COMMON_STARTMENU = &H16
Public Const CSIDL_COMMON_PROGRAMS = &H17
Public Const CSIDL_COMMON_STARTUP = &H18
Public Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19
Public Const CSIDL_APPDATA = &H1A
Public Const CSIDL_PRINTHOOD = &H1B
Public Const CSIDL_LOCAL_APPDATA = &H1C
Public Const CSIDL_ALTSTARTUP = &H1D
Public Const CSIDL_COMMON_ALTSTARTUP = &H1E
Public Const CSIDL_COMMON_FAVORITES = &H1F
Public Const CSIDL_INTERNET_CACHE = &H20
Public Const CSIDL_COOKIES = &H21
Public Const CSIDL_HISTORY = &H22
Public Const CSIDL_COMMON_APPDATA = &H23
Public Const CSIDL_WINDOWS = &H24
Public Const CSIDL_SYSTEM = &H25
Public Const CSIDL_PROGRAM_FILES = &H26
Public Const CSIDL_MYPICTURES = &H27
Public Const CSIDL_PROFILE = &H28
Public Const CSIDL_SYSTEMX86 = &H29
Public Const CSIDL_PROGRAM_FILESX86 = &H2A
Public Const CSIDL_PROGRAM_FILES_COMMON = &H2B
Public Const CSIDL_PROGRAM_FILES_COMMONX86 = &H2C
Public Const CSIDL_COMMON_TEMPLATES = &H2D
Public Const CSIDL_COMMON_DOCUMENTS = &H2E
Public Const CSIDL_COMMON_ADMINTOOLS = &H2F
Public Const CSIDL_ADMINTOOLS = &H30
Public Const CSIDL_CONNECTIONS = &H31
Public Const CSIDL_COMMON_MUSIC = &H35
Public Const CSIDL_COMMON_PICTURES = &H36
Public Const CSIDL_COMMON_VIDEO = &H37
Public Const CSIDL_RESOURCES = &H38
Public Const CSIDL_RESOURCES_LOCALIZED = &H39
Public Const CSIDL_COMMON_OEM_LINKS = &H3A
Public Const CSIDL_CDBURN_AREA = &H3B
Public Const CSIDL_COMPUTERSNEARME = &H3D
Private Type VS_FIXEDFILEINFO
Signature As Long
StrucVersionl As Integer ' e.g. = &h0000 = 0
StrucVersionh As Integer ' e.g. = &h0042 = .42
FileVersionMSl As Integer ' e.g. = &h0003 = 3
FileVersionMSh As Integer ' e.g. = &h0075 = .75
FileVersionLSl As Integer ' e.g. = &h0000 = 0
FileVersionLSh As Integer ' e.g. = &h0031 = .31
ProductVersionMSl As Integer ' e.g. = &h0003 = 3
ProductVersionMSh As Integer ' e.g. = &h0010 = .1
ProductVersionLSl As Integer ' e.g. = &h0000 = 0
ProductVersionLSh As Integer ' e.g. = &h0031 = .31
FileFlagsMask As Long ' = &h3F for version "0.42"
FileFlags As Long ' e.g. VFF_DEBUG Or VFF_PRERELEASE
FileOS As Long ' e.g. VOS_DOS_WINDOWS16
FileType As Long ' e.g. VFT_DRIVER
FileSubtype As Long ' e.g. VFT2_DRV_KEYBOARD
FileDateMS As Long ' e.g. 0
FileDateLS As Long ' e.g. 0
End Type
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Public Function CorrectFilename(ByVal InFile As String) As String
Dim TempFilename As String
TempFilename = InFile
If (Len(InFile) > 0) Then
TempFilename = Replace(TempFilename, "\", "_")
TempFilename = Replace(TempFilename, "/", "_")
TempFilename = Replace(TempFilename, ":", "-")
TempFilename = Replace(TempFilename, "*", "-")
TempFilename = Replace(TempFilename, "?", "-")
TempFilename = Replace(TempFilename, """", "'")
TempFilename = Replace(TempFilename, "<", "-")
TempFilename = Replace(TempFilename, ">", "-")
TempFilename = Replace(TempFilename, "|", "-")
If (LCase(right(TempFilename, 4)) <> ".pdf") Then
TempFilename = TempFilename & ".pdf"
End If
End If
CorrectFilename = TempFilename
End Function
Public Function StripPath(ByVal vData As String) As String
On Error Resume Next
Dim DotPos As Long
DotPos = InStrRev(vData, "\")
If DotPos > 0 Then
StripPath = right(vData, Len(vData) - DotPos)
Else
StripPath = vData
End If
End Function
Public Function StripFilename(ByVal vData As String) As String
On Error Resume Next
Dim DotPos As Long
DotPos = InStrRev(vData, "\")
If DotPos > 0 Then
StripFilename = left(vData, DotPos)
Else
StripFilename = Empty
End If
End Function
Public Function StripExtension(ByVal vData As String) As String
On Error Resume Next
Dim DotPos As Long
DotPos = InStrRev(vData, ".")
If DotPos > 0 Then
StripExtension = left(vData, DotPos - 1)
Else
StripExtension = vData
End If
End Function
Public Function GetExtension(ByVal vData As String) As String
On Error Resume Next
Dim DotPos As Long
DotPos = InStrRev(vData, ".")
If DotPos > 0 Then
GetExtension = right(vData, Len(vData) - DotPos)
End If
End Function
Public Function AddBackslash(ByVal S As String) As String
If Len(S) > 0 Then
If right$(S, 1) <> "\" Then
AddBackslash = S + "\"
Else
AddBackslash = S
End If
Else
AddBackslash = Empty
End If
End Function
Public Function RemoveBackslash(ByVal S As String) As String
If Len(S) > 0 Then
If right$(S, 1) = "\" Then
RemoveBackslash = left(S, Len(S) - 1)
Else
RemoveBackslash = S
End If
Else
RemoveBackslash = Empty
End If
End Function
Public Function VBPathExists(ByVal FolderPath As String) As Boolean
On Error GoTo NoPath:
Dir FolderPath
VBPathExists = True
Exit Function
NoPath:
VBPathExists = False
End Function
Public Function PathExists(ByVal FolderPath As String) As Boolean
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
PathExists = FSO.FolderExists(FolderPath)
Set FSO = Nothing
End Function
Public Function CreateDirectory(ByVal FolderPath As String) As Boolean
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
FolderPath = RemoveBackslash(FolderPath)
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CreateFolder FolderPath
CreateDirectory = FSO.FolderExists(FolderPath)
Set FSO = Nothing
End Function
Public Function DeleteDirectory(ByVal FolderPath As String) As Boolean
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
FolderPath = RemoveBackslash(FolderPath)
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFolder FolderPath, True
DeleteDirectory = Not FSO.FolderExists(FolderPath)
Set FSO = Nothing
End Function
Public Function VBFileExists(ByVal FilePath As String) As Boolean
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(FilePath, WFD)
VBFileExists = hFile <> INVALID_HANDLE_VALUE
FindClose (hFile)
End Function
Public Function FileExists(ByVal FilePath As String) As Boolean
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
FileExists = FSO.FileExists(FilePath)
Set FSO = Nothing
End Function
Public Function FileSize(FilePath As String) As Long
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
Dim FSO_File 'As Scripting.file
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(FilePath) Then
Set FSO_File = FSO.GetFile(FilePath)
FileSize = FSO_File.Size
Set FSO_File = Nothing
End If
Set FSO = Nothing
End Function
Public Function CopyFile(SourcePath As String, TargetPath As String, Optional Overwrite As Boolean = True) As Boolean
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(TargetPath)) Then
If (Overwrite = True) Then
If (DeleteFile(TargetPath)) Then
'Successfully deleted
FSO.CopyFile SourcePath, TargetPath
CopyFile = FSO.FileExists(TargetPath)
Else
'Unable to delete file
CopyFile = False
End If
Else
'File exists and will not be overwritten
CopyFile = False
End If
Else
'file doesn't exist, just move it
FSO.CopyFile SourcePath, TargetPath
CopyFile = FSO.FileExists(TargetPath)
End If
Set FSO = Nothing
End Function
Public Function MoveFile(SourcePath As String, TargetPath As String, Optional Overwrite As Boolean = True) As Boolean
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(TargetPath)) Then
If (Overwrite = True) Then
' If (FSO.DeleteFile(TargetPath)) Then '这一句老是判断不成功
'Successfully deleted
FSO.DeleteFile TargetPath, True
FSO.MoveFile SourcePath, TargetPath
MoveFile = FSO.FileExists(TargetPath) '先强制删除,再移入
' Else
' 'Unable to delete file
' MoveFile = False
' End If
Else
'File exists and will not be overwritten
MoveFile = False
End If
Else
'file doesn't exist, just move it
FSO.MoveFile SourcePath, TargetPath
MoveFile = FSO.FileExists(TargetPath)
End If
Set FSO = Nothing
End Function
Public Function DeleteFile(FilePath As String) As Boolean
On Local Error Resume Next
Dim FSO 'As Scripting.FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile (FilePath)
DeleteFile = Not FSO.FileExists(FilePath)
Set FSO = Nothing
End Function
Public Function GetTempFileName() As String
On Local Error Resume Next
Dim sTmp As String
Dim sTmp2 As String
sTmp2 = GetTempPath
sTmp = Space(Len(sTmp2) + 256)
Call GetTempFileNameA(sTmp2, App.EXEName, UNIQUE_NAME, sTmp)
'Hier wird immer der kurze Dateiname zur點kgegeben!!
GetTempFileName = GetShortName(left$(sTmp, InStr(sTmp, Chr$(0)) - 1))
DeleteFile GetTempFileName
End Function
Public Function GetTempPath() As String
On Local Error Resume Next
Dim sTmp As String
Dim i As Integer
i = GetTempPathA(0, "")
sTmp = Space(i)
Call GetTempPathA(i, sTmp)
GetTempPath = AddBackslash(left$(sTmp, i - 1))
End Function
Public Function GetSystemDir() As String
On Local Error Resume Next
Dim temp As String * 256
Dim X As Integer
X = GetSystemDirectory(temp, Len(temp))
GetSystemDir = left$(temp, X)
End Function
Public Function GetWinDir() As String
On Local Error Resume Next
Dim temp As String * 256
Dim X As Integer
X = GetWindowsDirectory(temp, Len(temp))
GetWinDir = left$(temp, X)
End Function
Public Function GetAllUsersDocumentsDir() As String
On Local Error Resume Next
Dim blnReturn As Long
Dim strBuffer As String
strBuffer = Space(255)
blnReturn = SHGetSpecialFolderPath(0, strBuffer, CSIDL_COMMON_DOCUMENTS, False)
GetAllUsersDocumentsDir = left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
End Function
Public Function GetSpecialFolder(FolderID As Long) As String
On Local Error Resume Next
Dim blnReturn As Long
Dim strBuffer As String
strBuffer = Space(255)
blnReturn = SHGetSpecialFolderPath(0, strBuffer, FolderID, False)
GetSpecialFolder = left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
End Function
Public Function IsFileOpen(Filename As String)
On Local Error Resume Next
Dim filenum As Integer
Dim errnum As Integer
filenum = FreeFile()
Open Filename For Input Lock Read As #filenum
Close filenum
errnum = Err
Select Case errnum
Case 0
IsFileOpen = False
Case Else
IsFileOpen = True
End Select
End Function
Public Function FileCompare(ByVal FilePath1 As String, ByVal FilePath2 As String) As Boolean
On Error GoTo ErrorHandler
Dim lLen1 As Long, lLen2 As Long
Dim iFileNum1 As Integer
Dim iFileNum2 As Integer
Dim bytArr1() As Byte, bytArr2() As Byte
Dim lCtr As Long, lStart As Long
Dim bAns As Boolean
If Dir(FilePath1) = "" Then Exit Function
If Dir(FilePath2) = "" Then Exit Function
lLen1 = FileLen(FilePath1)
lLen2 = FileLen(FilePath2)
If lLen1 <> lLen2 Then
Exit Function
Else
iFileNum1 = FreeFile
Open FilePath1 For Binary Access Read As #iFileNum1
iFileNum2 = FreeFile
Open FilePath2 For Binary Access Read As #iFileNum2
bytArr1() = InputB(LOF(iFileNum1), #iFileNum1)
bytArr2() = InputB(LOF(iFileNum2), #iFileNum2)
lLen1 = UBound(bytArr1)
lStart = LBound(bytArr1)
bAns = True
For lCtr = lStart To lLen1
If bytArr1(lCtr) <> bytArr2(lCtr) Then
bAns = False
Exit For
End If
Next
FileCompare = bAns
End If
ErrorHandler:
If iFileNum1 > 0 Then Close #iFileNum1
If iFileNum2 > 0 Then Close #iFileNum2
End Function
Public Function LoadTextFile(ByVal FilePath As String, ByRef Text As String) As Boolean
On Error GoTo ErrorHandler:
Dim iFile As Integer
If FileExists(FilePath) = False Then
LoadTextFile = False
Exit Function
End If
iFile = FreeFile
Open FilePath For Input As #iFile
Text = Input(LOF(iFile), #iFile)
Close #iFile
LoadTextFile = True
Exit Function
ErrorHandler:
If iFile > 0 Then Close #iFile
LoadTextFile = False
End Function
Public Function LoadTextFileTail(ByVal FilePath As String, ByRef Text As String, Size As Long) As Boolean
On Error GoTo ErrorHandler:
Dim iFile As Integer
If FileExists(FilePath) = False Then
LoadTextFileTail = False
Exit Function
End If
iFile = FreeFile
Open FilePath For Input As #iFile
If (Size >= LOF(iFile)) Then
Text = Input(LOF(iFile), #iFile)
Else
Seek #iFile, LOF(iFile) - Size
Text = Input(Size - 1, #iFile)
End If
Close #iFile
LoadTextFileTail = True
Exit Function
ErrorHandler:
If iFile > 0 Then Close #iFile
LoadTextFileTail = False
End Function
Public Sub WriteLogFile(ByVal myFileName As String, ByVal LogString As String)
On Local Error GoTo ErrorHandler
Dim oFile As Integer
oFile = FreeFile
If FileExists(myFileName) Then
Open myFileName For Append As #oFile
Else
Open myFileName For Output As #oFile
End If
Print #oFile, LogString
Close #oFile
Exit Sub
ErrorHandler:
If oFile > 0 Then Close #oFile
End Sub
Public Function WriteFile(FilePath As String, Content As String) As Boolean
On Local Error GoTo ErrorHandler
Dim FSO As Object 'Scripting.FileSystemObject
Dim FSO_TextStream As Object 'Scripting.TextStream
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_TextStream = FSO.CreateTextFile(FilePath, True)
If Not FSO_TextStream Is Nothing Then
FSO_TextStream.Write Content
FSO_TextStream.Close
If FSO.FileExists(FilePath) Then
WriteFile = True
End If
End If
Exit Function
ErrorHandler:
WriteFile = False
End Function
Public Function GetShortName(ByVal sLongFileName As String) As String
Dim lRetVal As Long
Dim sShortPathName As String
Dim iLen As Integer
'Set up buffer area for API function call return
iLen = GetShortPathName(sLongFileName, sShortPathName, iLen)
sShortPathName = Space(iLen)
'Call the function
lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
'Strip away unwanted characters.
GetShortName = left(sShortPathName, lRetVal)
End Function
Public Function ReadStdInToFile(Filename As String) As Boolean
Dim FSO As Object 'Scripting.FileSystemObject
Dim FSO_TextStream As Object 'Scripting.TextStream
Dim hStdIn As Long
Dim Buffer As String * 2048
Dim bytes As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_TextStream = FSO.CreateTextFile(Filename, True)
'Dim test As String
'Dim mobjSTDIN As Object 'Scripting.TextStream
' Set mobjSTDIN = FSO.GetStandardStream(StdIn)
' test = mobjSTDIN.ReadAll
' FSO_TextStream.Write test
If Not FSO_TextStream Is Nothing Then
hStdIn = GetStdHandle(STD_INPUT_HANDLE)
Do
ReadFile hStdIn, Buffer, Len(Buffer), bytes, 0&
FSO_TextStream.Write left(Buffer, bytes)
Loop Until bytes = 0
FSO_TextStream.Close
If FSO.FileExists(Filename) Then
ReadStdInToFile = True
End If
End If
End Function
'Public Function FileVersionInfo(FilePath As String) As String
'On Local Error Resume Next
'Dim FSO 'As Scripting.FileSystemObject
'
' Set FSO = CreateObject("Scripting.FileSystemObject")
' FileVersionInfo = FSO.GetFileVersion(FilePath)
' Set FSO = Nothing
'End Function
Public Function FileVersionInfo(sFileName As String) As String
Dim lFileHwnd As Long, lRet As Long, lBufferLen As Long, lplpBuffer As Long, lpuLen As Long
Dim abytBuffer() As Byte
Dim tVerInfo As VS_FIXEDFILEINFO
Dim sBlock As String, sStrucVer As String
'Get the size File version info structure
lBufferLen = GetFileVersionInfoSize(sFileName, lFileHwnd)
If lBufferLen = 0 Then
Exit Function
End If
'Create byte array buffer, then copy memory into structure
ReDim abytBuffer(lBufferLen)
Call GetFileVersionInfo(sFileName, 0&, lBufferLen, abytBuffer(0))
Call VerQueryValue(abytBuffer(0), "\", lplpBuffer, lpuLen)
Call CopyMem(tVerInfo, ByVal lplpBuffer, Len(tVerInfo))
'Determine structure version number (For info only)
sStrucVer = Format$(tVerInfo.StrucVersionh) & "." & Format$(tVerInfo.StrucVersionl)
'Concatenate file version number details into a result string
FileVersionInfo = tVerInfo.FileVersionMSh & "." & tVerInfo.FileVersionMSl & "." & tVerInfo.FileVersionLSh & "." & tVerInfo.FileVersionLSl
End Function
Public Function GetIncrementalFilename(FilePath As String) As String
Dim FolderPath As String
Dim NewFileName As String
Dim NewFilePath As String
Dim Filename As String
Dim ExtensionName As String
Dim BaseName As String
Dim i As Long
FolderPath = StripFilename(FilePath)
If PathExists(FolderPath) Then
'folder exists, that's good
If Not FileExists(FilePath) Then
'if given filename doesn't exist, everything's ok
GetIncrementalFilename = FilePath
Else
Filename = StripPath(FilePath)
ExtensionName = GetExtension(Filename)
BaseName = GetIncrementalBaseName(StripExtension(Filename))
i = 1
Do
NewFilePath = AddBackslash(FolderPath) & BaseName & "(" & i & ")." & ExtensionName
i = i + 1
Loop Until (Not FileExists(NewFilePath))
GetIncrementalFilename = NewFilePath
End If
End If
End Function
Public Function GetIncrementalBaseName(BaseName As String) As String
Dim RegEx As Object 'RegExp
Dim Matches As Object 'MatchCollection
Dim Match As Object 'Match
Dim Submatches As Object 'Submatches
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.MultiLine = True
RegEx.Global = True
RegEx.Pattern = "^(.*) \([0-9]+\)$"
Set Matches = RegEx.Execute(BaseName)
If Matches.Count > 0 Then
For Each Match In Matches
Set Submatches = Match.Submatches
If (Submatches.Count > 0) Then
GetIncrementalBaseName = Submatches(0)
End If
Exit For
Next
Else
GetIncrementalBaseName = BaseName
End If
Set RegEx = Nothing
Set Matches = Nothing
Set Match = Nothing
Set Submatches = Nothing
End Function