#
# main処理
#
# 外部プログラム名はフルパスで指定する
$strCmd = "C:\mongodb\util\jhead.exe"
# デジカメ画像ファイル格納パス
$pathname = "C:\mongodb\imgs"
# jpegファイル一覧
$files = Get-ChildItem $pathname -Recurse | Where-Object {$_.Extension -eq ".jpg"}
# DB Driverパス指定
$driver_path = "C:\mongodb\driver\CSharpDriver-1.7.0.4714"
Add-Type -Path "$driver_path\MongoDB.Bson.dll";
Add-Type -Path "$driver_path\MongoDB.Driver.dll";
# mongoDB接続
$dbName = "exifdb"; # DB名
$connectionString = "mongodb://localhost/{0}?safe=true" -f $dbName;
$db = [MongoDB.Driver.MongoDatabase]::Create($connectionString);
$collectionName = "exifcollection"; # collection名
$collection = $db[$collectionName];
foreach( $file in $files ){
# 画像ファイル情報
$filefullname = $file.fullname
$dir_name = Split-Path $filefullname -Parent
$file_name = Split-Path $filefullname -Leaf
$file_LastWriteTime = $file.LastWriteTime
$file_size = $file.Length
# mongoDBに書き込む情報(画像ファイル情報)
$doc = New-Object MongoDB.Bson.BsonDocument;
$doc.Add("FileFname", [MongoDB.Bson.BsonValue]::Create($file_name))
$doc.Add("FileDir", [MongoDB.Bson.BsonValue]::Create($dir_name))
$doc.Add("FileDate", [MongoDB.Bson.BsonValue]::Create($file_LastWriteTime))
$doc.Add("FileSize", [MongoDB.Bson.BsonValue]::Create($file_size))
# jheadを実行して画像Exif情報を取得する
$outputstr = ExecCmd $StrCmd $filefullname
$outputstr -split "`\r`\n" | % {
if ( $_ -ne "" ){ # 空行対策
$line = $_ -split ": ",2
$key = $line[0].trim().trim("`.")# 前後スペース、ドット除去
$value = $line[1].trim() # 前後スペース除去
}
if(($key) -and ($value)){
# mongoDBに書き込む情報(画像Exif情報)
$doc.Add($key, [MongoDB.Bson.BsonValue]::Create($value))
$key = ""
$value = ""
}
}
$collection.save($doc);
}