'#
'# ファイル名から更新日時を変更
'# 2013.01.26 @fashi2
'#
'# ファイル群をD&Dするだけ。フォルダ不可。
'# 同じ日時だった場合にずらす処理入れたけど微妙
'#
'# 2014.03.18 8~31時表記にも対応
'#

Option Explicit
Dim objFso, objRegExp, objShell, objDict
Dim colArg, objFile, objFolder, objFolderItem
Dim targetfile, filename, pfilename, dtlastmod, rootpathcheck

Dim secincrement, targetpath, targetdate, year, month, day, hour, min, sec, targetdt, TxOut

'# test
secincrement = 1

Set objFso		= CreateObject("Scripting.FileSystemObject")
Set colArg		= WScript.Arguments
Set objShell	= CreateObject("Shell.Application")
Set objDict		= WScript.CreateObject("Scripting.Dictionary")

If colArg.Count <> 0 Then

	TxOut = ""

	'# 引数処理
	For Each targetfile in colArg

		targetdt = ""
		pfilename = ""

		If (objFso.FileExists(targetfile) = True) Then

			'# 入力ファイル情報取得
			set objFile = objFso.GetFile(targetfile)
			targetpath  = objFile.ParentFolder & "\"
			rootpathcheck = objFile.Drive & "\" & "\"
			If (targetpath = rootpathcheck) Then
				targetpath  = objFile.ParentFolder
			End If
			filename  = objFile.Name
			dtlastmod = objFile.DateLastModified

			'# 日付抽出正規表現
			Set objRegExp = New RegExp
			objRegExp.IgnoreCase = True
			objRegExp.Pattern = "([12][0-9]{3})[/_-]?([01][0-9])[/_-]?([0123][0-9])[/_: -]?([0123][0-9])[_:-]?([0-5][0-9])[_:-]?([0-5][0-9]).*\.(jpeg|jpg|gif|bmp)$"
			set targetdate = objRegExp.Execute(filename)
			if (targetdate.Count > 0) Then
				year = targetdate.Item(0).SubMatches.Item(0)
				month = targetdate.Item(0).SubMatches.Item(1)
				day = targetdate.Item(0).SubMatches.Item(2)
				hour = targetdate.Item(0).SubMatches.Item(3)
                min = targetdate.Item(0).SubMatches.Item(4)
				sec = targetdate.Item(0).SubMatches.Item(5)
				targetdt = year & "/" & month & "/" & day & " " & hour & ":" & min & ":" & sec
				If (hour > 23) Then
					targetdt = year & "/" & month & "/" & day & " " & hour-8 & ":" & min & ":" & sec
					targetdt = DateAdd("h",8,targetdt)
				End If
			Else
				Set objRegExp = New RegExp
				objRegExp.IgnoreCase = True
				objRegExp.Pattern = "([12][0-9]{3})[/_-]?([01][0-9])[/_-]?([0123][0-9])[/_: -]?([0123][0-9])[_:-]?([0-5][0-9]).*\.(jpeg|jpg|gif|bmp)$"
				set targetdate = objRegExp.Execute(filename)
				if (targetdate.Count > 0) Then
					year = targetdate.Item(0).SubMatches.Item(0)
					month = targetdate.Item(0).SubMatches.Item(1)
					day = targetdate.Item(0).SubMatches.Item(2)
					hour = targetdate.Item(0).SubMatches.Item(3)
					min = targetdate.Item(0).SubMatches.Item(4)
					sec = "00"
					If (secincrement = 1) Then
						sec = "30"
						targetdt = year & "/" & month & "/" & day & " " & hour & ":" & min
						If (objDict.Exists(targetdt)) Then
							sec = objDict(targetdt)
							objDict.Remove targetdt
							If (StrComp(filename, pfilename) = -1) Then
								sec = sec-10
							Else 
								sec = sec+10
							End If
						End If
						objDict.Add targetdt,sec
						If (sec > 59) Then
							sec = "59"
						End If
					End If
					targetdt = year & "/" & month & "/" & day & " " & hour & ":" & min & ":" & sec
					If (hour > 23) Then
						targetdt = year & "/" & month & "/" & day & " " & hour-8 & ":" & min & ":" & sec
						targetdt = DateAdd("h",8,targetdt)
					End If
				End If
			End If

			If (targetdt <> "") Then

				Set objFolder		= objShell.NameSpace(targetpath)
				Set objFolderItem	= objFolder.Items.Item(filename)
				objFolderItem.ModifyDate	=	targetdt
				Set ObjFolderItem	= Nothing
				Set ObjFolder		= Nothing

				TxOut = TxOut & VbCrLf & filename & VbCrLf & dtlastmod & " → " & targetdt & VbCrLf
				pfilename = filename

			End If
		End If
	Next
	MsgBox(TxOut)
Else
	MsgBox("エクスプローラ等で直接jpegファイルをドロップして下さい")
End If
