LoginSignup
1
0

More than 1 year has passed since last update.

GAS フォルダの中にあるファイル更新日が特定日以降のZipファイルを解凍する

Last updated at Posted at 2021-05-15

Google Apps Scriptでのフォルダ操作やファイル操作についてのTipsです

こんにちは。システムエンジニアのDiavoloです。
現在のお仕事では、様々な機器やサーバ等が出力するログファイルを処理して集計する機会が多いです。
Gooleドライブにログファイルが保存してある場合に作成したGoogleAppsScriptを備忘録として記事にします。

例として2021年04月01日以降に更新されたファイルを解凍するGASです

function myFunction() {
  //フォルダの中にあるファイル一覧を取得する
  const files = DriveApp.getFolderById('フォルダid').getFiles();

  while(files.hasNext()){
    const file     = files.next();
    const filename = file.getName();
    const filetimestamp = file.getLastUpdated();

    if(fileDateCompare(filetimestamp,new Date('2021/04/01')) == 1 ){
    {
       console.log(filename);

       //ファイル名に.zipを含むファイルだけ解凍する
       if(filename.match(/.zip/)){
         var zipblob      = file.getBlob().setContentType("application/zip");
         var unZippedfile = Utilities.unzip(zipblob);
         var folder       = DriveApp.getFolderById(targetId);
         folder.createFile(unZippedfile[0]);
      }
    }
  }
}
function fileDateCompare(date1,date2){
  if(date1 >= date2){
    return 1;
  }
  else{
    return 0;
  }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0