07-24-2023, 10:21 AM
I am trying to retrieve all subfolders recursively into a character-delimited string "list" using server-side Javascript for Classic ASP. Here is my function that is supposed to do the trick:
function hasContent(text) {
if (text == undefined) {
return false;
} else if (text === undefined) {
return false;
} else if (text == "undefined") {
return false;
} else if (text === "undefined") {
return false;
} else if (typeof text == "undefined") {
return false;
} else if (text == null) {
return false;
} else if (text === null) {
return false;
} else if (text == "") {
return false;
} else if (text === "") {
return false;
} else {
return true;
}
}
function generateActualPath(path) {
try {
if (!hasContent(path)) {
return "./";
}
if (!/.+[\\/\\\\]$/.test(path)) {
path += "/";
}
if (!/^\\./.test(path)) {
path = "." + path;
}
return path;
} catch (e) {
return "./";
}
}
function retrieveDirectories(dir) {
try {
var actualPath = '/' + dir;
var obj = Server.CreateObject("Scripting.FileSystemObject");
dirs = obj.GetFolder(Server.MapPath(generateActualPath(actualPath)));
for (var enumm = new Enumerator(dirs.subFolders); !enumm.atEnd(); enumm.moveNext()) {
if (/^[a-zA-Z0-9\-_&%#]/.test(enumm.item().Name) && enumm.item().Name.trim().indexOf('_') != 0) {
if (hasContent(Session("directories"))) {
Session("directories") += enumm.item().Name + '|';
} else {
Session("directories") = enumm.item().Name + '|';
}
retrieveDirectories(enumm.item().Name);
}
}
} catch (e) {}
}
However, I only see the first-level subfolders of "dir" and nothing further, in spite of the recursive call. What might I be missing here? I verified that "dir" has up to 4 sub-levels of subdirectories that do not begin with "_" and contain legitimate alphanumeric characters per customer requirement.
function hasContent(text) {
if (text == undefined) {
return false;
} else if (text === undefined) {
return false;
} else if (text == "undefined") {
return false;
} else if (text === "undefined") {
return false;
} else if (typeof text == "undefined") {
return false;
} else if (text == null) {
return false;
} else if (text === null) {
return false;
} else if (text == "") {
return false;
} else if (text === "") {
return false;
} else {
return true;
}
}
function generateActualPath(path) {
try {
if (!hasContent(path)) {
return "./";
}
if (!/.+[\\/\\\\]$/.test(path)) {
path += "/";
}
if (!/^\\./.test(path)) {
path = "." + path;
}
return path;
} catch (e) {
return "./";
}
}
function retrieveDirectories(dir) {
try {
var actualPath = '/' + dir;
var obj = Server.CreateObject("Scripting.FileSystemObject");
dirs = obj.GetFolder(Server.MapPath(generateActualPath(actualPath)));
for (var enumm = new Enumerator(dirs.subFolders); !enumm.atEnd(); enumm.moveNext()) {
if (/^[a-zA-Z0-9\-_&%#]/.test(enumm.item().Name) && enumm.item().Name.trim().indexOf('_') != 0) {
if (hasContent(Session("directories"))) {
Session("directories") += enumm.item().Name + '|';
} else {
Session("directories") = enumm.item().Name + '|';
}
retrieveDirectories(enumm.item().Name);
}
}
} catch (e) {}
}
However, I only see the first-level subfolders of "dir" and nothing further, in spite of the recursive call. What might I be missing here? I verified that "dir" has up to 4 sub-levels of subdirectories that do not begin with "_" and contain legitimate alphanumeric characters per customer requirement.