
Function to repeat a command in all subfolders of a parent folder

topchem
When you just want to quickly repeat a simple command on all your data without writing a procedure.
//=================================================================================================================== // Function DoAllFolders // Version 1.0.3 // Created : 2014-07-24 // Last Modified : 2016-01-07 // Input : A command string typically copied from the history window // Calls : // Returns : -1 if procedure does not complete. Nothing if it finishes // Output : Executes the command string on all subfolders in the current folder. 1 level deep (does not recurse) // Requires : Because of the use of the DFR type, Igor 6.1 or higher is required. // The purpose of this function is to allow exploratory data analysis on repetitive data sets stored in separate data folders. // Typically this gets invoked after performing an operation on one data set that should then be repeated on all others. // The command may be copied from the history window or command line and pasted into the prompt displayed by the function // The command needs to be called from the parent folder // The operation is then invoked on all subfolders using the "Execute" command // All subfolders should have identically structured data (or at least should have the data structures used by the command) // Also, the command will be executed in all folders, including the one that had the original operation. For some functions it may be necessary to set // flags to allow overwriting of variables/string/waves created by the original operation. // The command string must be entered in string syntax, i.e. quotes and backslashes must be escaped using \ // Revision history // 1.0.1 : Turned on Preferences before executing the command, so things behave more as if executed from the Command Line (especially graphs) // 1.0.2: Added reference to parent folder into the reporting string at the end of the routine. That way the history shows where the command was executed. // 1.0.3 2016-01-07 : Made the Command String a parameter to the function, so it can be passed as a call. Which means that now you can make a menu entry // that runs a canned command over a set of folders. Slick. //=================================================================================================================== Function DoAllFolders(CommandString) String CommandString //The Command to be executed for each folder DFREF HomeFolder //Starting folder reference HomeFolder = GetDataFolderDFR() //Save the starting folder //Count the number of subfolders Variable FolderCount = CountObjectsDFR(HomeFolder,4) //return the number of datafolders if (FolderCount < 1) //Check that there are subfolders Print "Error: No subfolders to process" return -1 endif //We have some subfolders to process, so now get the command to execute if(stringmatch(CommandString,"")) //If an empty string was passed, we prompt for the command Prompt CommandString, "Command:" DoPrompt "Enter the Command to be Executed" CommandString //Prompt for the Command String. Note that this requires "\" escape codes for all special characters if (strlen(CommandString) ==0) //If no command was entered, treat it as if the user canceled (set V_Flag to 1) V_Flag = 1 endif if (V_Flag) Print "No Command entered" return -1 // User canceled endif endif //Turn on Preferences so all commands are executed as if from the Command line Preferences 1 //Traverse the subfolders and execute the Command in each //If the command is buggy, the routine will error at execution Variable i = 0 //index variable for loop String CurrentFolder //Set a string variable for the current folder name do CurrentFolder = GetIndexedObjNameDFR(HomeFolder, 4, i) //Pick the current subfolder SetDataFolder CurrentFolder Execute/Q CommandString //Execute the Command string as if it had been entered on the command line. Do not echo to history SetDataFolder HomeFolder //Return to starting folder i +=1 //Advance to the next folder while (i < FolderCount) // as long as expression is TRUE //Now print a message to the history window so we can track what happened since we suppressed history for the Execute command Printf "Executed Command '%s' in %g subfolders of '%s'\r", CommandString, FolderCount , GetDataFolder(0) End

Forum

Support

Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More