0

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)

11
  • In order to correctly understand your situation, can you provide a sample zip file for testing your script? And, can I ask you about the file size of your zip file? Commented Oct 22, 2022 at 1:03
  • I'm afraid I cannot supply a sample of the zip file. I can tell you that each zip file is named with the current date at the end of the filename and the .jpg files within all have a similar name with unique 17 characters at the start of the filename. The file size is approximately 40 MB. Commented Oct 24, 2022 at 23:31
  • No problem. 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. Commented Oct 25, 2022 at 0:26
  • Thank you for replying. About 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. Commented Oct 25, 2022 at 0:28
  • I have edited the question with a section explaining the folder structure and desired result Commented Oct 25, 2022 at 1:16

1 Answer 1

1

Listing ZIP files from Google Drive

After reviewing all documentation regarding Drive and how the apps script function, you would always need to unzip it in order to be able to view the data and files from Drive.

Being able to manually view or list a zip file and gather the data is not possible. This could be considered a missing feature or just a limitation in general on how it works. You might request a better clarification over here:

Sadly it seems that Drive API is limited in a way that would require the unzip process as discussed directly in the community:

References

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the confirmation. This is right along with what I was able to find. Zipping and importing the files was done essentially just to improve the import speed. I will likely try importing the image files without zipping and working to organize them after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.