Load each file into separate data folder

Average rating
(1 vote)

#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
	// 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
		SetDataFolder $dfName
		NewDataFolder/S $dfName
	String dataSetName = dfName
	*** Put your LoadWave command here ***
	SetDataFolder ::								// Restore current data folder
	return 0									// Success
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.
	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
		thePath = "data"
	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 = ""
		fileName = StringFromList(fileIndex, fileList)			// Get name of next text file in path
		if (strlen(fileName) == 0)
			break											// All done
		if (LoadOneFile(thePath, fileName) != 0)
			break			// Error
		fileIndex += 1
	while (1)												// Until all files done
	Printf "Loaded data from \"%s\"\r", dirPath
	return 0												// Success

Back to top