如何批量将文件移动到类似命名的文件夹?

时间:2012-05-08 16:23:31

标签: vba powershell

我正在尝试将我的文件批量移动到具有新名称的新文件夹。例如,我想从以下位置移动所有文件:

C:\猫\
C:\狗\

要:

C:\测试\猫-1234 \
C:\测试\狗-2477 \

我想要以这种方式移动大约400个文件夹。

目标文件夹将始终包含原始文件夹名称。

2 个答案:

答案 0 :(得分:1)

是的,您可以将文件从一个文件夹复制到另一个文件夹。您可以使用API​​ SHFileOperation :)它还会为您提供动画外观,当Windows将文件从1个文件夹复制到另一个文件夹时,您会看到以下内容:)这是一个示例。请根据您的实际需要进行修改。

Option Explicit

Private Declare Function SHFileOperation _
Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long

Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Private Const FO_COPY = &H2

Sub Sample()
    Dim FilePath1 As String, FilePath2 As String

    FilePath1 = "C:\cat"
    FilePath2 = "C:\test\cat-1234"

    '~~> Usage ~~> CopyFolder(From,To)
    '~~> Example ~~> CopyFolder("C:\Temp\1","C:\Temp\2")
    If CopyFolder(FilePath1, FilePath2) Then
        MsgBox "Copied"
    Else
        MsgBox "Not copied"
    End If
End Sub

Private Function CopyFolder(ByVal sFrom As String, _
ByVal sTo As String) As Boolean
    Dim SHFileOp As SHFILEOPSTRUCT
    On Error GoTo Whoa
    CopyFolder = False
    With SHFileOp
        .wFunc = FO_COPY
        .pFrom = sFrom
        .pTo = sTo
    End With
    SHFileOperation SHFileOp
    CopyFolder = True
    Exit Function
Whoa:
    MsgBox "Following error occurd while copying folder " & sFrom & vbCrLf & _
    Err.Description, vbExclamation, "Error message"
End Function

答案 1 :(得分:0)

我认为没有批量移动这样的事情,即使你编写批处理程序,操作系统也只允许你在一个文件中执行。