Load each file into separate data folder

#pragma rtGlobals=3     // Use modern global access method and strict wave access.

// This is a general framework for loading waves from multiple files, with the waves from
// each file stored in a separate data folder.
//
// This is useful when you have many similar files, each of which containing many columns.
// You wind up with one data folder for each file. The wave names in each data folder are the same.
// The data folder name identifies the source file.
//
// You need to supply your LoadWave command where it says
// "*** Put your LoadWave command here ***"
// Other changes may be required for your situation.

static StrConstant kFileNameExtension = ".txt"

Function LoadOneFile(pathName, fileName)
    String pathName         // Name of symbolic path or "" for dialog
    String fileName             // Name of data file or "" for dialog

    Variable refNum

    // First get a valid reference to a file.
    if ((strlen(pathName)==0) || (strlen(fileName)==0))
        // Display dialog looking for file.
        Open/D/R/P=$pathName/M="Select data file" refNum as fileName
        fileName = S_fileName       // S_fileName is set by Open/D
        if (strlen(fileName) == 0)  // User cancelled?
            return -1
        endif
    endif
   
    // Create data folder from file name
    String dfName = ParseFilePath(0, fileName, ":", 1, 0)
    dfName = RemoveEnding(dfName, kFileNameExtension)       // e.g., "MyFile.txt" -> "MyFile"
    if (DataFolderExists(dfName))
        String prompt
        sprintf prompt, "Data folder named '%s' exists. Click Yes to overwrite the existing data, No to cancel.", dfName
        DoAlert 1, prompt
        if (V_flag == 2)
            Print "Load cancelled. You can rename the existing data folder and try again."
            return -1                           // Cancelled
        endif
        SetDataFolder $dfName
    else
        NewDataFolder/S $dfName
    endif

    String dataSetName = dfName
   
    *** Put your LoadWave command here ***
   
    SetDataFolder ::                                // Restore current data folder
   
    return 0                                    // Success
End

Function LoadAllFilesInFolder()
    String thePath="_New Path_"
    Prompt thePath, "Name of path containing data files", popup PathList("*", ";", "")+"_New Path_"
    DoPrompt "Choose directory containing a set of data files", thePath
    if (V_flag != 0)
        return -1                                       // User cancelled.
    endif
   
    if (CmpStr(thePath, "_New Path_") == 0)     // User selected new path ?
        NewPath/O data                              // This brings up dialog and creates or overwrites path
        if (V_flag != 0)
            return -1                                   // User cancelled
        endif
        thePath = "data"
    endif
    PathInfo $thePath
    String dirPath = S_path
   
    String fileList = IndexedFile($thePath, -1, kFileNameExtension)     // List of all files in the folder

    String fileName
    Variable fileIndex=0
   
    String list = ""

    do
        fileName = StringFromList(fileIndex, fileList)          // Get name of next text file in path
        if (strlen(fileName) == 0)
            break                                           // All done
        endif
       
        if (LoadOneFile(thePath, fileName) != 0)
            break           // Error
        endif
       
        fileIndex += 1
    while (1)                                               // Until all files done
   
    Printf "Loaded data from \"%s\"\r", dirPath
   
    return 0                                                // Success
End

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More