Advertisement
fmradio

◎選択されたファイルパスを"ソートした後、"クリップボードにコピーするVBScript

Nov 16th, 2020 (edited)
1,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' ◎ 引数(選択されたファイルパス・フォルダパス)を、引数毎に改行して、"ソートした後、"クリップボードに送るVBScript
  2. ' path2clipboardS.vbs, path2Sclipboard.vbs, path2clipboard.vbs
  3. ' 複数ファイル選択に対応済み
  4. ' VBScriptのショートカットをSendToに入れておけば右クリック「送る」で実行可能
  5. ' スクリプトはShift-JISで記述し、改行コードは CRLFとする
  6. ' クリップボード使用時に、一瞬、コマンドプロンプトが表示されるけど許してね
  7. ' オリジナル https://ch.nicovideo.jp/imihito/blomaga/ar1109630
  8. ' 参考 http://blog.livedoor.jp/blackcode/archives/vbscript-how-to-use-arraylist.html?ref=popular_article&id=2777774-255458%27%20ArrayList%E3%81%AE%E4%BD%9C%E6%88%90
  9. ' 参考 https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.arraylist?redirectedfrom=MSDN&view=net-5.0
  10.  
  11. Option Explicit
  12. Const MSG_TITLE = "クリップボードにコピー"
  13. Const CLIP = "%windir%\System32\clip.exe"
  14.  
  15. ' 異常処理:引数なし
  16. Dim argsCnt ' As Integer
  17. argsCnt = Wscript.Arguments.Count - 1
  18. If argsCnt = -1 Then
  19.    Call MsgBox("ファイルをドロップしてください", vbExclamation, MSG_TITLE)
  20.    Call Wscript.Quit
  21. End If  
  22.  
  23.  
  24. ' Join用に引数を配列化
  25. ' Dim argsArray() 'As String
  26. ' ReDim argsArray(argsCnt)
  27.  
  28. ' ArrayListの作成
  29. Dim argsArrayList
  30. Set argsArrayList = CreateObject("System.Collections.ArrayList")
  31.  
  32. Dim i     ' As Integer
  33. With Wscript.Arguments
  34.      For i = 0 To argsCnt
  35.            ' argsArray(i) = .Item(i)
  36.           ' 要素を追加していく
  37.           argsArrayList.Add .Item(i)
  38.      Next ' i
  39. End With
  40.  
  41.  
  42. ' 要素をソート
  43. argsArrayList.Sort()
  44.  
  45.  
  46. ' '確認用(機能としては不要)
  47. ' For Each文で要素を表示
  48. ' Dim element
  49. ' For Each element In argsArrayList
  50.    ' Wscript.Echo element
  51. ' Next
  52. ' MsgBox "ソート前に一時停止"    'ここまで確認用
  53.  
  54.  
  55. ' 通常の配列に変換
  56. Dim argsArray : argsArray = argsArrayList.ToArray()
  57.  
  58.  
  59. ' クリップボードに送る(コピーする)文字列作成
  60. Dim cpText ' As String
  61. Dim myDel  ' As String
  62. myDel = vbCrLf
  63. cpText = Join(argsArray, myDel)
  64.  
  65. With CreateObject("Wscript.Shell")
  66.      With .Exec(CLIP)
  67.            Call .StdIn.Write(cpText)  'vbCrLf を使わず、WriteLineではなぜか改行が効かない
  68.     End With
  69.      i = 3 ' 再利用
  70.     ' Call .Popup("コピーが完了しました" & vbCrLf & "このウィンドウは" & i & "秒後に閉じます", i, MSG_TITLE, vbInformation)
  71. End With
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement