在VB中怎样将一个文件夹Florder1下的所有文件拷贝到另一文件夹Florder2中。filecopy是不是只能copy一个文件?windows api,copyfile呢?

解决方案 »

  1.   

    用循环就行了。
    或者调用dos命令。
      

  2.   

    或者使用API函数SHFileOperation
    如果你是使用Win9x/NT的话,你可以使用API函数SHFileOperation,这个函数可以同时拷贝、删除、改名或移动多个文件,甚至整个目录。如果你愿意,还可以显示相应的动画对话框,功能十分强大。SHFileOperation的参数是一个SHFILEOPSSTRUCT结构。这个结构中各成员的含义如下: 
         - hwnd - 显示文件操作对话框的窗口句柄 
         - wFunc - 表示要进行的操作,可以取以下值: 
         - FO_COPY - 拷贝文件。所要拷贝的文件由pFrom成员指定,目的地址有pTo成员指定。 
         - FO_DELETE - 删除pFrom指定的文件。(pTo 被忽略。) 
         - FO_MOVE - 移动文件。所要移动的文件由pFrom成员指定,目的地址有pTo成员指定。 
         - FO_RENAME - 改名pFrom指定的文件。 
         - pFrom - 指定文件名的缓冲区的地址。必须以Chr(0)结尾。如果包括多个文件以Chr(0)分割。 
         - pTo - 指定目的文件名或目录的缓冲区的地址。必须以Chr(0)结尾。如果使用了FOF_MULTIDESTFILES标志,可以包括多个文件名,文件名之间以Chr(0)分割。 
         - fFlags - 标志: 
         - FOF_ALLOWUNDO - 允许恢复 
         - FOF_FILESONLY - 如果使用了*.*,只操作文件。 
         - FOF_MULTIDESTFILES - pTo成员可以为多个目的文件。 
         - FOF_NOCONFIRMATION - 不显示确认对话框。 
         - FOF_NOCONFIRMMKDIR - 不确认是否建立目录。 
         - FOF_NOERRORUI - 如果有错误,不显示用户界面。 
         - FOF_RENAMEONCOLLISION - 如果目的文件已经存在,给要处理的文件一个新名字。 
         - FOF_SILENT - 不显示进度对话框。 
         - FOF_SIMPLEPROGRESS - 显示进度框,但不显示文件名。 
         - fAnyOperationsAborted -如果用户退出,该成员为TRUE,否则为FALSE。 
         - lpszProgressTitle - 进度框的标题,只有选择了FOF_SIMPLEPROGRESS标志才有效。 
         
        下面是一个例子显示如何拷贝文件: 
        -------------------- 
        1. 在Visual Basic中启动一个新的EXE工程,其中包括Form1。 
        2. 添加两个检查框和一个按钮在Form1上。 
        3. 加入以下代码到Form1的代码窗口: 
         Option Explicit 
         
         Private Const FO_COPY = &H2& 'Copies the files specified 
         'in the pFrom member to the 
         'location specified in the 
         'pTo member. 
         
         Private Const FO_DELETE = &H3& 'Deletes the files specified 
         'in pFrom (pTo is ignored.) 
         
         Private Const FO_MOVE = &H1& 'Moves the files specified 
         'in pFrom to the location 
         'specified in pTo. 
         
         Private Const FO_RENAME = &H4& 'Renames the files 
         'specified in pFrom. 
         
         Private Const FOF_ALLOWUNDO = &H40& 'Preserve Undo information. 
         
         Private Const FOF_CONFIRMMOUSE = &H2& 'Not currently implemented. 
         
         Private Const FOF_CREATEPROGRESSDLG = &H0& 'handle to the parent 
         'window for the 
         'progress dialog box. 
         
         Private Const FOF_FILESONLY = &H80& 'Perform the operation 
         'on files only if a 
         'wildcard file name 
         '(*.*) is specified. 
         
         Private Const FOF_MULTIDESTFILES = &H1& 'The pTo member 
         'specifies multiple 
         'destination files (one 
         'for each source file) 
         'rather than one 
         'directory where all 
         'source files are 
         'to be deposited. 
         
         Private Const FOF_NOCONFIRMATION = &H10& 'Respond with Yes to 
         'All for any dialog box 
         'that is displayed. 
         
         Private Const FOF_NOCONFIRMMKDIR = &H200& 'Does not confirm the 
         'creation of a new 
         'directory if the 
         'operation requires one 
         'to be created. 
         
         Private Const FOF_RENAMEONCOLLISION = &H8& 'Give the file being 
         'operated on a new name 
         'in a move, copy, or 
         'rename operation if a 
         'file with the target 
         'name already exists. 
         
         Private Const FOF_SILENT = &H4& 'Does not display a 
         'progress dialog box. 
         
         Private Const FOF_SIMPLEPROGRESS = &H100& 'Displays a progress 
         'dialog box but does 
         'not show the 
         'file names. 
         
         Private Const FOF_WANTMAPPINGHANDLE = &H20& 
         'If FOF_RENAMEONCOLLISION is specified, 
         'the hNameMappings member will be filled 
         'in if any files were renamed. 
         
         ' The SHFILOPSTRUCT is not double-word aligned. If no steps are 
         ' taken, the last 3 variables will not be passed correctly. This 
         ' has no impact unless the progress title needs to be changed. 
         
         Private Type SHFILEOPSTRUCT 
         hwnd As Long 
         wFunc As Long 
         pFrom As String 
         pTo As String 
         fFlags As Integer 
         fAnyOperationsAborted As Long 
         hNameMappings As Long 
         lpszProgressTitle As String 
         End Type 
         
         Private Declare Sub CopyMemory Lib "KERNEL32" _ 
         Alias "RtlMoveMemory" _ 
         (hpvDest As Any, _ 
         hpvSource As Any, _ 
         ByVal cbCopy As Long) 
         
         Private Declare Function SHFileOperation Lib "Shell32.dll" _ 
         Alias "SHFileOperationA" _ 
         (lpFileOp As Any) As Long 
         
         Private Sub Form_Load() 
         Check1.Caption = "Copy All Files in VB Directory" 
         Check2.Caption = "Display Custom Message" 
         Command1.Caption = "Copy Files" 
         End Sub 
         
         Private Sub Command1_Click() 
         Dim result As Long 
         Dim lenFileop As Long 
         Dim foBuf() As Byte 
         Dim fileop As SHFILEOPSTRUCT 
         
         lenFileop = LenB(fileop) ' double word alignment increase 
         ReDim foBuf(1 To lenFileop) ' the size of the structure. 
         
         With fileop 
         .hwnd = Me.hwnd 
         
         .wFunc = FO_COPY 
         
         ' The files to copy separated by Nulls and terminated by two 
         ' nulls 
         If Check1.Value = vbChecked Then 
         .pFrom = Environ("windir") & "\*.exe" 
         .fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY 
         Else 
         .pFrom = Environ("windir") & "\Explorer.exe" _ 
         & vbNullChar _ 
         & Environ("windir") & "\WinHelp.exe" _ 
         & vbNullChar _ 
         & vbNullChar 
         End If 
         
         .pTo = "C:\testfolder\" & vbNullChar & vbNullChar 
         
         If Check2.Value = vbChecked Then 
         .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or _ 
         FOF_NOCONFIRMMKDIR 
         .lpszProgressTitle = "Your custom dialog string " & _ 
         "appears here." & vbNullChar _ 
         & vbNullChar 
         End If 
         End With 
         
         ' Now we need to copy the structure into a byte array 
         Call CopyMemory(foBuf(1), fileop, lenFileop) 
         
         ' Next we move the last 12 bytes by 2 to byte align the data 
         Call CopyMemory(foBuf(19), foBuf(21), 12) 
         result = SHFileOperation(foBuf(1)) 
         
         If result <> 0 Then ' Operation failed 
         MsgBox Err.LastDllError 'Show the error returned from 
         'the API. 
         Else 
         If fileop.fAnyOperationsAborted <> 0 Then 
         MsgBox "Operation Failed" 
         End If 
         End If 
         End Sub      
        4. 运行这个例子。 
      

  3.   

    楼上的,能说得更详细一些吗?to  rainstormmaster循环??不知道文件名字怎么办?若文件夹中的文件数量和名字是不固定的呢?
      

  4.   

    faint~~~~~~~我昨天不是在你的另一个帖子里给你答案了么?怎么不好好看???=============================
        Dim fso
        Set fso = CreateObject("Scripting.FileSystemObject")
        fso.CopyFolder App.Path + "\Florder1\*", App.Path + "\Florder2\", True到 MSDN 查找 CopyFolder 的帮助!