Results 1 to 15 of 15

Thread: copy entire folder and sub folders

  1. #1

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    copy entire folder and sub folders

    hi, i am making a program that i would like to copy a folder from one workstation/document server to my network server. how can i make an app copy a folder (E:\documents\ or //A/DOCS$/) to a server (//HOST/DOCS$ or C:\Documents\) to backup documents and overwrite the old files and do this every 10 or 15 mins. i am making this program incase one of my servers brakes down.
    Thanks, dandono.
    If there is only one perfect person in the universe, does that make them imperfect?

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

    Re: copy entire folder and sub folders

    Some APIs are involved so here is a sample:
    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.  
    48.     CopyFolder "c:\temp\test1", "c:\temp\test2"
    49.  
    50. End Sub

  3. #3

  4. #4
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: copy entire folder and sub folders

    Nice to see the old Dos commands still have their uses.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  5. #5

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: copy entire folder and sub folders

    i could not get the api version to work but does the shell one copy over network aswell?
    If there is only one perfect person in the universe, does that make them imperfect?

  6. #6

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: copy entire folder and sub folders

    It doesn't appear to. I even tried mapping the drive to \david on the remote machine, and then copying \test with \test2 in it to \test

    xcopy y:\test c:\test
    0 files copied.

  8. #8

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: copy entire folder and sub folders

    sorry, i was referring to the xcopy command.

  10. #10

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

    Re: copy entire folder and sub folders

    Also, XCOPY might not recognize long names so you'll need to convert long path to short (the DOS way):
    VB Code:
    1. Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
    2.     (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
    3. Public Function GetShortPath(strFileName As String) As String
    4.     'KPD-Team 1999
    5.     'URL: [url]http://www.allapi.net/[/url]
    6.     'E-Mail: [email][email protected][/email]
    7.     Dim lngRes As Long, strPath As String
    8.     'Create a buffer
    9.     strPath = String$(165, 0)
    10.     'retrieve the short pathname
    11.     lngRes = GetShortPathName(strFileName, strPath, 164)
    12.     'remove all unnecessary chr$(0)'s
    13.     GetShortPath = Left$(strPath, lngRes)
    14. End Function
    15. Private Sub Form_Load()
    16.     MsgBox GetShortPath("c:\Program Files\")
    17. End Sub

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: copy entire folder and sub folders

    Nope, I was using \David\Test

  13. #13

  14. #14

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: copy entire folder and sub folders

    i'm using this code: Shell "xcopy " + txtloc.Text + " " + txthost.Text
    it does not seem to work and i am on a user that is a net admin. does it work if it is copying to a windows server 2003 domain controler from a windows xp professional workstation?
    If there is only one perfect person in the universe, does that make them imperfect?

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: copy entire folder and sub folders

    I would go with the API version. I fought for quite a while to try to get a batch/cmd file to work, and ran into one problem after another. I finally redid everything using an older API, and I guess that it hasn't been tested, yet.
    Usie RhinoBulls example above. It should work.

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