Results 1 to 12 of 12

Thread: [RESOLVED] Automatically copy

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Automatically copy

    Hi,

    Is it possible to modify this code so that is will automatically either copy the file to the location or keep both files depending on whether the file exists or not?

    vb Code:
    1. Private Type SHFILEOPSTRUCT
    2.     hWnd As Long
    3.     wFunc As Long
    4.     pFrom As String
    5.     pTo As String
    6.     fFlags As Integer
    7.     fAnyOperationsAborted As Long
    8.     hNameMappings As Long
    9.     lpszProgressTitle As Long ' only used if FOF_SIMPLEPROGRESS, sets dialog title
    10. End Type
    11. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    12. ' Available Operations
    13. Private Const FO_COPY = &H2 ' Copy File/Folder
    14. Private Const FO_DELETE = &H3  ' Delete File/Folder
    15. Private Const FO_MOVE = &H1  ' Move File/Folder
    16. Private Const FO_RENAME = &H4  ' Rename File/Folder
    17. ' Flags
    18. Private Const FOF_ALLOWUNDO = &H40  ' Allow to undo rename, delete ie sends to recycle bin
    19. Private Const FOF_FILESONLY = &H80  ' Only allow files
    20. Private Const FOF_NOCONFIRMATION = &H10  ' No File Delete or Overwrite Confirmation Dialog
    21. Private Const FOF_SILENT = &H4  ' No copy/move dialog
    22. Private Const FOF_SIMPLEPROGRESS = &H100  ' Does not display file names
    23.  
    24. Public Sub VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
    25.  
    26.     Dim op As SHFILEOPSTRUCT
    27.     With op
    28.         .wFunc = FO_COPY ' Set function
    29.         .pTo = strTarget  ' Set new path
    30.         .pFrom = strSource ' Set current path
    31.         .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION And FOF_FILESONLY
    32.     End With
    33.     ' Perform operation
    34.     SHFileOperation op
    35.  
    36. End Sub

    Usage:

    vb Code:
    1. VBCopyFolder fullpath & "\*", d

    Such as the dialog asks the user whether or not they want to override an existing file or keep both files. Is it possible to do that automatically without the user interaction?

    Thanks,


    Nightwalker
    Last edited by Nightwalker83; May 4th, 2012 at 05:53 AM. Reason: Fixed spelling!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Automatically copy

    Never mind, Randy Birch's code has saved me once again! Here it is.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Automatically copy

    Hi

    I have written below code but it gives me this error , 'Unknown Error 70 " Permission denied

    FileCopy filename, dfolder

    dfolder = "\\comp1\Shared Folder\"

    Shared Folder has permissions of Full Control.

    Thanks

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Automatically copy

    You have to specify the target filename

  5. #5
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: [RESOLVED] Automatically copy

    Hi Miser

    What i need to change

    filename = "e:\" & "abx.xls"
    dfolder = "\\comp1\Shared Folder\"

    filecopy filename,dfolder

    Thanks

  6. #6
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: [RESOLVED] Automatically copy

    Hi

    Secondly i want if file exists in Target Folder it should overwrite

    Thanks

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Automatically copy

    As I said you need to specify the target filename. The code you showed tells it where to put the file but does not tell it what name to use.

    filecopy source, target

    you can check to see if the file exists before you try to copy and use the kill statement to remove the existing file and then copy or you can try to copy and trap the error that will occur if the file already exists and then remove the existing file and retry the copy or you can use another method to copy the file.

  8. #8

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Automatically copy

    Quote Originally Posted by Jagjit View Post
    Hi

    Secondly i want if file exists in Target Folder it should overwrite

    Thanks
    You need to add

    Code:
    Private Const FOF_RENAMEONCOLLISION As Long = &H8
    under where it has "
    Available Operations" in the first post and change the flag to
    Code:
    .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY Or 
    FOF_RENAMEONCOLLISION
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Automatically copy

    That would be fine if he was using the API but it looks like he is using the FileCopy function within VB so that would not apply.

  10. #10

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Automatically copy

    Quote Originally Posted by DataMiser View Post
    That would be fine if he was using the API but it looks like he is using the FileCopy function within VB so that would not apply.
    Well, if he is FileCopy there was no need for him to reply to this thread.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Automatically copy

    Agreed, He should have started his own thread or better still looked at the example in the online help.

  12. #12

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Automatically copy

    Quote Originally Posted by DataMiser View Post
    Agreed, He should have started his own thread or better still looked at the example in the online help.
    He did started his own thread that is where I linked to this thread because it wasn't clear what he was planning to do! I gave him the above code as an
    alternative to the FileCopy method in case he want to use rename, etc later on.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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