3

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

3 Answers 3

4

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
2
  • 1
    I just re-used this code and if you don't apply a full path it doesn't work.
    – GeorgeC
    Commented Feb 22, 2017 at 5:10
  • i just figured out that you do not have to apply the full path - passing it with a variable works, but you need to build in a pause (like " Note "Text File 'Liste' was successfully created" ..the code is otherwise to fast for the filesystem i think :)
    – sn1ks
    Commented Oct 10, 2019 at 7:39
2

It looks to me as if you haven't "dim'ed" your path variable.

Try adding this Dim line just inside the Sub Main:

Sub Main

Dim path_to_temp_tables As String
...

And I would also add a backslash at then end of the path:

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

You loop thru the list of tables in this way:

Dim sFile As String
Fetch First From List
Do Until EOT(List)
   sFile = List.COL1

   Open Table sFile As __XXX
   Alter Table __XXX ( modify elev_text Char(4) ) Interactive
   Pack Table __XXX Graphic Data
   Close Table __XXX

   Fetch Next From List
Loop

Note that I expect the full path of the table to be in the first column (here COL1). If it's not in that column change the number to the right (COL1 to COL2 for instance).

2
  • thanks. That removes the error but I don't see anything happening. How do I get it to alter the table and pack it? See updated script.
    – GeorgeC
    Commented Aug 23, 2016 at 6:39
  • 1
    Here's an example on how to loop thru a table and get the values from a column: gis.stackexchange.com/questions/65497/… Commented Aug 23, 2016 at 7:32
2

My previous answer was building on your solution to use the Dir DOS command to get a list of files from a directory.

If you want to create a more stable solution, you can use one of the .NET methods out there for searching thru a folder for files. This will give you an array of files (or tables) that you can loop directly in your code.

If you get the FILELib.dll from the mbLibrary github repository and place it in the folder with your mbx, you can do like this:

Include "MapBasic.def"

Declare Method FILEFindFilesInFolder    '**Searches thru this specific folder only, no subfolders
   Class "FILELib.Files"  Lib "FILELib.dll" Alias "FindFilesInFolder"
   ( ByVal sPath As String      'Path to search
   , ByVal sMask As String      'Mask to use, eg. "*.*", "*.tab" etc
   ) As Integer
Declare Method FILEGetFindFilesFileName
   Class "FILELib.Files"  Lib "FILELib.dll" Alias "GetFindFilesFileName"
   ( ByVal nFileItem As Integer     'File number to get the name full path of
   ) As String

Declare Sub Main

Sub Main

   Dim path_to_temp_tables As String
   Dim nNumFiles, nFile As Integer

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

   nNumFiles = FILEFindFilesInFolder(path_to_temp_tables, "*.tab")

   For nFile = 1 To nNumFiles
      Open Table FILEGetFindFilesFileName(nFile) As __XXX
      Alter Table __XXX ( modify elev_text Char(4) ) Interactive
      Pack Table __XXX Graphic Data
      Close Table __XXXEnd Sub
   Next

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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