TIPCORNER - FILEDIALOGS

Home

API File Search
Filedialogs
The FILES statement
The FILES statement in action
Open Source Editor for WinXP
Spotlight on John Fisher
Using Winsock
Winsock API Reference

It is possible to use multiple extensions in a filedialog. The code here will show both BAS and LBA files in the dialog:

filedialog "Open file..",filePath$+"*.bas;*.lba",file$    

To show more than one extension, be sure that each one is in the form that has an asterisk, then a dot, then the desired extension - "*.ext". Separate these extensions with semi-colons. Here are some possible template strings for extensions:

"*.txt;*.bas;*.dat;*.lba"
"*.bmp;*.jpg;*.gif"
"*.ini;*.log"


We can have a filedialog open in the directory of our choice by including a path in the template string.

filedialog "Open","c:\myfiles\*.txt",file$

The template string can be a concatenation of two strings. If it is, then be sure that the path part of the string contains a backslash.

Correct:
     filedialog "Open","c:\myfiles\"+"*.txt",file$
Also Correct:
     filedialog "Open","c:\myfiles" +"\*.txt",file$    

Incorrect (lacks a backslash): filedialog "Open","c:\myfiles"+"*.txt",file$

The path part of the string can be a string variable. To open a file in a program's default directory:

Correct:
     filedialog "Open",DefaultDir$ +"\*.txt",file$ 

Incorrect (lacks a backslash): fildialog "Open",DefaultDir$ + "*.txt", file$


We can take a filename that is returned from a filedialog and separate the path from the filename. This is useful when we want to use the FILES command to check for a file's existence. Here are two Liberty BASIC functions that accept an
argument that is a filename returned from a filedialog. They both parse the string with WHILE/WEND loops, looking for the final instance of a backslash. They begin at the end of the filename and check each character to see if it is a backslash, moving to the next character to the left with each iteration. This check is done with the Mid$() function. When a backslash character is found, the loop terminates and the index variable contains the position within the filename string for the final backslash.

The SeparateFile$ function returns the information after the final backslash, which is the filename alone. The SeparatePath$ function returns the information before the final backslash, which is the pathname.

Separate path and filename:

 shortFile$=SeparateFile$(file$)
     filePath$=SeparatePath$(file$)

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$) filelength=len(f$) while mid$(f$, fileindex,1)<>"\" fileindex=fileindex-1 wend SeparatePath$=left$(f$,fileindex) end function


Once we have the pathname and filename separated, we can check for file's existence before attempting to open it. We use the FILES statement to do this.

We have created a special function in the Open Source Editor to check for a file's existence. If the file doesn't exist,
we don't attempt to open it, because that would cause a program crash!

 if FileExist(filePath$,shortFile$)<1 then
     notice "Error"+chr$(13)+"File does not exist."
     wait
     end if

The FileExist function accepts arguments of the pathname and the filename, then uses the FILES statement to check for the existence. If the value of info$(0,0) is 0, then the file doesn't exist.

function FileExist(fPath$,fFile$)
     files fPath$,fFile$,info$(
     FileExist=val(info$(0,0))
     end function

Home

API File Search
Filedialogs
The FILES statement
The FILES statement in action
Open Source Editor for WinXP
Spotlight on John Fisher
Using Winsock
Winsock API Reference