Hello!
New user and new to the forum and need some advice from the experts. Just starting to explore switch and the scripting capabilities. My scripting experience is very limited but I am learning as I go. I have completed the scripting in switch documentation and looking to use scripting to solve this problem.
Currently using an inject job element in a flow. The job being injected contains miscellaneous files at the root level and sub-folders with files. I would like the script to remove / filter all sub-folders and files in those sub-folders, while only keeping jpeg file/s at the root level of the job. Particularly jpegs with "TDS" in the filename. I am able to filter jobs that do not contain sub-folders with regular expressions ^(.*TDS.*)+(.jpg|.jpeg)$ and get the desired results. It is when the inject element introduces jobs with sub-folders that things get messy.
I have searched this forum and have not found something close enough to use as a starting point. If anyone can point me in the right direction or lend a hand to get the basic script started that would most appreciated.
Delete sub-folders and ignore all files except jpegs
Re: Delete sub-folders and ignore all files except jpegs
This script should achieve what you're wanting:
I've been testing with some test job folders that I made that contained jpeg/jpg with and without TDS in the file name as well as subfolders and text documents, this script will output the job folder with only jpeg/jpg files containing TDS in the file name.
Code: Select all
// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job ) {
// Create new directory object from incoming folder path
var jobFolder = new Dir(job.getPath());
// Create array of files in the job folder
var files = jobFolder.entryList("*.*", Dir.Files|Dir.NoDotAndDotDot, Dir.Name);
// Create new job folder path to copy files to
var newJobPath = job.createPathWithName(job.getNameProper(), true);
var fileMatchingRegEx = false;
// Loop through files in job folder
for (var i = 0; i < files.length; i += 1) {
// Check current file name against RegEx
if (files[i].find(/^(.*TDS.*)+(.jpg|.jpeg)$/) !== -1) {
fileMatchingRegEx = true;
// Copy to new job folder
s.copy(job.getPath() + "/" + files[i], newJobPath + "/" + files[i]);
}
}
// Check that file matching RegEx was found
if (fileMatchingRegEx) {
// Send to success connection
job.sendToData(1, newJobPath);
job.sendToNull(job.getPath());
} else {
// Send to data connection
job.log(3, "No files matching RegEx were found");
job.sendToData(3, job.getPath());
}
}
Re: Delete sub-folders and ignore all files except jpegs
Use the "Delete files" app that is part of the bundle "Folder apps". With this app you can easily keep all the jpg files you want and with the "Rename job" element you can flatten the folder hierarchy of the job folder.
Re: Delete sub-folders and ignore all files except jpegs
mkayyyy,
Your script worked beautifully. Thank you for this and the comments are very helpful. I do have a question. What part filters out folders below the root folder level? The script works as expected I am just trying to understand how. Does it have to do with the directory class enumeration via the .entryList values?
freddyp,
I installed the "Folder app" and tried to achieve the same result as the script. I was unsuccessful, since there was no way to delete the sub-folders and only keep the jpegs form the root folder. The "Rename job" element did flatten the hierarchy but was unable to filter out the root level jpeg. However I see many uses for the flow elements offered in the app and plan on using them in future flows.
Thank you for all your help!
Your script worked beautifully. Thank you for this and the comments are very helpful. I do have a question. What part filters out folders below the root folder level? The script works as expected I am just trying to understand how. Does it have to do with the directory class enumeration via the .entryList values?
freddyp,
I installed the "Folder app" and tried to achieve the same result as the script. I was unsuccessful, since there was no way to delete the sub-folders and only keep the jpegs form the root folder. The "Rename job" element did flatten the hierarchy but was unable to filter out the root level jpeg. However I see many uses for the flow elements offered in the app and plan on using them in future flows.
Thank you for all your help!
Re: Delete sub-folders and ignore all files except jpegs
Glad I could help! Yes, its the FilterSpec enums used in the entryList function that filtered out folders in the root level.A_ton wrote: ↑Mon Oct 07, 2019 8:13 pm mkayyyy,
Your script worked beautifully. Thank you for this and the comments are very helpful. I do have a question. What part filters out folders below the root folder level? The script works as expected I am just trying to understand how. Does it have to do with the directory class enumeration via the .entryList values?
By using the Dir.Files and Dir.NoDotAndDotDot FilterSpecs, an array of only files and no "." and ".." special entries will be returned.