MapInfo Pro

 View Only

MapBasic Monday: Selecting Files and Folders

  • 1.  MapBasic Monday: Selecting Files and Folders

    Employee
    Posted 29 days ago
      |   view attached

    I wrote this article in response to a question recently on the MapInfo-L, the first mailing list for anything around MapInfo Pro.

    Richard was looking for a way to let the user select an output folder for tables created by his MapBasic tool. The question is quite common, so I thought it would be a good idea to introduce a few ways for the user to select files and folders through MapBasic.

    I'll introduce four methods below: two MapBasic built-in functions and two custom functions from the FILELib. FILELib is a module that is part of the Common MapBasic Library that you can find on GitHub.

    In my example, I have downloaded the FILELib.dll.def and the FILELib.dll files into the folder where my application resides. I have included the def til in my source code and can so reference functions in the DLL from my application. You can also bring down the entire Common MapBasic Library and use a MapBasic Project File to include various modules in your application development.

    In the examples below, I typically specify MapInfo Tables (*.TAB). But you can use these functions to let the user select any file type. You only have to specify the file type when using the function.

    Happy #MapInfoMonday

    FileOpenDlg() Function

    The FileOpenDlg() function is built into MapBasic and so very easy to use.

    You pass it four parameters: The initial path, the initial filename, the file type, and the title for the dialog.

    The function will return the name of the file selected by the user.

    If the user cancels the dialog, the function will return an empty string.

    Here's an example:

    	sPath		= ApplicationDirectory$()
    	sFileName	= ""
    	sFile 		= FileOpenDlg(sPath, sFileName, "TAB", "Select Table to Open...")
    	If sFile = "" Then
    		'**The user canceled the dialog
    		Exit Sub
    	End If
    	Print "Selected File: " & sFile
    	Open Table sFile
    	sTab		= PathToTableName$(sFile)

    Also, keep in mind that the function doesn't open the file. That's up to you. As you can see above, I use the Open Table statement to open the table selected by the user.

    The dialog looks like this

    Notice how the dialog has the same shortcuts as the normal Open dialog in MapInfo Pro.

    FileSaveAsDlg() Function

    The second method is the FileSaveAsDlg. This works similarly to the FileOpenDlg as it takes the same parameters.

    But where FileOpenDlg only allows you to select existing files, FileSaveAsDlg also allows you to specify not existing files simply by entering the filename in the File name field.

    	sPath		= PathToDirectory$(sFile)
    	sFileName	= PathToFileName$(sFile)
    	sFileName	= Left$(sFileName, Len(sFileName) - 4) & " Copy"
    	sFile		= FileSaveAsDlg(sPath, sFileName, "TAB", "Select Location and Name for Table to Save...")
    	If sFile = "" Then
    		'**The user cancelled the dialog
    		Exit Sub
    	End If
    	Print "Selected File: " & sFile
    	Commit Table sTab As sFile

    The first few lines above take the name of the first table and add " copy" to it as the default name when saving a copy.

    And just like before, the FileSaveAsDlg() doesn't save a copy of your table. You need to do this explicitly in your code as you can see in the last line above.

    The dialog looks like this

    If the user selects an existing file name, the dialog will also warn the user that this file may get overwritten.

    FILEOpenFilesDlg() Custom Function

    Sometimes selecting a single file just isn't enough. If you want to let the user select multiple existing files, you can use the FILEOpenFilesDlg function which is part of the FILELib.dll. The DLL holds several other methods for working with files and folders.

    The function FILEOpenFilesDlg() is based on a standard Windows dialog which gives you a bit more flexibility.

    It takes 3 parameters: A title for the dialog, an initial path to look for files, and a string representing the file type.

    	sPath		= PathToDirectory$(sFile)
    	nNumFiles 	= FILEOpenFilesDlg("Select Files to Process...", sPath, "MapInfo Tables (*.tab)|*.tab")
    	If nNumFiles = 0 Then
    		'**The user cancelled the dialog
    		Exit Sub
    	End If
    
    	For nFile = 1 To nNumFiles
    		sFile = FILEGetOpenFilesDlgFileName(nFile)
    		Print nFile & ". Selected : File: " & sFile
    		Open Table sFile
    	Next

    The function returns the number of files selected by the user. If the user cancels the dialog the function returns 0.

    After you get the number of files selected, you need to loop this number of times to get the name of the files selected. You can see how to do that in the last half of the example.

    You can also configure the dialog to allow the user to select multiple file types. You can use a file type like this to allow the user to select both MapInfo Tables and MapInfo Workspaces:

    "MapInfo files (*.tab,*.wor)|*.tab;*.wor"

    The dialog looks a bit different from the standard MapBasic dialogs. As you can see it allows you to select multiple files.

    FILEBrowseForFolder() Custom Function

    The final example I want to share allows the user to select a folder instead of a file. This is especially useful if you are looking for a location to create output files or a location that holds many files that need to be processed by your application.

    It takes a description and the starting folder as input.

    	sPath = FILEBrowseForFolder("Select Path to Store Output into", sPath)
    	If sPath = "" Then
    		'**The user canceled the dialog
    		Exit Sub
    	End If
    	Print "Selected Path: " & sPath

    The function returns the full path of the folder selected. It does return an empty string if the user cancels the dialog.

    One thing to keep in mind is that the path returned by this function doesn't have a backslash (\) at the end. Make sure you add this before concatenating the path with file names.

    The dialog looks like this

    I hope that you have found this useful. I have attached a zip file with my example code, the files FILELib.dll.def and FILELib.dll.


    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------

    Attachment(s)

    zip
    FilesAndFolders.zip   4 KB 1 version