Cycling through folders in a directoryHi all,
I have a script that works on a set of data that my I get from a cyclic loading machine. However, I receive batches of data from this machine that is organized into folders containing the .txt file that I want to run the script on. Is there a way that I can paste the script file into the parent folder containing all of the subfolders that contain the .txt files and have it run through each folder (opening the .txt files in the folders and running the script on each .txt file as it goes)?
I have the script set up right now to where it can return the current file path where the script is located by using , and right now, I have to explicitly define (type out in the script) the file name to open up the data file.
Using JMP 17.
Thanks in advance!
Content OrganizationRe: Cycling through folders in a directoryto expand @Jed_Campbell example a bit,
for each ({filename}, lstFiles,
//your script for each file
dt = filename; // it isn't really a name, it is a data table handle
... do something with this open data table that dt points to
close( dt, "nosave" ); // if you are done with it
);
Copying filename to dt is just to make it clear what the list element is. You might dt<
Re: Cycling through folders in a directoryJed,
Seems like the multiple file import data is working, however, I cannot get the "for each" function to work. It seems like it is not iterating through the list of files created with Multiple File import - I am getting the same return value from my script 33 times (33 is the number of items in the list of files).
Re: Cycling through folders in a directoryThanks for the tips!
Re: Cycling through folders in a directoryThanks for the response - will try soon!
Re: Cycling through folders in a directoryAnd if MFI doesn't do what you need, FilesInDirectory(...) returns a list of files and directories. The isFile() and isDirectory() and FileCreationDate() and FileModificationDate() may help you get what you need. (approximate names, check the scripting index.) I think FilesInDirectory does not return directories when the recursive option is used. There may be items in the list that are not files or directories, so use the test that makes the most sense. But if MFI will do it, you'll have a simpler JSL script.
Re: Cycling through folders in a directoryThe Multiple File Import() function is a fantastic shortcut for this, as it returns a list of files. I'd suggest doing the import via the GUI first, then just copying the script and modifying it as necessary. Pseudo-script below:
m = Multiple File Import (lots of settings);
lstFiles = m << import data;
for each ({filename}, lstFiles,
//your script for each file
);