Combine multiple data files into a single file using Surfer automation

If you have multiple data files that you want to combine into a single file for plotting, gridding, or performing calculations, you can do so quickly with a Surfer script. You will need to put all of your data files in a single directory before running this script. This works on data files with the same file extension, and which do not have a header row (though it could be modified to accommodate these cases if needed).

To run this script:

  1. Copy the script below, or download the attached BAS file: CombineDataFiles.bas.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press DELETE.
  5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
  6. Change the file path, output file name, and data file extension in the User-defined variables section at the beginning of the script.
  7. Click Script | Run to run the script.

 


 

Sub Main

'Declare the variable that will reference the application
Dim SurferApp, Wks, WksRange As Object
Set SurferApp = CreateObject("Surfer.Application")
SurferApp.Visible = True

'===============================================
'User-defined variables
'===============================================
'Directory containing all of your data files
file_directory = "c:\surfer\"

'Name of the new, combined file
newfile = "combined_file.dat"

'File extension to loop through
file_extension = "dat"
'===============================================
	
'Opens the first data file in the worksheet
Set Wks =SurferApp.Documents.Add(srfDocWks)

'Adds a \ to the end of the file path if it doesn't have one already
If  Len(file_directory)-InStrRev(file_directory,"\") <> 0 Then file_directory = file_directory + "\"

'Declares the variable to loop over (all data files in the directory)
data_file = Dir( file_directory  + "*." + file_extension)

'Initializes the row to import data into as row 1
import_row = 1

'Loops through all data files in the specified directory
While data_file <> ""

	'Finds the last row in the current worksheet range
	Wks.Merge(file_directory+data_file,import_row,1)

	'Gets new row to import the next data file into
	import_row = Wks.UsedRange.LastRow+1

	'Gets the next data file
	data_file = Dir()
Wend

'Saves the combined data file
Wks.SaveAs(file_directory+newfile,srfSaveFormatDat)
End Sub

 

Updated November 2021

Was this article helpful?
0 out of 1 found this helpful

Comments

2 comments
Date Votes
  • Hi Olivier,

    Thank you for your comment! I'm able to run this script as I expect. Can you send me the data files you're attempting to combine and the script with your edits to support@goldensoftware.com? I'd like to take a look at both to see what may be causing you to get that error. 

    Best,

    Greg McCotter

    0
  • This works great now. Thank for you help! a very useful tool indeed.

    0

Please sign in to leave a comment.