Windowsネットワークで共有しているファイルが誰かに更新されたらGrowlで通知するスクリプト

ネットワーク上で不特定多数が共有するファイルがいつ更新されているか確認したい場合って多々ある。
そこで、ファイルの更新日をチェックして前回の更新日から変更があれば Growl で通知するスクリプトかいてみた。
実行すると、こんな感じでファイルの更新を通知してくれる。

http://gyazo.com/b50dbd9dc063d15a33e0b723ecd167e5.png


監視対象のパスの書き方をそれなりにすれば、別にWindowsに限定されないはず...

ruby_gntpが必要です。

gem install ruby_gntp

でインストールしといてください。

スクリプト

require 'kconv'
require 'rubygems'
require 'ruby_gntp'

targets = [
  "//000.000.000.000/shared/doc/Excelファイル.xls",
  "d:/Projects/hoge/test.txt"
]

entry = {}

while true
  targets.each do |target|

    current = File.mtime(target)
    puts "#{Time.now} check #{target} ctime#{current}"
    unless entry[target]
      entry[target] = current
    else
      if entry[target] < current
        puts "#{target} has updated!"
        GNTP.notify (
          :app_name => "File observer",
          :title    => "File has updated!!",
          :text     => target.toutf8
        )
      end
      entry[target] = current
    end
  end 

  sleep 5 * 60 # 5min.
end

カスタマイズ方法

監視対象ファイルの設定

target配列に監視対象のファイルのパスを定義します。具体的には以下のような感じです。
例1 C:\hoge\fuga.txt

"c:/hoge/fuga.txt"

例2 \\fileserver\hoge\fuga.txt (コンピュータ "fileserver" の共有フォルダ"hoge"の中の"hoge.txt")

"//fileserver/hoge/fuga.txt"

インターバル(間隔)

監視する間隔は sleep のパラメータを調整します。

  sleep 5 * 60 # 5min.