Advertisement
fmradio

ファイル名の末尾に「ファイル更新日時」、または「現在日時」を付与する。その後「コピー」、または「リネーム」するVBScript

Nov 13th, 2020 (edited)
2,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' ファイル名の末尾に「ファイル更新日時」、または「現在日時」を付与する。その後「コピー」、または「リネーム」するVBScript
  2. '
  3. ' 複数ファイル選択可能。
  4. ' VBScriptのショートカットをSendToに入れておけば右クリック「送る」で実行可能。
  5. '
  6. ' スクリプトはShift-JISで記述し、改行コードは CRLFとする。
  7. ' 参考
  8. ' http://gren-dken.hatenablog.com/entry/2013/08/22/000119
  9. ' 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/
  10. '
  11.  
  12. Option Explicit
  13.  
  14. Dim Msg
  15. Dim Switch1
  16. Dim Switch2
  17.  
  18. Const TITLE1 = "選択ファイルをファイル更新日時、現在日時でコピー、リネームする"
  19.  
  20.  
  21. Msg = MsgBox("コピーする?(Y)、リネームする?(N)", vbQuestion + vbYesNoCancel, TITLE1)
  22.  
  23. If Msg = vbYes Then
  24.        Switch1 = "Y"
  25. ElseIf Msg = vbNo Then
  26.        Switch1 = "N"
  27. Else
  28.        ' MsgBox "キャンセルを押しました。" ,vbExclamation, TITLE1
  29.       WScript.Quit
  30. End If
  31.  
  32.  
  33. Msg = MsgBox("更新日時で?(Y)、現在日時で?(N)", vbQuestion + vbYesNoCancel, TITLE1)
  34.  
  35. If Msg = vbYes Then
  36.        Switch2 = "Y"
  37. ElseIf Msg = vbNo Then
  38.        Switch2 = "N"
  39. Else
  40.        ' MsgBox "キャンセルを押しました。" ,vbExclamation, TITLE1
  41.       WScript.Quit
  42. End If
  43.  
  44.  
  45. ' 引数確認
  46. If WScript.Arguments.Count < 1 Then
  47.     WScript.Quit
  48. End If
  49.  
  50. Dim fso
  51. Set fso = CreateObject("Scripting.FileSystemObject")
  52.  
  53.  
  54. Dim targetPath, changePath
  55.  
  56. For Each targetPath In WScript.Arguments
  57.     If fso.FileExists(targetPath) Then
  58.         changePath = createNewFilePath(targetPath)
  59.  
  60.         If Switch1 = "Y" Then
  61.            Call fso.CopyFile(targetPath, changePath)
  62.         Else
  63.            Call fso.MoveFile(targetPath, changePath)
  64.         End If
  65.  
  66.     End If
  67. Next
  68.  
  69. ' 更新日時、または、現在日時を付与したパスを生成
  70. Function createNewFilePath(targetPath)
  71.     Dim fo
  72.     Set fo = fso.GetFile(targetPath)
  73.  
  74.     ' Dim lastModified, strFormattedDate, strDate
  75.    Dim lastModified
  76.  
  77.     If Switch2 = "Y" Then  ' ファイル更新日時取得
  78.       lastModified = fo.DateLastModified
  79.     Else                   ' 現在日時取得
  80.       lastModified = Now
  81.     End If
  82.  
  83.     ' 不要文字削除
  84.    lastModified = FormatDateTime(lastModified, vbShortDate) & " " & Right("0" & FormatDateTime(lastModified, vbLongTime),8)
  85.     lastModified = Replace(lastModified, "/", "")
  86.     lastModified = Replace(lastModified, ":", "")
  87.     lastModified = Replace(lastModified, " ", "_")
  88.  
  89.     'strDate = FormatDateTime(lastModified, vbShortDate) & " " & Right("0" & FormatDateTime(lastModified, vbLongTime),8)
  90.    'strFormattedDate = Replace(Replace(Replace(strDate, "/", ""), ":", ""), " ", "_")
  91.  
  92.     ' ファイルパス分割
  93.    Dim targetDir
  94.     Dim targetBaseName
  95.     Dim targetExt
  96.     targetDir = fso.GetParentFolderName(targetPath)
  97.     targetBaseName = fso.GetBaseName(targetPath)
  98.     targetExt = fso.GetExtensionName(targetPath)
  99.  
  100.     ' 変更後ファイルパス生成し、返却
  101.    createNewFilePath = targetDir & "\" & targetBaseName & "_" & lastModified & "." & targetExt
  102. End Function
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement