If you have multiple data files that you want to combine into a single file for plotting, gridding, or performing calculations on, you can do so with Surfer. 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:
- Copy the script below, or download the attached BAS file: CombineDataFiles.bas.
- In a Windows Explorer window, navigate to C:\Program Files\GoldenSoftware\Surfer 15\Scripter.
- Double click on Scripter.exe to launch Scripter.
- Press Ctrl+A to select all of the existing lines then press DELETE.
- 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.
- Change the file path, output file name, and data file extension in the User-defined variables secdtion at the beginning of the script.
- Click Script | Run to run the script.
Sub Main 'Declare the variable that will reference the application Dim SurferApp, Wks As Object Set SurferApp = CreateObject("Surfer.Application") SurferApp.Visible = True '=============================================== 'User-defined variables '=============================================== 'Directory containing all of your data files file_directory = "c:\users\leslie\desktop\new folder\" '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 <> "" 'Declares WksRange as an object Dim WksRange As Object '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 'Gets the next data file data_file = Dir() Wend 'Saves the combined data file Wks.SaveAs(file_directory+newfile,srfSaveFormatDat) End Sub