<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="file" webkitdirectory id="file" />
<script>
const file = document.getElementById("file");
let pathArr = [];
file.addEventListener("change", function (event) {
Array.from(this.files).forEach((item) => {
pathArr.push(item.webkitRelativePath);
});
let InputPath = {};
for (let i = 0; i < pathArr.length; i++) {
const pathAndFile = pathArr[i].split("/");
let root = pathAndFile[0];
let second = pathAndFile[0] + "/" + pathAndFile[1];
let third = pathAndFile[0] + "/" + pathAndFile[1] + "/" + pathAndFile[2];
let filename = pathAndFile[pathAndFile.length - 1];
for (let j = 0; j < pathAndFile.length; j++) {
if (j === 0) {
if (InputPath.PathName) {
continue;
} else {
InputPath.PathName = root;
InputPath.PathInfo = [];
}
}
if (j === 1) {
if (
InputPath.PathInfo[InputPath.PathInfo.length - 1] &&
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathName === second
) {
continue;
} else {
InputPath.PathInfo.push({
PathName: second,
PathInfo: [],
});
}
}
if (j === 2) {
if (
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo.length > 0 &&
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo[
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo.length - 1
].PathName === third
) {
continue;
} else {
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo.push({
PathName: third,
fileNames: [],
});
}
}
if (j === 3) {
let index = 0;
if (
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo.length > 0 &&
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo[
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo.length - 1
].PathName === third
) {
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo[
InputPath.PathInfo[InputPath.PathInfo.length - 1].PathInfo.length - 1
].fileNames.push(filename);
} else {
continue;
}
}
}
}
console.log(InputPath);
});
</script>
</body>
</html>