Blitz_File_FileName.bb

Name: Blitz_File_FileName.bb
Version: 1.1
Released: August 27th, 2007
Description: Commands for splitting a file path into file name, directory and extension.
Download: Download Now (3.53KB)
; ----------------------------------------------------------------------
; -- 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

;;; Returns the directory name component of path.
;;; The path to use.
;;; A version of PHP's "dirname" function.
;;; The directory name from a path.
;;; File_GetDirName$("c:\my-dir\another-dir\file.txt") will return ""c:\my-dir\another-dir\"
;;; Blitz.File
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

;;; Returns the file name component of a path.
;;; The path to use.
;;; A version of PHP's "basename" function.
;;; The file name from a path, or "" if not found
;;; File_GetFileName$("c:\my-dir\another-dir\file.txt") will return "file.txt"
;;; Blitz.File
Function File_GetFileName$(path$)
	
	If path = "" Then Return ""
	
	Local fileName$	= File_SplitAfterChar(File_ConvertSlashes(path$), "/")
	If fileName = "" Then fileName = path
	
	Return fileName
	
End Function

;;; Returns the extension part of a filename.
;;; The file name to get the extension of.
;;; Will return text after the final "." character.
;;; The file extension found, or "" if not found.
;;; File_GetFileName$("c:\my-dir\another-dir\file.txt") will return "txt"
;;; Blitz.File
Function File_GetExtension$(fileName$)
	Return File_SplitAfterChar(File_ConvertSlashes(fileName$), ".")
End Function

; --------------------------------------------------
; -- Utility Functions
; --------------------------------------------------

;;; Convert backslashes in a filename to forward slashes.
;;; The filename to convert.
;;; Filename with only forward slashes.
;;; Blitz.File
Function File_ConvertSlashes$(fileName$)	
	Return Replace(fileName, "\", "/")
End Function

;;; Gets the remainder of a string after the last instance of a character "char" has been reached.
;;; The file name to split.
;;; The character to look for.
;;; The remainder of the string after char, or "" if not found.
;;; Blitz.File
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