Code archives/File Utilities/FileName, FileExtention, FilePath, AddFileExtention

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

Download source code

FileName, FileExtention, FilePath, AddFileExtention by Diego2007
Does what the name says. I know, there are already two entries, wich do the same, but they will fail, if you want to analyze difficult filnames (ex. "C:\MyDreams...\Testfile" or the example given in the code). I think, you don't know, what the user will type in ...
- - - - - - - - - - - - - - -
Der Name ist programm. Ich weiß, dass bereits zwei Einträge existieren, diedas selbe machen, aber sie scheitern bei der Analyse schwiediger Dateinamen (z.B. "C:\MyDreams...\Testfile" oder dem Beispiel, das im Programmcode gegeben wird). Ich bin der Meinung, du weißt nicht, was der Benutzer eingeben wird ...
; Example | Beispiel
File$ = "C:\My.Files\Docs/File.txt" ; Difficult Filename | Schwieriger Dateiname
Print FileName$(File$)
Print FileExtention$(File$)
Print FilePath$(File$)
WaitKey
End

Function FileName$(File$)
For I% = Len(File$) To 1 Step -1
	If Mid(File$, I%, 1) = "\" Or Mid(File$, I%, 1) = "/" Then Return Mid(File$, I% + 1)
	Next
Return File$
End Function

Function FileExtention$(File$)
For I% = Len(File$) To 1 Step -1
	If Mid(File$, I%, 1) = "." Return Mid(File$, I% + 1)
	If Mid(File$, I%, 1) = "\" Or Mid(File$, I%, 1) = "/" Then Return ""
	Next
End Function

Function FilePath$(File$)
For I% = Len(File$) To 1 Step -1
	If Mid(File$, I%, 1) = "\" Or Mid(File$, I%, 1) = "/" Then Return Left(File$, I% - 1)
	Next
End Function

; Adds a file extention, if not exists. | Fügt eine Dateiendung hinzu falls noch keine da ist.
Function AddFileExtention$(File$, Extention$)
If FileExtention(File$) = "" Then Return File$ + "." + Extention$ Else Return File$
End Function

Comments

_332007
From the Blitz3D help document:
Instr (string1$, string2$, offset)
Parameters
string1$ = the string you wish to search
string2$ = the string to find
offset = valid integer starting position to being search (optional)

Description
This command will allow you to search for an occurance of a string within another string. The command returns the location (number of characters from the left) of the string you are looking for. Command returns a Zero if no matches are found.




Diego2007
I choosed an other solution, because Instr (string1$, string2$, offset) retuns the first occurance of a string, and we need the last. Alltrough it is possible to search all "\"s till we won't find anyone else, I think it's a little bit faster and more understandable to search the last "\" by compareing each character, starting with the last.


Code Archives Forum