Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ファイル名の末尾に「ファイル更新日時」、または「現在日時」を付与する。その後「コピー」、または「リネーム」するVBScript
- '
- ' 複数ファイル選択可能。
- ' VBScriptのショートカットをSendToに入れておけば右クリック「送る」で実行可能。
- '
- ' スクリプトはShift-JISで記述し、改行コードは CRLFとする。
- ' 参考
- ' http://gren-dken.hatenablog.com/entry/2013/08/22/000119
- ' https://itbyari.wordpress.com/2015/12/06/%E5%8F%B3%E3%82%AF%E3%83%AA%E3%83%83%E3%82%AF%E3%81%8B%E3%82%89%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AB%E6%97%A5%E6%99%82%E3%82%92%E4%BB%98%E4%B8%8E%E3%81%97%E3%81%A6%E3%82%B3%E3%83%94%E3%83%BC/
- '
- Option Explicit
- Dim Msg
- Dim Switch1
- Dim Switch2
- Const TITLE1 = "選択ファイルをファイル更新日時、現在日時でコピー、リネームする"
- Msg = MsgBox("コピーする?(Y)、リネームする?(N)", vbQuestion + vbYesNoCancel, TITLE1)
- If Msg = vbYes Then
- Switch1 = "Y"
- ElseIf Msg = vbNo Then
- Switch1 = "N"
- Else
- ' MsgBox "キャンセルを押しました。" ,vbExclamation, TITLE1
- WScript.Quit
- End If
- Msg = MsgBox("更新日時で?(Y)、現在日時で?(N)", vbQuestion + vbYesNoCancel, TITLE1)
- If Msg = vbYes Then
- Switch2 = "Y"
- ElseIf Msg = vbNo Then
- Switch2 = "N"
- Else
- ' MsgBox "キャンセルを押しました。" ,vbExclamation, TITLE1
- WScript.Quit
- End If
- ' 引数確認
- If WScript.Arguments.Count < 1 Then
- WScript.Quit
- End If
- Dim fso
- Set fso = CreateObject("Scripting.FileSystemObject")
- Dim targetPath, changePath
- For Each targetPath In WScript.Arguments
- If fso.FileExists(targetPath) Then
- changePath = createNewFilePath(targetPath)
- If Switch1 = "Y" Then
- Call fso.CopyFile(targetPath, changePath)
- Else
- Call fso.MoveFile(targetPath, changePath)
- End If
- End If
- Next
- ' 更新日時、または、現在日時を付与したパスを生成
- Function createNewFilePath(targetPath)
- Dim fo
- Set fo = fso.GetFile(targetPath)
- ' Dim lastModified, strFormattedDate, strDate
- Dim lastModified
- If Switch2 = "Y" Then ' ファイル更新日時取得
- lastModified = fo.DateLastModified
- Else ' 現在日時取得
- lastModified = Now
- End If
- ' 不要文字削除
- lastModified = FormatDateTime(lastModified, vbShortDate) & " " & Right("0" & FormatDateTime(lastModified, vbLongTime),8)
- lastModified = Replace(lastModified, "/", "")
- lastModified = Replace(lastModified, ":", "")
- lastModified = Replace(lastModified, " ", "_")
- 'strDate = FormatDateTime(lastModified, vbShortDate) & " " & Right("0" & FormatDateTime(lastModified, vbLongTime),8)
- 'strFormattedDate = Replace(Replace(Replace(strDate, "/", ""), ":", ""), " ", "_")
- ' ファイルパス分割
- Dim targetDir
- Dim targetBaseName
- Dim targetExt
- targetDir = fso.GetParentFolderName(targetPath)
- targetBaseName = fso.GetBaseName(targetPath)
- targetExt = fso.GetExtensionName(targetPath)
- ' 変更後ファイルパス生成し、返却
- createNewFilePath = targetDir & "\" & targetBaseName & "_" & lastModified & "." & targetExt
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement