将文件夹移动到另一个目录

时间:2020-03-10 07:38:17

标签: excel vba directory move

我最近在这里发布了一个有关将文件移动到另一个目录(Moving files to another directory)的问题,现在我想移动文件夹,然后将其存档。

布局与A中的现有文件夹,B中的目标和C列相同,以确认是否完成。

image example

提供的代码为

Sub move_files()
    Dim i As Long
    With ActiveSheet
        For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
            Err.Clear
            On Error Resume Next
            Name (.Cells(i, 1)) As .Cells(i, 2) & "\" & StrReverse(Split(StrReverse(.Cells(i, 1)), "\")(0))
            If Err = 0 Then .Cells(i, 3) = "YES" Else .Cells(i, 3) = "NO"
            On Error GoTo 0
        Next
    End With
End Sub

鉴于我正在尝试移动整列,没有人知道上述内容是否适合移动文件夹,因为该文件夹当前仅适用于文件。我在网上搜索过,但通常只搜索一个文件。

1 个答案:

答案 0 :(得分:1)

这是仅移动文件夹的修订版。希望它会起作用。

Sub move_folders()
  Dim i As Long
  Dim oFSO As Object
  Dim sep As String

  Set oFSO = CreateObject("Scripting.FileSystemObject")
  With ActiveSheet
    For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
      Err.Clear
      If Left(StrReverse(.Cells(i, 2)), 1) = "\" Then sep = "" Else sep = "\"
      On Error Resume Next
      oFSO.MoveFolder .Cells(i, 1), .Cells(i, 2) & sep & StrReverse(Split(StrReverse(.Cells(i, 1)), "\")(0))
      If Err = 0 Then .Cells(i, 3) = "YES" Else .Cells(i, 3) = "NO"
      On Error GoTo 0
    Next
  End With
End Sub