I am attempting to read the names of files within a zipped folder on Google Drive. From there I will create new folders using a trimmed portion of the file name.
My below codes works, however the way I am reading the filename is to unzip it first, copy it to a temp location and read the filename. This function runs pretty slowly, my zipped file has 800+ files. Each one with a unique filename.
The code below first checks in the folder destination if a folder with yesterdays date as the title exists, if not it creates one. Then it checks a set folder for a zip file who's title contains yesterdays date. After finding it, it will loop through the zip file and extract each file within (jpegs) save them to a folder 'Temp' and retrieve a substring of the name. Then it checks the appropriate date folder to see if it has any folders that exist with the filename substring, if not it creates a new folder.
function CreateNewImageFolder() {
const optionalArgs={supportsAllDrives: true}; // enables support for shared drive
const timezone = Session.getScriptTimeZone(); // returns script timezone
const afId = 'archivefolderID' // Image Drive "Image_Archive" Folder ID
const aDest = DriveApp.getFolderById(afId); // returns 'Archive source' folder "Image_Archive"
const op20Id = 'fixedfolderW/allfilenames' // Image Drive "Folder that contains all possible filenames" Folder ID
const op20 = DriveApp.getFolderById(op20Id); // returns OP20 folder
const tId = 'tempfolderID' // Image Drive "temp" Folder ID
const temp = DriveApp.getFolderById(tId); // returns OP20 folder "temp"
var yday = Utilities.formatDate(GetYesterday(1), timezone, "MM-dd"); // returns yesterdays date for file name search
var yyear = Utilities.formatDate(GetYesterday(1), timezone, "yyyy-MM-dd"); // returns yesterdays date + year for folder name search
Logger.log("Yesterday: %s", yday);
//// Checks for existing Date Folder and creates a new one if not exist ///
var datefold = aDest.searchFolders("title contains '"+yyear+"'"); // search aDest for existing date folder
if (datefold.hasNext()===true) {
while (datefold.hasNext()) {
var dfolder = datefold.next();
Logger.log("Folder %s already exists", dfolder.getName());
}
} else{
var dfolder=aDest.createFolder(yyear); // if not exist create new folder in aDest
Logger.log("New Date Folder Name: %s", dfolder.getName());
}
var dfoldId = dfolder.getId();
var vDest = DriveApp.getFolderById(dfoldId);
Logger.log("New Folder Destination: %s", vDest.getName());
//// loops through ZIP file iterator ////
var zfi = op20.searchFiles("title contains '"+yday+"'"); // search op20 folder for Yesterdays Zip File 'file iterator'
while (zfi.hasNext()){ // loops through ZIP file iterator
var file = zfi.next(); // every loop sets active file to next
Logger.log("Zip Folder: %s", file.getName());
var fileBlob = file.getBlob(); // get file blob
fileBlob.setContentType("application/zip");
var unZippedfile = Utilities.unzip(fileBlob); // unzipped file iterator
//// loops all blob elements ////
for (i=0; i<unZippedfile.length; i++) {
var uzf = temp.createFile(unZippedfile[i]); // creates upzipped file in "temp" folder
var fileName = uzf.getName().substring(0, 17); // trims Image name string to first 17 characters
Logger.log("unzipped File Name: %s", fileName);
//// Checks for existing Folder and creates a new one if not exist ////
var vfold = vDest.searchFolders("title contains '"+fileName+"'"); // search vDest for existing folder
if (vfold.hasNext()===true) {
while (vfold.hasNext()) {
var vfolder = vfold.next();
Logger.log("Folder %s already exists", vfolder.getName());
}
} else{
var vfolder=vDest.createFolder(fileName); // if not exist create new folder in aDest
Logger.log("New Folder Name: %s", vfolder.getName());
}
uzf.setTrashed(true);
}
}
}
Simple Example Zipped Folder Located at:
Root > Image_Archive > Station > Camera > Date > 'zippedfolderName_2022_10_23'
zipped folder contains several image files all starting with unique 17 character #. ex: 1ABCXXXXXXXXXXXX1
Desired Output
Root > Image_Archive > New_Folder (named with yesterdays date) > New_Folder (named using 17 character # ex: 1ABCXXXXXXXXXXXX1)
However it is not resolved. As stated below, it does not appear to be possible. I will attempt a different approach by not using a zip file at all., I have to apologize for my poor English skill. In order to correctly understand your current issue, can you provide your sample input and output situations you expect as the images? By this, I would like to confirm it.