How can I loop through all my SRF files in a directory and export the contour maps to SHP?

This script loops through all the SRF files in a directory, and exports any contour maps within those SRF projects to Shapefile format.


To run this script:

  1. Save the attached file to your computer Loop SRFs export SHP contours.bas
  2. Open it in Scripter.
  3. Click Script | Run to run the script.


  1. Copy the script below.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer 15\Scripter.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press Delete.
  5. Press Ctrl+V to paste it into Scripter. 
  6. Click Script | Run to run the script.



Sub Main

'Opens all SRF file in directory, loops though Plot objects and exports contour maps to SHP
Debug.Clear ''''''''''''' User Variables '''''''''''''''''' file_extension = "srf" file_directory = "C:\Users\Justine\Desktop\Test\" outpath = "C:\Users\Justine\Desktop\TestOut\" ''''''''''''''''''''''''''''''''''''''''''''''' Set SurferApp = CreateObject("surfer.application") SurferApp.Visible = True 'Start the loop to open SRF files file_extension = LCase(Right(file_extension,(Len(file_extension) - InStrRev(file_extension,".")))) If Len(file_directory)-InStrRev(file_directory,"\") <> 0 Then file_directory = file_directory + "\" srf_file = Dir( file_directory + "*." + file_extension) On Error GoTo FileError While srf_file <> "" 'Declares Plot As an Object And opens an SRF file Dim Plot1 As Object Set Plot1 = SurferApp.Documents.Open(file_directory + srf_file) Dim ContourLayer As Object 'Loop through and find only map frames For i = 1 To Plot1.Shapes.Count If Plot1.Shapes.Item(i).Type = srfShapeMapFrame Then 'Once you find a map frame, loop through and find only contour layers For j = 1 To Plot1.Shapes.Item(i).Overlays.Count If Plot1.Shapes.Item(i).Overlays.Item(j).Type = srfShapeContourMap Then 'Once you find a contour layer, get the naming convention set up Set ContourLayer = Plot1.Shapes.Item(i).Overlays.Item(j) layer_name = ContourLayer.Name extension = Right(layer_name, Len(layer_name)-(Len(layer_name)-4)) 'Remove any .grd Contour layer extensions for output filename If extension = ".grd" Then layer_name = Left(layer_name, Len(layer_name)-4) End If srf_export_file = Left(srf_file, Len(srf_file)-(Len(srf_file)-InStrRev(srf_file,".")+1) ) + "-" 'Exports the contours to a 2D SHP file ContourLayer.ExportContours(FileName:=outpath + srf_export_file + layer_name + ".shp", Format:=srfConFormatShp2D) End If 'Find the next contour layer Next j End If 'Find the next Map Frame Next i Debug.Print srf_file srf_file = Dir() 'get next file Wend SurferApp.Quit Exit Sub 'Print a meaningful error message for each file that did not export correctly FileError: Debug.Print "Error: " + srf_file + " " + Err.Description Resume Next End Sub
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.