wolfmasa's blog

フロンターレとプログラミング関係の話題を、気が向いたときにつぶやくブログです。

GoogleDriveのいらないファイルを削除した話(続編)

これでうまくいったと思ったら、勘違いがあったので細くエントリー

実際には、取得したGoogleDrive::Collection#contentsを見ると、100個しかとれてない。

まさかと思って、ソースを見てみると、

        # By default, it returns the first 100 files. See document of GoogleDrive::Session#files method
        # for how to get all files.

とのことなので、ファイルを全て取得する実装に変更

具体的には、collection#contents#each で回すのではなくcollection#contents &block で各要素にアクセスする。

セッションも引数でとるようにして、

require 'google/api_client'
require 'google_drive'

# Creates a session.
target = ARGV.shift
access_token = ARGV.shift
session = GoogleDrive.login_with_oauth(access_token)

def searchAndDeleteCR2(target, pathArray)
  counts = 0
  target.contents do |item|
    counts += 1
    if item.class == GoogleDrive::Collection
      searchAndDeleteCR2(item, [pathArray, item.title]) 
    elsif /cr2$/i === item.title
      puts "Delete: " + [pathArray, item.title].join('/')
      item.delete true
    end
  end
  puts "Target: #{pathArray.join('/')} (Count #{counts})"
end

root = session.collection_by_title(target)

p root.title
searchAndDeleteCR2(root, [])

こんどこそ大丈夫かな。