Copy a bunch of files but can't cancel in the middle of copy 
Author Message
 Copy a bunch of files but can't cancel in the middle of copy

Hi,  I am trying to copy a folder using the VBScripting runtime. I create a
filesystemobject
and get a folder object and use it's copy method but... I want to be able to
cancel the copy
 in mid copy if the user presses cancel How do I doit? ps. do I need the set
objFS =nothing?
or when I unload the form will it release the memory used by the object?
here's the code...

Private Sub cmdCancel_Click()
Set objFS = Nothing
Set objFolder = Nothing
Unload Me
End Sub

Private Sub cmdCopy_Click()
cmdCancel.SetFocus
subCopyFiles
End Sub

Public Sub subCopyFiles()
Dim objFS As FileSystemObject
Dim objFolder As Folder
Set objFS = New FileSystemObject
Set objFolder = objFS.GetFolder("c:\junk\")
objFolder.Copy ("c:\junk2\")
End Sub



Sat, 04 Aug 2001 03:00:00 GMT  
 Copy a bunch of files but can't cancel in the middle of copy
IMHO, the Windows Shell makes a better copy mechanism I think than the
scripting object.  Here's a real simple example of a large copy (for me,
~23mb).  When you use the Win95 Shell to copy, it provides its familiar
dialog that shows the "flying pages", the progress indicator, and the Cancel
button.  Paste this code into a form and change the file copied in the
Form_Click routine to a large one you have and try it out.

HTH

---------------------------------------------
Option Explicit

' start ShellFileOperation declarations
Private Const FOF_SILENT = &H4
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_NOCONFIRMMKDIR = &H200

Public Enum FileOperationEnum
  foeMove = &H1
  foeCopy = &H2
  foeDelete = &H3
  foeRename = &H4
End Enum

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 Function SHFileOperation _
                Lib "shell32.dll" _
                Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As
Long
' end ShellFileOperation declarations

Private Sub Form_Click()
  ShellFileOperation Me.hwnd, "C:\PROMART\Data\Osceola\suspend.mdb", "D:\",
foeCopy, , "Copying Suspend..."
End Sub

Public Sub ShellFileOperation(ByVal lnghWnd As Long, _
                              ByVal strSource As String, _
                              ByVal strDestination As String, _
                              ByVal foeOperation As FileOperationEnum, _
                              Optional ByVal fSilent As Boolean = False, _
                              Optional ByVal strTitle As String =
"Performing file operations...")
  ' Comments  : Performs shell file operations
  ' Parameters: lnghWnd - handle to window to serve as
  '             the parent for the dialog. Use a form's
  '             hWnd property for example
  '             strSource - source file or folder
  '             strDestination - destination file or folder
  '             fSilent - if true, no warnings are displayed
  '             strTitle - title of the progress dialog
  ' Returns   : Nothing
  '
  Dim foData    As SHFILEOPSTRUCT
  Dim lngFlags  As Long
  Dim lngResult As Long

  On Error GoTo PROC_ERR

  ' set flags for no prompting
  If fSilent Then
    lngFlags = FOF_NOCONFIRMMKDIR Or FOF_NOCONFIRMATION Or FOF_SILENT
  End If

  ' set shell file operations settings
  With foData
    .hwnd = lnghWnd
    .pFrom = strSource
    .pTo = strDestination
    .fFlags = lngFlags
    .lpszProgressTitle = strTitle
    .wFunc = foeOperation

    ' do it
    lngResult = SHFileOperation(foData)
  End With

PROC_EXIT:
  Exit Sub

PROC_ERR:
  MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
    "ShellFileOperation"

  Resume PROC_EXIT
End Sub
---------------------------------------------
--
Earl Damron, MCSD, MVP (Visual Basic)

Common Controls Replacement Project Member
http://www.mvps.org/ccrp

Quote:

>Hi,  I am trying to copy a folder using the vbscripting runtime. I create a
>filesystemobject
>and get a folder object and use it's copy method but... I want to be able
to
>cancel the copy
> in mid copy if the user presses cancel How do I doit? ps. do I need the
set
>objFS =nothing?
>or when I unload the form will it release the memory used by the object?
>here's the code...

>Private Sub cmdCancel_Click()
>Set objFS = Nothing
>Set objFolder = Nothing
>Unload Me
>End Sub

>Private Sub cmdCopy_Click()
>cmdCancel.SetFocus
>subCopyFiles
>End Sub

>Public Sub subCopyFiles()
>Dim objFS As FileSystemObject
>Dim objFolder As Folder
>Set objFS = New FileSystemObject
>Set objFolder = objFS.GetFolder("c:\junk\")
>objFolder.Copy ("c:\junk2\")
>End Sub



Mon, 06 Aug 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Find a copy a file with File.Copy method

2. how to use Volume Shadow Copy to copy an in use file

3. Copy a file in a folder and past the copy in another folder

4. Rich Text File (.RTF) -Can't copy multiple .rtf files

5. Rich Text File (.RTF) -Can't copy multiple .rtf files (VB 6.0 Enterprise)

6. copy folder and copy files

7. Can't copy file after recent modification

8. Copy Files using FileSystemObject (Doesn't work)

9. Copy BLOB's out of Oracle into a file using VBA in Access 97

10. File copying from cd-rom's and Read-Only Attributes

11. Copying Files into the Briefcase doesn't work

12. Syncronizing and copying files between PC's and other drives

 

 
Powered by phpBB® Forum Software