Results 1 to 13 of 13

Thread: strt.CopyFolder

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    strt.CopyFolder

    Hello

    i have got a command button on a form which used to copy the contents of the folder to another folder, however it is now throwing an error for some reason.

    it comes up with the msg "error" because of the messagebox
    can you guys tell me how to figure out why is it throwing an error?

    VB Code:
    1. Dim strt As New FileSystemObject
    2.  
    3. '        'this actually over writes all files in the currentDirPath directory with those stored under the initialize directory
    4.     Command2.BackColor = RGB(255, 255, 255)
    5.     Command2.Refresh
    6.     On Error GoTo Error:
    7.     'Call strt.CopyFolder(WiMAXDoORsrootdir & "\Initialize", currentDirPath, True)
    8.     Call strt.CopyFolder("C:\WiMAXDoORs\Projects\Initialize", "C:\WiMAXDoORs\Projects\SampleProject-2", True)
    9.     MsgBox "no error"
    10.  
    11. Error:
    12.     Command2.BackColor = &H8000000F
    13.     MsgBox "error"
    14.     Command2.Refresh


    i removed the error handler and it says

    Run-time error '-2147023672 (800704c8)':
    Method 'CopyFolder' of object 'IFileSystem3' failed
    and it comes up with end, debug, help
    Last edited by vb_student; Oct 5th, 2005 at 10:57 AM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: strt.CopyFolder

    Frist you need Exit Sub before the Error: label or error handler will get executed every time you run your procedure:
    VB Code:
    1. MsgBox "no error"
    2.  
    3.     [b]Exit Sub[/b]
    4.  
    5. Error:
    6.     Command2.BackColor = &H8000000F
    7.     MsgBox "error"
    8.     Command2.Refresh

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: strt.CopyFolder

    If destination is an existing file, an error occurs.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: strt.CopyFolder

    i thought the "true" variable took care of the destination file

    i looked up the VB reference and it turned up

    VB Code:
    1. object.CopyFolder source, destination[, overwrite]

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: strt.CopyFolder

    Yes, but in the case there's a file that has the same name (has no extension) then it can't create a folder there. For all other cases, the overwrite parameter would have taken care of it.

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: strt.CopyFolder

    This error may occur if an existing file in the destination folder is open by another application.

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: strt.CopyFolder

    Here is what I use to copy folder(s):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type SHFILEOPSTRUCT
    4.         hwnd As Long
    5.         wFunc As Long
    6.         pFrom As String
    7.         pTo As String
    8.         fFlags As Integer
    9.         fAnyOperationsAborted As Long
    10.         hNameMappings As Long
    11.         lpszProgressTitle As String '  only used if FOF_SIMPLEPROGRESS
    12. End Type
    13.  
    14. Private Const FOF_MULTIDESTFILES = &H1
    15. Private Const FOF_CONFIRMMOUSE = &H2
    16. Private Const FOF_SILENT = &H4
    17. Private Const FOF_RENAMEONCOLLISION = &H8
    18. Private Const FOF_NOCONFIRMATION = &H10
    19. Private Const FOF_WANTMAPPINGHANDLE = &H20
    20. Private Const FOF_CREATEPROGRESSDLG = &H0
    21. Private Const FOF_ALLOWUNDO = &H40
    22. Private Const FOF_FILESONLY = &H80
    23. Private Const FOF_SIMPLEPROGRESS = &H100
    24. Private Const FOF_NOCONFIRMMKDIR = &H200
    25.  
    26. Private Const FO_MOVE = 1
    27. Private Const FO_COPY = 2
    28. Private Const FO_DELETE = 3
    29. Private Const FO_RENAME = 4
    30.  
    31. Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
    32.    
    33. Public Function CopyFolder(ByVal strSource As String, ByVal strDest As String) As Boolean
    34. '=========================================================================================
    35. Dim varFOS As SHFILEOPSTRUCT
    36.     With varFOS
    37.         .fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOCONFIRMMKDIR
    38.         .wFunc = FO_COPY
    39.         .pFrom = strSource
    40.         .pTo = strDest
    41.     End With
    42.     Call SHFileOperation(varFOS)
    43.     CopyFolder = (varFOS.fAnyOperationsAborted = 0)
    44. End Function
    45.  
    46. Private Sub Command1_Click()
    47.     CopyFolder "c:\temp\test", "c:\temp\test2"
    48. End Sub

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: strt.CopyFolder

    If you decided to do the copy again... so the folder and files are already existing from the previous copy. And during the 2nd copy, a file in the folder was opened by another app.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: strt.CopyFolder

    leinad31 did not quite understand what you meant by

    es, but in the case there's a file that has the same name (has no extension) then it can't create a folder there. For all other cases, the overwrite parameter would have taken care of it.
    you mean if there is a file with the name as the containing folder?

    brucevde, you mean if i am looking at the file in windows explorer i will get the error?

    is there any way (error code) which will tell me why the error occured?

    rhino, basically i have an initialise folder which contains inital settings in files, say i have fooinitialisefolder1 which has foo1.xls, foo2.xls, foo3.xls
    and then i have a bunch of folders which the users can modify

    e.g. i have a destination folder foodetfolder with the files foo1.xls, foo2.xls, foo3.xls (however foo1.xls, foo2.xls & foo3.xls have slightly different contents then the files in fooinitialisefolder)

    now i have this initialise button which causes the overwriting or replacement of the foo1.xls, foo2.xls and foo3.xls files in the foodestfolder with the foo1.xls, foo2.xls & foo3.xls in the fooinitialisefolder

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: strt.CopyFolder

    I'll tackle it from another direction... if you can have folders with extensions then you can have files with no extension. For a given name, either its a directory or a file it cant be both.

    In the attached pic I renamed a folder to have an extension txt. So if yuo were doing a folder copy but there was already a file using the same name as the folder then an error will occur. That could be one source of error, brucevd pointed out another possible cause of the error.
    Attached Images Attached Images  

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: strt.CopyFolder

    so you mean to say that say if the containing folder is foofoo and a contained file is foofoo.txt then the error will occur, correct?

    if i have the folder open in windows explorer, could that cause a problem.

    is there any way one can find the cause of the error by looking at some error code?

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: strt.CopyFolder

    No....

    Call strt.CopyFolder("C:\WiMAXDoORs\Projects\Initialize", "C:\WiMAXDoORs\Projects\SampleProject-2", True)

    If SampleProject-2 already exists in destination and is a file then an error will occur
    If SampleProject-2 already exists in destination and is a folder then it will overwrite the folder and it's content
    If while overwriting the content of SampleProject-2 it encounters a file that is being USED by another application (such as an open word document) then it can't overwrite that file and an error will occur

    Having a folder open in windows explorer will not cause the error. It's having an open FILE that will cause an error.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: strt.CopyFolder

    thanks for the reply, then in that case i probably have an open file in the detination folder.

    my source and destination folders both contain excel files, is there any way of checking if the destination foder contains an open file(any of the excel files is open?)?

    none of myfies are open in MS excel, the copyfolder seems to be a temperamental function, sometimes it works,sometimes it does not. i have also checked in the task manager there is no instance of MS Excel running
    Last edited by vb_student; Oct 6th, 2005 at 08:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width