How do I open several shapefiles, save as Tab files then append as one table using MapBasic code?

I have created a list that contains the tables and path to tables. But cannot loop to save tables individually.

I have reviewed many of your posted answers (really good answers) but still cannot find solution.

  • look at ogr2ogr
    – Ian Turton
    Feb 27 '18 at 7:41

You need to look at a few MapBasic statements

  1. You need to loop as many times as you have files. Which method you want to use depends on how you have your list of files

    • Do Until EOF() can be used if you are reading your file names from a file
    • For Next is easy to use if your file names are stored in an array in your MapBasic program
  2. If your list of file names is stored in a file, you need to open this file and read the file names one at a time. Look at these statements:

    • Open File
    • Input #
  3. To access a Shape file in MapInfo Pro, you first need to register a tab file holding information describing how to access and read the Shape file.

    • Register Table is the statement you need to use to create the tab file for each Shape file
  4. Finally you need to open the Shape files using the tab file you just created

    • Open Table is used to open any tab file no matter what data type is refers to.
  5. For the first table, you want to make a copy of it in the form of a Native MapInfo tab file and then you need to open this one again.

    • Commit Table lets you save a copy of the Shape file into a Native tab file
    • Open Table is also used to open Native tab file
  6. For the remaining Shape files, you can insert their records into the Native tab file you created using the first Shape file

    • Insert Into Select From is a Insert structure that can be used to insert all records from one table into another table
    • Commit Table is also used to save changes to a table.

Something along these lines:

Dim sOutputTabFile, sTempTabFile, sInputShpFile As String
Dim bTabCreated As Logical

Open File "C:\inputtablename.txt"
   For Input
   As #579

'**This is the final merged table
sOutputTabFile = "C:\MergedTable.tab"
'**This is just a temporary tab file
sTempTabFile = PathToDirectory$(TempFileName$("")) & "temptable.tab"

Do While Not EOF(579)
   '**I expect each line to just hold a Shape file name
   Line Input #579, sInputTabFile
   If Not EOF(579) Then
      '**Creating Tab file for current Shape file
      Register Table sInputShpFile 
        Type ShapeFile 
        '<...> You need to specify the Shape settings here
        Into sTempTabFile

      '**Opening the table using an Alias   
      Open Table sTempTabFile As "TempTab"

       If bTabCreated Then
          '**Inserting the records from the Shape fiel into the merged tab file
          Insert Into OutputTab
             Select * From TempTab
           '**Creating a copy in native format
          Commit Table TempTab
             As sOutputTabFile
             Type NATIVEX  '** As Type NATIVEX, bigger than 2 GB
          '**Opening the table using an Alias   
          Open Table sOutputTabFile As "OutputTab"   

          bTabCreated = true   
       End If         
       '**Close the table referring to the current shape file
       Close Table TempTab
   End If

Close File #579   

Commit Table OutputTab
Close Table OutputTab
Open Table sOutputTabFile
  • Hi Peter, can loop to open files but can't figure out how save with unique names. My Loop goes through creates the same file name with each table's data but I end up with last file in list as final table. Need to read list and capture each row in list'st table name and commit with that table's name. Can't seem to grab the table name in my loop to save as unique value based on record in the table.
    – Donna
    Feb 27 '18 at 18:21
  • Have a look at the small code example that I added Feb 28 '18 at 9:48
  • Hi Peter, Excellent. Mission accomplished. Was the use of #579 arbitrary or does that have a specific use. I have never seen it. Though my Mapbasic knowledge is limited. Thanks very much!
    – Donna
    Feb 28 '18 at 15:49
  • #579 is the number I assign to the file. It can be any number - I just try to stay away the obvious such as 1, 2 and 3 :-) Feb 28 '18 at 15:53
  • 1
    No. It did not. But I finally figured out that it was pointing to an old Liste file. I could not get it to create the new file so I started a new version of the program. That cleared everything up. Thanks.
    – Donna
    Apr 16 '18 at 22:58

If it doesn't need to be through mapbasic code? It can be achieved quite quickly and easily using ogr2ogr.

The following.bat file will achieve what you want using ogr2ogr.

Variables to set are sourcefolder and mergedfile

SET sourcefolder=C:\temp\ToMerge
SET mergedfile=C:\temp\Merged\merged.shp

for  %%f in (%sourcefolder%\*.shp) do (
    ECHO %%f

    if exist %mergedfile%  (
                    ogr2ogr -f "ESRI Shapefile" -update -append "%mergedfile%" "%%f" -nln merged 
                    ECHO "%%f APPENDED"

    if not exist %mergedfile% (
        ogr2ogr -f "ESRI Shapefile" "%mergedfile%" "%%f"


ogr2ogr -f "MapInfo File" C:\temp\Merged\merged_astab.tab %mergedfile%

  • Yes, needs to be MapBasic for use with MapInfo. Attempting to avoid manually opening 100's of files to save as tab files in MI and change the structure to reorder columns for same 100's of tables. Reading file names from a test file generated using Dos dir cmd.
    – Donna
    Feb 27 '18 at 18:17
  • I took a look at ogr2ogr and found I need to spend more time to understand the product and usage. Looks interesting. Thanks for the suggestion. - Donna
    – Donna
    Mar 6 '18 at 14:14
  • Great, going forward it should prove to be very useful. It is always good to know what is out there I guess, more tools at your disposal.
    – Cushen
    Mar 6 '18 at 22:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.