-
Notifications
You must be signed in to change notification settings - Fork 109
/
Blitz_File_FileName.bb
90 lines (74 loc) · 3.45 KB
/
Blitz_File_FileName.bb
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
; ----------------------------------------------------------------------
; -- Blitz_File_FileName.bb
; --
; -- Functions for finding out the names of files, directories
; -- and their extensions.
; --
; -- Author : Phil Newton (http://www.sodaware.net/)
; -- Licence : Free to use & modify. Credit appreciated but not required.
; -- Version : 1.1
; ----------------------------------------------------------------------
; --------------------------------------------------
; -- API Functions
; --------------------------------------------------
; File_GetDirName
; File_GetFileName
; File_GetExtension
;;; <summary>Returns the directory name component of path.</summary>
;;; <param name="path">The path to use.</param>
;;; <remarks>A version of PHP's "dirname" function.</remarks>
;;; <returns>The directory name from a path.</returns>
;;; <example>File_GetDirName$("c:\my-dir\another-dir\file.txt") will return ""c:\my-dir\another-dir\"</example>
;;; <subsystem>Blitz.File</subsystem>
Function File_GetDirName$(path$)
; Strip trailing slashes & return the part of the path that isn't the filename
If Right(path, 1) = "/" Or Right(path, 1) = "\" Then path = Left(path, Len(path) - 1)
Return Left(path, Len(path) - Len(File_GetFileName(path)))
End Function
;;; <summary>Returns the file name component of a path.</summary>
;;; <param name="path">The path to use.</param>
;;; <remarks>A version of PHP's "basename" function.</remarks>
;;; <returns>The file name from a path, or "" if not found</returns>
;;; <example>File_GetFileName$("c:\my-dir\another-dir\file.txt") will return "file.txt"</example>
;;; <subsystem>Blitz.File</subsystem>
Function File_GetFileName$(path$)
If path = "" Then Return ""
Local fileName$ = File_SplitAfterChar(File_ConvertSlashes(path$), "/")
If fileName = "" Then fileName = path
Return fileName
End Function
;;; <summary>Returns the extension part of a filename.</summary>
;;; <param name="fileName">The file name to get the extension of.</param>
;;; <remarks>Will return text after the final "." character.</remarks>
;;; <returns>The file extension found, or "" if not found.</returns>
;;; <example>File_GetFileName$("c:\my-dir\another-dir\file.txt") will return "txt"</example>
;;; <subsystem>Blitz.File</subsystem>
Function File_GetExtension$(fileName$)
Return File_SplitAfterChar(File_ConvertSlashes(fileName$), ".")
End Function
; --------------------------------------------------
; -- Utility Functions
; --------------------------------------------------
;;; <summary>Convert backslashes in a filename to forward slashes.</summary>
;;; <param name="fileName">The filename to convert.</param>
;;; <returns>Filename with only forward slashes.</returns>
;;; <subsystem>Blitz.File</subsystem>
Function File_ConvertSlashes$(fileName$)
Return Replace(fileName, "\", "/")
End Function
;;; <summary>Gets the remainder of a string after the last instance of a character "char" has been reached.</summary>
;;; <param name="fileName">The file name to split.</param>
;;; <param name="char">The character to look for.</param>
;;; <returns>The remainder of the string after char, or "" if not found.</returns>
;;; <subsystem>Blitz.File</subsystem>
Function File_SplitAfterChar$(fileName$, char$)
Local afterChar$ = ""
; Start at the end of the name, and look for the char
For stringPos = Len(fileName) To 1 Step -1
If Mid(fileName, stringPos, 1) = char Then
afterChar = Right(fileName, Len(fileName) - stringpos)
Exit
EndIf
Next
Return afterChar$
End Function