MapInfo Pro Developers User Group

Welcome to the MapInfo Pro Developers community!  We are a group dedicated to the discussion and understanding of MapBasic and .Net MapInfoPro AddIn development. Bring your questions and ideas here. This group includes several members of the Pro development team from around the world.

Please feel free to start a discussion in the discussion tab or join in a conversation.

Product Information  Ideas Portal  MapInfo Community Downloads

Discussions

Members

Resources

Events

 View Only
  • 1.  SearchForFiles

    Employee
    Posted 06-29-2018 09:45
      |   view attached

    Finding files in Folders using MapBasic

    I have a few times seen requests for searching for files from a MapBasic application. The use case could be that you have a batch process that runs thru all tab files in a specific folder. In these cases the tables you need to work on are not open in MapInfo Pro and you might not even know their names.

    So how would you get a list of these files? Here is one way.

    I have created a .NET assembly that you can use from within your MapBasic sourcecode. The assembly has a number of methods but the specific methods we are interested in here are:

    • Method FILEFindFilesInFolder(sPath, sMask) As Integer
    • Method FILEFindFilesInFolders(sPath, sMask) As Integer
    • Method FILEGetFindFilesFileName(nFileItem) As String

     

    The first two can be used to search for files. The first looks in the specified folder only. The second also looks in subfolders. The both return the number of files found.

    The last method is used to get the file names found.

    Here's how to use the methods in MapBasic.

    nNumFiles = FILEFindFilesInFolder(sPath, sMask)

    '**If no files where found, nNumFiles is 0

    If nNumFiles = 0 Then

    Note "No files where found in this folder: " & sPath

    Exit Sub

    End If

    '**Just a basic example of how to get the file names found

    '**You'd probably do something a bit more clever with the files.

    For nFile = 1 To nNumFiles

    Print nFile & ": " & FILEGetFindFilesFileName(nFile)

    Next

    I have included a very basic application that shows how this works and how to declare the .NET methods in your MapBasic application.

    If you are using mbLibrary (the MapBasic Common Libraries from Community Download: http://communitydownloads.pbinsight.com/code-exchange/download/mapbasic-common-libraries), the methods are declared in the FILELib.def. Just include that file to get access to the methods.

    Attachment(s)

    zip
    SearchForFiles.zip   4 KB 1 version