cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
sandersjrs
Level II

Cycling through folders in a directory

Hi 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 <path 1 = Convert File Path( "" );>, 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!

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Cycling through folders in a directory

The 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
);

View solution in original post

Craige_Hales
Super User

Re: Cycling through folders in a directory

to 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<<graphbuilder(...), etc.

 

Craige

View solution in original post

6 REPLIES 6

Re: Cycling through folders in a directory

The 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
);
Craige_Hales
Super User

Re: Cycling through folders in a directory

And 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.

Craige
sandersjrs
Level II

Re: Cycling through folders in a directory

Thanks for the tips!

sandersjrs
Level II

Re: Cycling through folders in a directory

Thanks for the response - will try soon!

sandersjrs
Level II

Re: Cycling through folders in a directory

Jed,

 

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).

Craige_Hales
Super User

Re: Cycling through folders in a directory

to 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<<graphbuilder(...), etc.

 

Craige