2012-05-08 141 views
0

我正在嘗試將我的文件批量移動到具有新名稱的新文件夾。例如,我想將所有文件從移動:如何將文件批量移動到同名文件夾?

C:\貓\
C:\狗\

要:

C:\測試\ CAT-1234 \
C: \測試\狗-2477 \

我有大約400個文件夾,我想這樣移動。

目標文件夾將始終包含原始文件夾名稱。

+0

任何你想使用VBA移動文件的原因?這可能不是最合適的語言。 – assylias

+0

@assylias什麼是適當的語言來做到這一點?我想使用VBA的唯一原因是,如果有問題,我可以調試代碼。我想象一個PowerShell腳本可能是最好的,但我對PS不是很熟悉。 – toolshed

+0

我對PS也不熟悉,但我確實認爲這是shell腳本可以做得很好的事情。 – assylias

回答

1

是的,你可以將文件從一個文件夾複製到另一個文件夾。你可以使用API​​ SHFileOperation :)它也會給你動畫的樣子,當你從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 
+0

謝謝悉達思,這讓我走上了正軌! – toolshed

0

我認爲沒有批量移動這樣的事情,即使你編寫了一個批處理程序,操作系統也只會讓你在同一時間執行一個文件。

相關問題