TIPCORNER - Paths and File Names
- by Brad Moore and Alyce Watson

Home

Paths and File Names
Observed online
Resources for the beginner
Graphics Drawing Rules
beginner's guide to API and DLL
Drawing IN MEMORY
Radiobuttons via API
NumbWord
Working with Comboboxes

I often find myself doing the same thing when I start working with files. I struggle to separate the path from the filename when filedialog returns a fully qualified filename (which it always does). Early in September Alyce wrote a quick little demo for someone in the Liberty Basic community and I asked her if I could include it in the newsletter.

I figure if I am always forgetting this and re-inventing it each time I need it, someone else is too. This little snippet of a program has two short functions. One returns the file name and the other the path when passed a fully qualified filename. The trick is the `\' character in the pathname. Both routines search from the end of the pathname string one character at a time looking for the backward slash. Once it is located the file name is all the characters to the left of it, and the path is all the characters to the right including the backward slash. Simple, clean, elegant code. Thanks Alyce for letting me share your work. Please use this where ever it is appropriate. We share it so others do not have to reinvent it. - Brad.

FileDialog "Select a file", "*.*", nameoffile$

Print "FileDialog returned the following: ";nameoffile$
Print
Print "Path of file is: ";SeparatePath$(nameoffile$)
Print "Filename is: ";SeparateFile$(nameoffile$)
Print
Input a$
End

Function SeparateFile$(f$)
   fileindex = Len(f$)
   filelength = Len(f$)
   While Mid$(f$, fileindex,1) <> "\"
      fileindex = fileindex - 1
   Wend
   SeparateFile$ = Right$(f$, filelength - fileindex)
End Function

Function SeparatePath$(f$)
   fileindex = Len(f$)
   While Mid$(f$, fileindex,1) <> "\"
      fileindex = fileindex - 1
   Wend
   SeparatePath$ = Left$(f$, fileindex)
End Function
  



Home

Paths and File Names
Observed online
Resources for the beginner
Graphics Drawing Rules
beginner's guide to API and DLL
Drawing IN MEMORY
Radiobuttons via API
NumbWord
Working with Comboboxes