Combine multiple data files into a single file using Surfer automation

Combining multiple data files into a single file is a common task when preparing data for gridding, mapping, or analysis in Surfer. This script automates the process by merging all data files with the same extension from a specified directory into one consolidated file. The script works with any delimited text format (DAT, CSV, TXT) and preserves all data while combining files sequentially. This is particularly useful when you have collected data from multiple sources or time periods that need to be analyzed together.

To run this script:

  1. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer.
  2. Double click on Scripter.exe to launch Scripter.
  3. Copy and paste the script below into Scripter's code window (be sure to delete any existing lines first), or simply download the attached BAS file and open it in Scripter.
  4. Update the values in the USER-DEFINED VARIABLES section near the top of the script as needed. If you skip this step, the script will still run using the current values for these variables.
  5. Ensure all data files you want to combine are in the same directory and have the same file extension before running the script.
  6. Click Script | Run to run the script.

Sub Main
    
    'Clear any output in the Immediate window
    Debug.Clear
    
    'Initialize Surfer
    Dim SurferApp As Object
    Set SurferApp = CreateObject("Surfer.Application")
    SurferApp.Visible = True

    '===============================================
    'USER-DEFINED VARIABLES
    '===============================================
    'Directory containing all of your data files
    fileDirectory = "C:\Temp\"
    
    'Name of the new, combined output file
    outputFileName = "combined_data.dat"
    
    'File extension to search for (without the dot)
    fileExtension = "dat"
    '===============================================

    'Create a new worksheet to hold the combined data
    Dim Wks As Object
    Set Wks = SurferApp.Documents.Add(DocType:=srfDocWks)

    'Ensure the directory path ends with a backslash
    If Right(fileDirectory, 1) <> "\" Then
        fileDirectory = fileDirectory + "\"
    End If

    'Find the first data file in the directory
    dataFileName = Dir(fileDirectory + "*." + fileExtension)
    
    'Initialize the row counter for importing data
    importRow = 1
    fileCount = 0

    'Loop through all data files in the specified directory
    While dataFileName <> ""
        
        'Display progress information
        fileCount = fileCount + 1
        Debug.Print "Processing file " + Str(fileCount) + ": " + dataFileName

        'Merge the current data file into the worksheet
        Wks.Merge(FileName:=fileDirectory + dataFileName, _
                    Row:=importRow, Col:=1)

        'Update the import row for the next file
        importRow = Wks.UsedRange.LastRow + 1

        'Get the next data file in the directory
        dataFileName = Dir()
    Wend

    'Check if any files were processed
    If fileCount = 0 Then
        MsgBox "No files with extension '" + fileExtension + "' found in directory: " + fileDirectory, _
               vbExclamation, "No Files Found"
        Exit Sub
    End If

    'Save the combined data file
    outputFilePath = fileDirectory + outputFileName
    Wks.SaveAs(FileName:=outputFilePath, FileFormat:=srfSaveFormatDat)
    
    'Display completion message
    MsgBox "Successfully combined " + Str(fileCount) + " files into: " + outputFilePath, _
           vbInformation, "Files Combined"
    
    Debug.Print "Script completed successfully!"

End Sub
Was this article 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.