Excel-VBA-Collection
210 строк · 6.2 Кб
1Attribute VB_Name = "StrFiles"
2Option Explicit
3Option Compare Text
4Option Base 1
5DefLng A-Z
6
7Const ExtChar = "."
8Const PathChar = "\"
9Const UNCChar = "\\"
10Const SepChar = ";"
11
12Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
13Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
14Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
15Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
16'Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
17
18'Windows
19Public Function GetWinDir() As String
20Dim s As String, n
21n = 255
22s = Space(n)
23n = GetWindowsDirectory(s, n)
24GetWinDir = RightSlash(Left(s, n))
25End Function
26
27Public Function GetWinSysDir() As String
28Dim s As String, n
29n = 255
30s = Space(n)
31n = GetSystemDirectory(s, n)
32GetWinSysDir = RightSlash(Left(s, n))
33End Function
34
35Public Function GetWinTempDir() As String
36Dim s As String, n
37n = 255
38s = Space(n)
39n = GetTempPath(n, s)
40GetWinTempDir = RightSlash(Left(s, n))
41End Function
42
43Public Function GetWinTempFile(Optional Prefix As String = "TMP") As String
44Dim s As String, n
45n = 255: s = Space(n)
46n = GetTempPath(n, s)
47GetTempFileName Left(s, n), Prefix, 0, s
48GetWinTempFile = Left(s, InStr(s, vbNullChar) - 1)
49End Function
50
51'Files
52Public Function FullFile(File As String) As String
53Dim Disk As String, Path As String, CurPath As String
54If Len(File) = 0 Then
55FullFile = vbNullString
56Exit Function
57End If
58CurPath = ActiveWorkbook.FullName
59CurPath = Left(CurPath, InStrR(CurPath, PathChar) - 1) 'instead CurDir!
60If Mid(File, 2, 1) = ":" Then
61Disk = Left(File, 2)
62Path = Mid(File, 3)
63ElseIf Left(File, 2) = UNCChar Then
64Disk = vbNullString
65Path = File
66Else 'ignore CurDir
67'Disk = Left(CurDir, 2)
68Disk = Left(CurPath, 2)
69Path = File
70End If
71If Left(Path, 2) = ".\" Then Path = Mid(Path, 3)
72Do While Left(Path, 3) = "..\"
73Disk = Left(Disk, InStrR(Disk, PathChar) - 1)
74Path = Mid(Path, 4)
75Loop
76If Left(Path, 1) = PathChar Then
77FullFile = Disk & Path
78Else
79'FullFile = RightPathName(CurDir(Disk), Path)
80FullFile = RightPathName(CurPath, Path)
81End If
82End Function
83
84Public Function FilePath(File As String) As String
85Dim n As Long
86File = FullFile(File)
87If IsDir(File) Then
88FilePath = RightSlash(File)
89Else
90n = InStrR(File, PathChar)
91If n > 0 Then
92FilePath = Left(File, n)
93Else
94FilePath = RightSlash(CurDir)
95End If
96End If
97End Function
98
99Public Function FileNameExt(File As String) As String
100Dim n As Long
101n = InStrR(File, PathChar)
102If n = Len(File) Then
103FileNameExt = vbNullString
104ElseIf n > 0 Then
105FileNameExt = Mid(File, n + 1)
106Else
107FileNameExt = File
108End If
109End Function
110
111Public Function FileNameOnly(File As String) As String
112Dim n1 As Long, n2 As Long
113If Right(File, 1) = PathChar Then File = Left(File, Len(File) - 1)
114n1 = InStrR(File, PathChar)
115n2 = InStrR(File, ExtChar)
116If n1 > n2 Then
117FileNameOnly = Mid(File, n1 + 1)
118ElseIf n2 > 0 Then
119FileNameOnly = Mid(File, n1 + 1, n2 - n1 - 1)
120Else
121FileNameOnly = Mid(FileNameOnly, n1 + 1)
122End If
123End Function
124
125Public Function FileExt(File As String) As String
126Dim n1 As Long, n2 As Long
127If Right(File, 1) = PathChar Then File = Left(File, Len(File) - 1)
128n1 = InStrR(File, PathChar)
129n2 = InStrR(File, ExtChar)
130If n1 > n2 Then
131FileExt = vbNullString
132ElseIf n2 > 0 Then
133FileExt = Mid(File, n2 + 1)
134Else
135FileExt = vbNullString
136End If
137End Function
138
139Public Function ChangeFileExt(File As String, FileExt As String) As String
140'No error checking!
141If Len(File) = 0 Then
142ChangeFileExt = vbNullString
143Else
144ChangeFileExt = FullFile(Left(File, InStrR(File, ExtChar)) & FileExt)
145End If
146End Function
147
148Public Function RightSlash(FilePath As String) As String
149If Len(FilePath) = 0 Then FilePath = CurDir
150If Right(FilePath, 1) = PathChar Then
151RightSlash = FilePath
152Else
153RightSlash = FilePath & PathChar
154End If
155End Function
156
157Public Function RightPathName(FilePath As String, File As String) As String
158RightPathName = RightSlash(FilePath) & File
159End Function
160
161'Quote long filenames with spaces in
162Public Function QFile(File As String) As String
163If InStr(File, " ") > 0 And Left(File, 1) <> """" Then
164QFile = """" & File & """"
165Else
166QFile = File
167End If
168End Function
169
170'Create all dirs in the path
171Public Sub ForceDirectories(Path As String)
172Dim Arr As Variant, i As Long, s As String
173If IsDir(Path) Then Exit Sub
174On Error Resume Next
175Path = FullFile(Path)
176Arr = StrToArr(Path, PathChar)
177s = Left(Path, InStr(Path, PathChar) - 1)
178For i = 2 To UBound(Arr)
179s = s & PathChar & CStr(Arr(i))
180MkDir s
181Next
182End Sub
183
184'Search for the file a-la PATH
185Public Function PathDirectories(Path As String, File As String) As String
186Dim Arr As Variant, i As Long, s As String
187On Error Resume Next
188s = ActiveWorkbook.FullName
189s = Left(s, InStrR(s, PathChar) - 1)
190Path = Path & SepChar & s '& SepChar & Environ("PATH")
191Arr = StrToArr(Path, SepChar)
192For i = 1 To UBound(Arr)
193s = FullFile(RightPathName(CStr(Arr(i)), File))
194If IsFile(s) Then
195PathDirectories = FullFile(s)
196Exit Function
197End If
198Next
199PathDirectories = vbNullString
200End Function
201
202Public Function CountFiles(Mask As String) As Long
203Dim File As String
204File = Dir(Mask)
205CountFiles = 0
206Do While File <> vbNullString
207CountFiles = CountFiles + 1
208File = Dir
209Loop
210End Function
211