fork download
  1. Option Explicit
  2.  
  3. Const book_folder As String = "C:\Users\owner\Desktop\テスト用" '終端に\をつけないこと
  4. Const start_folder As String = "C:\Users\owner\Desktop\A\B" '終端に\をつけないこと
  5. Const Hyper_Link As Boolean = True 'True=ハイパーリンク表示 する False=通常表示
  6. Dim T_count As Long
  7. Dim T_folders() As String
  8. Dim T_endnames() As String
  9. Public Sub 一括移動処理()
  10. Dim ws As Worksheet
  11. Dim wrow As Long
  12. Dim maxrow As Long
  13. Dim i As Long
  14. Dim ecode As Long
  15. Dim fname As String
  16. Dim keyword As String
  17. Dim dest_path As String
  18. Dim exctr As Long: exctr = 0
  19. Set ws = Worksheets("移動結果")
  20. ws.Rows("2:" & Rows.Count).ClearContents
  21. 'ハイパーリンクを削除
  22. ws.Range("C2:C" & Rows.Count).ClearHyperlinks
  23. ws.Range("E2:E" & Rows.Count).ClearHyperlinks
  24. 'フォントの下線を削除
  25. ws.Range("C2:C" & Rows.Count).Font.Underline = False
  26. ws.Range("E2:E" & Rows.Count).Font.Underline = False
  27. '選択範囲のフォント文字色を標準色に変更
  28. ws.Range("C2:C" & Rows.Count).Font.ColorIndex = xlAutomatic
  29. ws.Range("E2:E" & Rows.Count).Font.ColorIndex = xlAutomatic
  30.  
  31. 'ファイル一覧を取得する
  32. Dim FSO As Object
  33. Set FSO = CreateObject("Scripting.FileSystemObject")
  34. Dim FSO_folder As Object
  35. Dim FSO_files As Object
  36. Dim FSO_file As Object
  37. 'ブックの格納フォルダ情報
  38. Set FSO_folder = FSO.getfolder(Book_folder)
  39. 'ブックの一覧取得
  40. Set FSO_files = FSO_folder.files
  41. wrow = 2
  42. '取得したブック一覧をシートへ書き込む
  43. For Each FSO_file In FSO_files
  44. ws.Cells(wrow, "A").Value = FSO_file.name
  45. wrow = wrow + 1
  46. Next
  47. maxrow = wrow - 1
  48. 'フォルダ一覧を取得する(3階層目のフォルダのみ)
  49. T_count = 0
  50. Call get_subfolders(FSO, Start_folder, 1)
  51. '取得したフォルダ一覧をシートへ書き込む
  52. For i = 0 To T_count - 1
  53. If Hyper_Link = True Then
  54. ws.Hyperlinks.Add anchor:=Range("E" & i + 2), Address:=T_folders(i), TextToDisplay:=T_folders(i)
  55. Else
  56. ws.Cells(i + 2, "E").Value = T_folders(i)
  57. End If
  58. Next
  59. 'ファイルを移動する
  60. For wrow = 2 To maxrow
  61. fname = ws.Cells(wrow, "A").Value
  62. If CheckFileName(fname, keyword) = True Then
  63. 'ファイル名が異動対象の場合、移動を行う
  64. ecode = move_book(FSO, fname, keyword, dest_path)
  65. If dest_path <> "" Then
  66. If Hyper_Link = True Then
  67. ws.Hyperlinks.Add anchor:=Range("C" & wrow), Address:=dest_path, TextToDisplay:=dest_path
  68. Else
  69. ws.Cells(wrow, "C").Value = dest_path
  70. End If
  71. End If
  72. If ecode = 0 Then
  73. '移動結果が正常の場合
  74. ws.Cells(wrow, "B").Value = "完了"
  75. exctr = exctr + 1
  76. Else
  77. '移動結果が以上の場合
  78. ws.Cells(wrow, "B").Value = "ERR" & ecode
  79. End If
  80. Else
  81. 'ファイル名が異動対象外の場合
  82. ws.Cells(wrow, "B").Value = "ERR1"
  83. End If
  84. Next
  85. MsgBox ("移動件数=" & exctr & "件")
  86. End Sub
  87. 'ファイル移動
  88. '戻り値 0:正常 2:該当フォルダ無し 3:既に移動先のフォルダ内にファイルが存在する
  89. Private Function move_book(ByRef FSO As Object, ByVal fname As String, ByVal keyword As String, ByRef dest_path As String) As Long
  90. Dim i As Long
  91. Dim end_path As String
  92. Dim dest_file As String
  93. Dim src_file As String
  94. move_book = 3
  95. '全フォルダをチェックする
  96. For i = 0 To T_count - 1
  97. '1フォルダを取り出す
  98. dest_path = T_folders(i)
  99. '終端のフォルダ名を取得
  100. end_path = T_endnames(i)
  101. '終端のフォルダ名とキーワードの両方を大文字に変換後比較する
  102. If UCase(end_path) = UCase(keyword) Then
  103. '終端フォルダとキーワードが一致すれば、移動対象となる
  104. dest_file = dest_path & "\" & fname
  105. '移動先のフォルダ内に移動しようとするファイルが存在するなら、戻り値=3で終了
  106. If FSO.fileexists(dest_file) = True Then Exit Function
  107. 'ファイルの移動を行う
  108. src_file = Book_folder & "\" & fname
  109. Call FSO.movefile(src_file, dest_file)
  110. '戻り値=0で終了
  111. move_book = 0
  112. Exit Function
  113. End If
  114. Next
  115. '移動対象のフォルダが存在しない場合
  116. '戻り値=2で終了
  117. dest_path = ""
  118. move_book = 2
  119. End Function
  120. 'ファイル名チェック
  121. Private Function CheckFileName(ByVal fname As String, ByRef keyword As String) As Boolean
  122. Dim RE As Object
  123. Dim REmatch As Object
  124. CheckFileName = False
  125. Set RE = CreateObject("VBScript.RegExp")
  126. RE.Pattern = "^\d{6}(\s+)\d{4}(\s+)(.+?)(\s+)記録用\.xlsx$"
  127. RE.Global = True
  128. RE.IgnoreCase = True
  129. '正規表現によるファイル名のマッチ試験
  130. Set REmatch = RE.Execute(fname)
  131. 'マッチしないなら終了
  132. If REmatch.Count < 1 Then Exit Function
  133. '日付の後の空白、通し番号の後の空白、記録用の前の空白が1桁でないなら終了
  134. If Len(REmatch(0).submatches(0)) <> 1 Then Exit Function
  135. If Len(REmatch(0).submatches(1)) <> 1 Then Exit Function
  136. If Len(REmatch(0).submatches(3)) <> 1 Then Exit Function
  137. 'キーワードを設定
  138. keyword = REmatch(0).submatches(2)
  139. CheckFileName = True
  140. End Function
  141. 'サブフォルダの取得(再帰)
  142. Sub get_subfolders(ByRef FSO As Object, ByVal s_folder As String, ByVal level As Long)
  143. Dim folder As Object
  144. '指定フォルダ内の全フォルダを処理する
  145. For Each folder In FSO.getfolder(s_folder).subfolders
  146. ReDim Preserve T_folders(T_count)
  147. ReDim Preserve T_endnames(T_count)
  148. T_folders(T_count) = folder.path
  149. T_endnames(T_count) = folder.name
  150. T_count = T_count + 1
  151. '取得したフォルダの下位フォルダを取得する
  152. Call get_subfolders(FSO, folder.path, level + 1)
  153. Next
  154. End Sub
  155.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty