[GIS] Mapbasic cycle through directory of tab files

mapbasic

I want to cycle through a directory of .tab files and then run a process on the files. I found https://groups.google.com/forum/#!topic/mapinfo-l/reYENsRRziM but can't figure out how to define the path properly.

Once this is done I need it to alter and pack the tables.

Include "MapBasic.def"

Declare Sub Main

Sub Main
path_to_temp_tables = "Z:\projects\1_DSC24_Contours\Working\live_environment\5m\data\scratch"

'Run the Dos Command
Run program "Cmd.exe /c dir """+path_to_temp_tables+"*.TAB"" /b /w  > """+path_to_temp_tables+"Liste.txt"""

'Registering a text file in MIpro
Register Table path_to_temp_tables+"Liste.txt"  TYPE ASCII Delimiter 9 Charset "WindowsLatin1" Into path_to_temp_tables+"Liste.TAB"

'Opening the new TAB file based on your text file
Open Table path_to_temp_tables+"Liste.TAB" as Liste
'Alter Table "Liste" ( modify elev_text Char(4) ) Interactive
'Pack Table Liste.TAB Graphic Data
End Sub

Liste above should be the original name of the input file.

I get enter image description here

I have tried a trailing slash and putting the files in the same directory and then

path_to_temp_tables = ""

…but I get the same issue.

===== UPDATED SCRIPT ====

The dim removes the error but I only see the .txt file being created. How do I get it to cycle through the files and then on each file it should alter the table and pack it? See updated script.

Include "MapBasic.def"

Declare Sub Main

Sub Main
Dim path_to_temp_tables As String
path_to_temp_tables = "Z:\projects\1_DSC24_Contours\Working\live_environment\5m\data\scratch\"

'Run the Dos Command
Run program "Cmd.exe /c dir """+path_to_temp_tables+"*.TAB"" /b /w  > """+path_to_temp_tables+"List.txt"""

'OPEN first file in List.txt
 'Alter Table "XXX" ( modify elev_text Char(4) ) Interactive
 'Pack Table XXX.TAB Graphic Data
'CONTINUE on next file
End Sub

Best Answer

First thing you need to do is either register the text file as a mapinfo table or, better in my opinion, just read the text file directly to get the table paths from it. Then, loop through the table paths and open, alter and pack each table. The below should do the job for you, just replace the text file path with the code you used to build your text file and pass the file's path to the ReadTextFileToArray sub.

Include "MapBasic.def"

Declare Sub Main
Declare Sub ReadTextFileToArray(ByVal txtFilePath as String, myArray() as String)

Sub Main()

Dim myArray() as String    '// create an array to store the table paths
Dim tabName, tabPath, txtFile as String
Dim i as Integer

'// test file
txtFile = "C:\Temp\test.txt"

'// create your list of paths text file... then:

Call ReadTextFileToArray(txtFile, myArray) '// populate your array with the table paths from the text file

For i = 1 to UBound(myArray)
    If right$(myArray(i), 3) = "TAB" then   '// check that this is a tab file path
        tabPath = myArray(i)    '// get path from array
        Open table tabPath      '// open table
        tabName = TableInfo(0, TAB_INFO_NAME)   '// get table name
        Alter table tabName(modify elev_text Char(4))   '// alter table
        Pack table tabName  Graphic Data    '// pack table
    End if
Next    '// go to next element in array

End Sub

Sub ReadTextFileToArray(ByVal txtFilePath as String, myArray() as String)

Dim i as Integer

    i = 1

    Open file txtFilePath for input as #1   '// open text file for input (to read) as #1
    Do Until EOF(1)             '// do until the end of file #1 is reached
        Redim myArray(i)        '// resize array to i
        Input #1, myArray(i)    '// read value from text file
        i = i + 1               '// increment i
    Loop                        '// return to Do Until...

End Sub
Related Question