1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google Apps ScriptでFolder内のファイルを再起的に扱える関数を作成した

Last updated at Posted at 2022-10-13

元の関数

recursive.gs
// rootFolder: Folderクラス
// type: both, onlyFolder, onlyFile
// func: function(object: Folder or File, type: "Folder" or "File")
function recursive(rootFolder, type, func) {
  const files = rootFolder.getFiles()
  const folders = rootFolder.getFolders()

  if(type=="both" || type=="onlyFile"){
    while(files.hasNext()){
      func(files.next(), "File")
    }
  }

  while(folders.hasNext()){
    const folder = folders.next()
    if(type=="both" || type=="onlyFolder") { func(folder, "Folder") }
    recursive(folder, type, func)
  }

}

使い方

listAllFileAndFolder
function listAllFileAndFolderInFolder(folder){
  recursive(folder, "both", function(folderOrFile, type){
      console.log(type) // Folder or File
      console.log(folderOrFile.getName())
      console.log(folderOrFile.getUrl())
  })
}

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?