Code archives/File Utilities/Get Filename/Folder/Extension

This code has been declared by its author to be Public Domain code.

Download source code

Get Filename/Folder/Extension by Nebula2003
I was modifying some code and I needed only the filename from a string that contained the entire path. So I coded a function for this and two others just for the heck of it.

filename$ = getfilename$(path_and_file)$
extension$ = getextension$(path_and_file)$
directory$ = getdirectory$(path_and_file)$

filename$ would look like "renamed.bmp"
extension$ would look like "bmp"
directory$ would look like "c:\games\pacmanIV\"
Function getfilename$(filename$) ; Returns the filename and extension
	lastdir = 1
	For i=1 To Len(filename$)
		If Mid$(filename$,i,1) = "\" Then Lastdir = i
	Next
	If Lastdir > 1 Then Lastdir = Lastdir + 1
	For i=Lastdir To Len(filename$)
		a$ = a$ + Mid(filename$,i,1)
	Next
	Return a$
End Function

Function getextension$(filename$) ; Returns the extension minus the .
	lastdir = 1
	For i=1 To Len(filename$)
		If Mid$(filename$,i,1) = "." Then Lastdir = i
	Next
	If Lastdir > 1 Then Lastdir = Lastdir + 1
	For i=Lastdir To Len(filename$)
		a$ = a$ + Mid(filename$,i,1)
	Next
	Return a$
End Function

Function getdirectory$(filename$) ; Returns the complete directory including drive
	lastdir = 1
	For i=1 To Len(filename$)
		If Mid$(filename$,i,1) = "\" Then Lastdir = i
	Next
	For i=1 To Lastdir
		a$ = a$ + Mid(filename$,i,1)
	Next
	Return a$
End Function

Comments

None.

Code Archives Forum