見出し画像

Google Apps Script(GAS)を使わずにGoogleドライブのフォルダ・ファイル構造の一覧を作る方法

試行錯誤した結果、私は調べたい対象のGoogleドライブのフォルダをPCにダウンロードして、dirコマンドでフォルダとファイルの情報を得ることにした。大量のGoogleドライブのフォルダを、Google Apps Script(GAS)のプログラムで調べようとした場合、その処理がGoogleの定めた制限時間の6分間で終わらないからである。

なお、後述する通り、同じ内容のインプットデータを同じプログラムで処理しても、成功する場合と時間切れになる場合があった。

この作業に取り組んだ経緯は下に記述する。なお、参考書や関連サイトを見たり、生成AIに聞きながら作った、Google Apps Script(GAS)のプログラムの詳細については別の記事に記録として残す。

**********************************

私が65歳になって、定年再雇用契約が終わった後、求職活動をしてきたことは、これまで何回か記事にした。

毎週、職業安定所に通ったものの、約半年間就職できなかった。しかし暑い夏が始まる直前、パートタイマーの職を見つけることができた。出勤は週1回で、家でできる仕事は持ち帰ってやっている。

職場までの通勤に40分、往復だと80分かかり、しかも今年の夏はとても暑かったので、家で仕事ができるのはとてもありがたかった。

就職までの経緯については、書いておきたいことがあるのだけれど、それは別の機会に回すとして、表題のことについて書いておきたい。

私が働いている職場では、Googleドライブに格納された相当数のフォルダやファイルを複数の人が共用していて、全体の情報が把握できず、困っていた。個人が作ったり編集した、似たような名前のフォルダやファイルがあり、どれが最新かわからない状態である。最初から構成とルールを考え、それに沿ってファイルを格納していけばいいのだが、Googleドライブを使い始める前に、それを決めている企業は多くないと思う。

いずれにせよ、Googleドライブの今ある状態(As is)を把握し、あるべき構成(To be)にしたいという要望に応えるため、現状調査を行うことにした。

Googleは、Googleの様々なサービスについて、利用者が操作できるプログラム環境を用意している。そのプログラムは、Google Apps Script(GAS)と呼ばれており、習熟している人には書けるだろう。
しかし参考書を読んでも、素人の私はまったくわからなかった。

そこで、「Googleドライブのフォルダ・ファイル一覧を出力する方法」をインターネットで検索した。多くの関連記事がみつかったものの、目当ての結果を得られた方法が載っていたのは、この記事だけであった。

この記事の著者にはとても感謝している。

ただし、注意するべきことがある。
この記事に書かれているGASは、Googleドライブの「マイドライブ」の中の全部のフォルダとファイルの情報を出力してくれるが、実は、GASのプログラムは何であれ、動かせる制限時間は6分と決まっている。

つまり、6分以内に処理が終わらなければ、時間切れで終わってしまう!

時間切れログ表示

しかも、私がテストしたところ、フォルダとファイルの数が同じでも、実行する時間帯によって、正常終了したり、上のようなエラーになったりした(具体的には約1200個のファイルを格納したフォルダを指定してテストした)。おそらく、Google側のサーバの稼働状況が混んでいたら、処理が遅くなるのだろう。

実行ボタンを押した後、6分間待ってエラー表示だと、非常にがっかりする。

そこで私は、GASを使うのをやめた。GoogleドライブのフォルダをPCにダウンロードして、dirコマンドを使うことにした。

Googleドライブのフォルダを右クリックして、PCにダウンロードする画面を呼び出す

dirコマンドの使い方については、検索すれば簡単に見つかる。ダウンロードしたフォルダにdirコマンドを実行すると、このように表示される。

C:\Users\a-e>dir C:\Users\e\Downloads\test1-20231006*093531*-001\test1
ドライブ C のボリューム ラベルは Windows です
ボリューム シリアル番号は ********* です
C:\Users\a-e\Downloads\test1-20231006*093531*-001\test1 のディレクトリ
2023/10/06 19:00 <DIR> .
2023/10/06 18:59 <DIR> ..
2023/10/06 19:00 <DIR> emp1
2023/10/06 18:59 <DIR> EBackup
2023/10/06 18:59 <DIR> sdc1
2023/10/06 18:59 4,702 test1111.xlsx
1 個のファイル 4,702 バイト
5 個のディレクトリ    ******* バイトの空き領域

dirの実行例

ファイルが多いとダウンロードの時間もかかるし、PCのディスクの空き容量も必要だが、dirを実行したらPCから消せばすむ。それになによりdirコマンドは秒単位で完了する。結果を直接テキストファイルに書きだすこともできる。
表示はそっけないが、フォルダやファイル名を知るには十分である。下位のフォルダを調べたければ、この作業を繰り返すだけなので誰でもできる。

なお、フォルダやファイルの作成者や作成時期を知るためには、Googleドライブの画面を見るか、制限時間のあるGoogle Apps Script(GAS)を使うかしかない。しかし、調べたいフォルダやファイルの場所がわかるだけでも作業時間は短くできるだろう。

私にとってはコロンブスの卵であった。

「Googleドライブのフォルダ・ファイル一覧を出力する方法」を検索したら、ダウンロードしてdirを使う方法は出てこない。6分の制限時間を回避する方法を書いた記事もあるが、それを元のプログラムに適用するためには、知識と経験と時間が必要である。それに先に書いた通り、そもそも1個を除いて他のプログラムは使い物にならず、私を疲弊させた。

古くて懐かしいdosコマンドは今でも役に立つ。

タイトル写真は、「パソコンでもスマホでも使える! 「Googleドライブ」の使い方の基本」サライ、2021/7/26より引用

How to create a list of folders and file structures in Google Drive without using Google Apps Script (GAS)

After much trial and error, I decided to download the Google Drive folder that I wanted to check to my PC and use the dir command to get folder and file information. This is because when trying to examine a large number of Google Drive folders using a Google Apps Script (GAS) program, the process cannot be completed within the six-minute time limit set by Google.

As will be described later, even when input data with the same content is processed by the same program, there are cases where it succeeds and cases where the time runs out.

The background to this work is described below. In addition, I will record the details of the Google Apps Script (GAS) program that I created while looking at reference books and related sites, and listening to the generation AI in a separate article.

********************************************************************************
I have written several times about how I started looking for a job after I turned 65 and my reemployment contract ended.

Although I went to the employment office every week, I couldn't find a job for about half a year, but just before the start of the hot summer, I was able to find a part-time job. I only go to work once a week and take home the work I can do at home.

It took me 40 minutes to commute to work and 80 minutes round trip, and it was very hot this summer, so I was very grateful to be able to work from home.

There is something I would like to write about how I got the job, but I would like to leave that for another time and write about the title.
At the workplace where I work, a considerable number of folders and files stored in Google Drive are shared by multiple people, and I was having trouble keeping track of all the information.

At that workplace, multiple people shared a large number of folders and files stored on Google Drive, and it was difficult to keep track of all the information. There are folders and files with similar names that were created or edited by individuals, and it is difficult to know which one is the latest. All you have to do is think about the structure and rules from the beginning and store files accordingly, but I don't think many companies decide on this before they start using Google Drive.

In any case, we decided to investigate the current situation in order to understand the current state of Google Drive (as is) and respond to requests for the desired configuration (to be).

Google provides a program environment that allows users to operate various Google services. The program is called Google Apps Script (GAS), and anyone who is familiar with it should be able to write it. However, even after reading the reference books, I had no idea.
So, I searched the Internet for "How to output a list of Google Drive folders and files." Although many related articles were found, this article was the only one that described a method that achieved the desired results.

I am very grateful to the author of this article.

However, there are some things you should be careful about. The GAS described in this article outputs information about all folders and files in Google Drive's "My Drive", but in reality, no matter what GAS program it is, the operating time is only 6 minutes. .

In other words, if the processing is not completed within 6 minutes, the time will run out and the program will end prematurely!

What's more, when I tested it, even if the number of folders and files were the same, depending on the time of day it was executed, it sometimes ended normally or gave an error like the one above (I tested it with a folder containing about 1200 files). ). Probably, if Google's servers are busy, the processing will be slow.

It's very disappointing to wait 6 minutes after pressing the run button to see an error message.

So I stopped using GAS. I decided to download the Google Drive folder to my PC and use the dir command.

Instructions on how to use the dir command can be easily found by searching the Internet. When you run the dir command, I can get necessary information.

If there are a lot of files, it will take time to download, and you will also need some free space on your PC's disk, but you can delete them from your PC after running dir. Best of all, the dir command completes in seconds. You can also export the results directly to a text file. Although the displayed content is simple, it is enough to know the folder and file names. Anyone who wants to check a lower-level folder can do this by simply repeating this process.

The only way to find out who created a folder or file and when it was created is to look at the Google Drive screen or use Google Apps Script (GAS), which has a time limit. However, just knowing the location of the folder or file you want to check can save you a lot of time.

For me, it was Columbus's egg.

When I searched for "How to output a list of Google Drive folders and files," I couldn't find a method to download and use dir. There are articles written about how to get around the 6-minute time limit, but applying them to the original program requires knowledge, experience, and time. Besides, as I wrote earlier, all but one of the other programs were useless and tired me out.

The good ol` dos command is still useful.

Title photo is quoted from "Basics of how to use Google Drive" Sarai, July 26, 2021

この記事が気に入ったらサポートをしてみませんか?