requesting a file

BlitzPlus Forums/BlitzPlus Programming/requesting a file

jhocking(Posted 2003) [#1]
I am trying to request an input file from the user. However I only want the filename itself, whereas RequestFile returns the filename with path. In other words, I want "file.txt" but RequestFile returns "C:\My Documents\file.txt"

Is there any way to use RequestFile so that it only returns to filename without path? Or is there any way to manipulate the returned string to trim out the path?

ADDITION: Still curious if there is a better way but I just snagged this code out of BBGui to trim the filename.

Function TrimFilename$(path$)
For a=Len(path) To 1 Step -1
byte$=Mid(path,a,1)
If byte="\"
Return Right(path,Len(path)-a)
EndIf
Next
End Function


soja(Posted 2003) [#2]
I don't think there is a way for RequestFile to do it (yet?)... You must manipulate the returned path. I can think of two ways to do it off the top of my head: the Blitz way, and the Windows way. Both have advantages and disadvantages.

The "Blitz" way:
a$ = "C:\My Documents\file.txt"
b$ = GetFilenameFromPath(a$)
Notify b$

Function GetFilenameFromPath$(path$)
	Local index% = 0
	Repeat
		index = Instr(path$, "\", 0)
		path$ = Right$(path$, Len(path$)-index)
	Until index = 0
	Return path$
End Function


The advantages of this is that it's fully supported in Blitz and is likely to work in any case. The disadvantages is that you have to provide it yourself, it may be a little slower, and it's not technically supported by Windows. It assumes that the file name is always what appears after the last backslash.

Here's the "Windows" way:
[put this in a .decls file]
.lib "shlwapi.dll"
PathFindFileName$(Path$):"PathFindFileNameA"

[put this in your .bb file]
a$ = "C:\My Documents\file.txt"
b$ = PathFindFileName(a$)
Notify b$


The advantage here is that the functionality is already provided by Windows. The disadvantage is that it may not be supported by ALL versions of Windows. Shlwapi.dll (Shell Light-weight utility API) is included with Windows, but I *think* it will only work if you have IE5 installed (in other words, Win98SE and on by default). I'm not positive.

There are probably other solutions.