オオブログ!

答えのないことを考えるのが好きな大学院生のブログ。

RとWindowsのタスクスケジューラを使って定期的な作業を自動化する。How to automate the task you regularly do using R and Task scheduler on Windows.

*I translated important Japanese sentences to English ones below. So, you can ignore Japanese ones.

こんにちは。

いつもオオブログ!を読んでいただきありがとうございます。

いつもは、自分が社会に対して思ったことなどを書いていて、

主に自分の友達が共感してくれたり、直接会ったときに議論してくれたりLINEくれたりと、

そこそこ反響があって面白いので、そっちもやってはいこうと思うのですが、

せっかくこうして全世界に発信しているわけなので、たまには知らない誰かのためにも役立つ情報を書いていけたらなと思います。

バイリンガル発信にも挑戦。

 

 

そこで、今回は、Windowsタスクスケジューラという機能と、統計解析系のプログラミング言語であるRを用いて、「定期的に行っている作業の自動化」をやっていきたいと思います。

WindowsでできることはだいたいMacでもできると思うので、たぶんタスクスケジューラのような機能がMacにもあるはずです。ここでは言及しませんが。

I wanna show you one of the ways to automate processes you regularly do, using R and “Task Scheduler” on Windows.

自動化といえば、たぶんPythonのほうが向いているのかもしれませんが、自分が研究などでRを使うことが今のところ多いので、Rをつかってやっていきたいなと思います。

Rのダウンロードや、便利なRstudioなどの使い方は、他のサイトに書き尽くされていると思うので、省略します。

僕の用いているOSはWindows10で、RはR3.6.3を使ってます。

I use Windows10 and R3.6.3.

この自動化させるやり方は、「自動化させたい作業のスクリプトをRで書く」と「バッチファイル(説明は後)を作成する」、「スクリプトが書かれたファイルをタスクスケジューラに登録し、定期的に起動させる」という3つの部分に分かれます。

The method can be divided into 3 parts. One is to write the codes for the process you wanna automate. The next part is to create a batch file. Finally, you can register the file you wrote the code to Task Scheduler and make it work regularly.

僕が自動化させたかった作業は、ファイルのバックアップです。 自分のパソコン内のファイルが消えてしまったら嫌なので、重要なファイルは毎日、研究室のバックアップ用の共有ネットワークファルダに保存するようにしました。

What I wanted to do is that to automate the process to backup important files in my PC. And I wanted to save them in an shared folder for our laboratory as well once a day.

  1. 自動化させたい作業のスクリプトをRで書く-write the codes for the process you wanna automate

まず、僕はファイルのバックアップすなわち自分のファイルを他の場所にコピーするというスクリプトを書きますが、ここはあなたが何を自動化させたいかによるので、適宜変えてしまって下さい。

First, here I’m writing the code to copy files in a folder and paste them to another folder for backup. But if you wanna do other things you can change the codes here.

僕は以下のスクリプトをググりつつ作りました。R studioで、左上のFile -> New file -> R Scriptで作るのが一番手っ取り早いのではないでしょうか。

その辺は自己流でやっちゃったので、もっといい方法があったら教えてください。。

I made the script below on R studio starting with “File (topleft on the screen) -> New file -> R Script”.

ほんで、コンピュータの中にパスっていう概念があるんですけど、

“C:/Users/owner/Desktop/unchi”みたいなやつで、これは、Userの中の、ownerの中の、デスクトップの中の、unchiというフォルダーを、指してます。

「こいつへのパス(道)はこういう感じでたどり着けますよ」っていうものです。

Windowsだったらエクスプローラーで、特定のファイルとかを選択してアドレスバーをクリックしたら、そのファイルまでのパスが表示されるはずです。

パスはいろいろ奥が深いので、気になった人は、ググってみてください。

The following scripts inclides the concept of “path” on Computer Technology. If you wanna know about it more, search “path” on google.

#デスクトップ下の"unchi"というフォルダーへのパスを"oripath"というデータに格納(パスの内容は人によりけり)。
#set the path to the folder you have files you wanna copy. The path is up to yours.
oripath <- "C:/Users/owner/Desktop/unchi"

#バックアップしたいフォルダへのパスを"newpath"に格納。
#set the path to the folder you wanna paste the files.
newpath <- "//xxx.xxx.xx/shared/unchi"

#oripathで指定されたフォルダ内の~.docxファイルだけを"datafiles"にリストアップ。
#List "docx" files in the folder oripath describes and make the list in "datafiles".
datafiles <- dir(oripath, "*.docx", ignore.case = TRUE, all.files = TRUE)

#oripath内のdatafilesをnewpathにコピー&ペースト。上書きされるので、同じ名前のファイルあるときは注意。
#Copy the "datefiles" in "oripath" and paste them to "newpath". Note that files which have same names will be overwrritten.
file.copy(file.path(oripath, datafiles), newpath, overwrite = TRUE)

このスクリプトのRファイルを、ローカルのCドライブの直下に保存します。 僕は、“file_backup.R”という名前で保存しました。

Save this file just under local “C drive”. And name it “file_backup.R”

 

  1. バッチファイルを作成する-Creat a batch file

次に、バッチファイルというものを作成します。バッチファイルとは、Windowsのシェル(詳しくは難しすぎるので割愛。まあ、パソコンの中身)に何かを実行させるためのテキストファイルで、 メモ帳アプリで作成して、保存するときの拡張子を.txtから.batにかえて保存するだけで作成できます。

Next, you’re gonna create a batch file. You can make it on Notepad and change .txt to .bat when you save it.

メモ帳に、下の2行(C:のあとは改行)のように書いてください。Text on Notepad like below.

cd C:\
“C:\Program Files\R\R-3.6.3\bin\Rscript.exe” file_backup.R

1行目のcd は、change directoryっていって、場所をこのパスのところにしてください的なコマンドです。Cドライブの直下(したい作業を書いたRファイルがある場所)を作業をする場所(ディレクトリ)に指定します。

cd in the 1st line means change directory. set there as the working directory.

2行目は、まず、Rを立ち上げる時に使われるファイル(.exe)の場所を指定しています。Rscript.exeはRをインストールした時に勝手にProgram Files > R > R-3.x.x > binの中にあるはずです。 で、そのRを立ち上げて“file_backup.R”をやれ!って言ってます。

In the second line, it points the “Rscript.exe” which is used for launching R, and then orders like “run file_backup.R using R!” hahaha.

このバッチファイルは任意の場所に保存してください。

This batch file can be saved wherever you like. But not too bad place.

 

  1. 作ったスクリプトをタスクスケジューラに登録-register the file you wrote the code to Task Scheduler and make it work regularly

タスクスケジューラと検索すれば、やり方は出てきます。

作ったバッチファイルを実行ファイルとし、時刻や頻度などを設定すれば、あなたも自動化マンの仲間入り!

I got too tired to explain more. So search “task scheduler” on Google. Then you can make it. To be short, register the batch file as the one should be conducted once a day or smth, and set time and frequency. FInally, you will be a person who can manupulate automation.

ちなみに、バックアップですが、Dropboxからはいけました。GoogleドライブやOneDriveはまだ試していません。誰か試してみてください。

By the way, I could backup files from Dropbox folders but I haven’t tried Google Drive and OneDrive yet. So please give me your feedback after trying them.

そして、たまたまかもしれませんが、ファイル名に日本語があるとうまくいかなかったです。パスの途中も、最初は漢字の名前のフォルダーがあったのですが、うまく認識されなかったので、全部英語の名前に変えました。

以上です。ではまた!

That’s about everything for today. Thank you for reading!