Need help with some code to fill in and save forms to different folders
!
So, I'm trying to fill in a form I made with information in a txt file and then save it to one of a number of folders according to information in the form. I have a series of folders setup named after number ranges (i.e. 1-49, 50-99, 100-149, etc.) and some of the folders are preceded by the letter "z" because our numbering system for what we're tracking with these forms includes some names formatted with a Z at the end ( the people here before me kinda suck).
The logic of the code I'm trying to correct is such (if you just want to see the code it's below):
import text data within a while loop -> use indexOf to search the target field value for the letter Z -> use switch to setup 2 blocks of code, 1 for values that contain "z" and 1 for values without "z" -> determine which code block to run based on the "z" -> use substr(0,3) to pull the first three characters from the value and use that in the code blocks to compare to our number ranges to see which range the number falls in -> set a variable with a string according to which range it falls in (i.e. "100-149/") -> go to save the PDF -> use the previously set variable to amend the output directory of saveAs to which ever range it switched our variable to and save the PDF in the corresponding folder named as the number range -> increment a variable and repeat the loop
My problem now is that the variable we're setting with the switch code is coming out as just a zero. So instead of adding my string and making it create the PDFs in the correct subfolders, it just adds a 0 to the beginning of the file name and saves it in the top level folder. I thought I had my logic in the switch section correct but maybe I'm just not familiar enough with JS to see my issue.
If there's a better method/place of posting the code so it stays formatted like it is in the editor just let me know.
Code:
ImportES = app.trustedFunction(function() {
var fileName = "/c/Users/jcoleman/Desktop/Endless Slings/Endless Sling Data.txt";
var outputDir = "/c/Users/jcoleman/Desktop/Endless Slings/";
var whichCert = this.getField("PFFSID").value;
var firstThree = whichCert.substr(0,3);
var whichFolder = "";
var whichCase = whichCert.indexOf("Z");
var err = 0;
var idx = 0;
while (err == 0) {
err = this.importTextData(fileName, idx);
switch (whichCase) {
case 3: {
if (0 < firstThree && firstThree <49)
whichFolder = "z1-49/";
else if (49 < firstThree && firstThree <99)
whichFolder = "z50-99/";
else if (99 < firstThree && firstThree <149)
whichFolder = "z100-149/";
else if (149 < firstThree && firstThree <199)
whichFolder = "z150-199/";
else if (199 < firstThree && firstThree <249)
whichFolder = "z200-249/";
else if (249 < firstThree && firstThree <299)
whichFolder = "z250-299/";
else if (299 < firstThree && firstThree <349)
whichFolder = "z300-349/";
else if (349 < firstThree && firstThree <399)
whichFolder = "z350-399/";
else if (399 < firstThree && firstThree <449)
whichFolder = "z400-449/";
else if (449 < firstThree && firstThree <499)
whichFolder = "z450-499/";
else if (499 < firstThree && firstThree <549)
whichFolder = "z500-549/";
else if (549 < firstThree && firstThree <599)
whichFolder = "z550-599/";
else if (599 < firstThree && firstThree <649)
whichFolder = "z600-649/";
else if (649 < firstThree && firstThree <699)
whichFolder = "z650-699/";
else if (699 < firstThree && firstThree <749)
whichFolder = "z700-749/";
else if (749 < firstThree && firstThree <799)
whichFolder = "z750-799/";
else if (799 < firstThree && firstThree <849)
whichFolder = "z800-849/";
else if (849 < firstThree && firstThree <899)
whichFolder = "z850-899/";
else if (899 < firstThree && firstThree <949)
whichFolder = "z900-949/";
else if (949 < firstThree && firstThree <999)
whichFolder = "z950-999/";
}
break;
case -1: {
if (0 < firstThree && firstThree <49)
whichFolder = "1-49/";
else if (49 < firstThree && firstThree <99)
whichFolder = "50-99/";
else if (99 < firstThree && firstThree <149)
whichFolder = "100-149/";
else if (149 < firstThree && firstThree <199)
whichFolder = "150-199/";
else if (199 < firstThree && firstThree <249)
whichFolder = "200-249/";
else if (249 < firstThree && firstThree <299)
whichFolder = "250-299/";
else if (299 < firstThree && firstThree <349)
whichFolder = "300-349/";
else if (349 < firstThree && firstThree <399)
whichFolder = "350-399/";
else if (399 < firstThree && firstThree <449)
whichFolder = "400-449/";
else if (449 < firstThree && firstThree <499)
whichFolder = "450-499/";
else if (499 < firstThree && firstThree <549)
whichFolder = "500-549/";
else if (549 < firstThree && firstThree <599)
whichFolder = "550-599/";
else if (599 < firstThree && firstThree <649)
whichFolder = "600-649/";
else if (649 < firstThree && firstThree <699)
whichFolder = "650-699/";
else if (699 < firstThree && firstThree <749)
whichFolder = "700-749/";
else if (749 < firstThree && firstThree <799)
whichFolder = "750-799/";
else if (799 < firstThree && firstThree <849)
whichFolder = "800-849/";
else if (849 < firstThree && firstThree <899)
whichFolder = "850-899/";
else if (899 < firstThree && firstThree <949)
whichFolder = "900-949/";
else if (949 < firstThree && firstThree <999)
whichFolder = "950-999/";
}
}
if (err == -1)
app.alert("Error: Cannot Open File");
else if (err == -2)
app.alert("Error: Cannot Load Data");
else if (err == 1)
app.alert("Warning: User Cancelled File Select");
else if (err == 2)
app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
app.alert("Warning: Missing Data");
else if (err == 0) {
this.saveAs(outputDir + whichFolder + "ES-" + this.getField("PFFSID").value + ".pdf");
idx++;
}
}
});
