Results 1 to 5 of 5

Thread: [RESOLVED] Need help to modify copy file function

  1. #1

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Resolved [RESOLVED] Need help to modify copy file function

    Hi all,

    Here is the code I have to copy the file:
    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
    12. End Type
    13.  
    14. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    15.  
    16. Private Const FOF_ALLOWUNDO = &H40
    17. Private Const FOF_NOCONFIRMATION = &H10
    18. Private Const FO_COPY = &H2
    19.  
    20. Public Function ShellFileCopy(src As String, dest As String, Optional NoConfirm As Boolean = False) As Boolean
    21. 'PURPOSE: COPY FILES VIA SHELL API
    22. 'THIS DISPLAYS THE COPY PROGRESS DIALOG BOX
    23. 'PARAMETERS: src: Source File (FullPath)
    24. 'dest: Destination File (FullPath)
    25. 'NoConfirm (Optional): If set to true, no confirmation box is displayed when overwriting
    26. 'existing files, and no copy progress dialog box is displayed.
    27.  
    28. 'Returns (True if Successful, false otherwise)
    29.  
    30. 'EXAMPLE:
    31. 'dim bSuccess as boolean
    32. 'bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt")
    33. 'bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt", False)
    34.  
    35.     Dim WinType_SFO As SHFILEOPSTRUCT
    36.     Dim lRet As Long
    37.     Dim lflags As Long
    38.  
    39.     lflags = FOF_ALLOWUNDO
    40.     If NoConfirm Then lflags = lflags & FOF_NOCONFIRMATION
    41.     With WinType_SFO
    42.         .wFunc = FO_COPY
    43.         .pFrom = src
    44.         .pTo = dest
    45.         .fFlags = lflags
    46.     End With
    47.  
    48.     lRet = SHFileOperation(WinType_SFO)
    49.     ShellFileCopy = (lRet = 0)
    50. End Function
    51.  
    52. Private Sub Form_Load()
    53. Dim bsuccess As Boolean
    54. bsuccess = ShellFileCopy("C:\Temp\Excel[1].zip", "C:\Temp\Excel[2].zip", False)
    55. End Sub

    What I want to do is to modify this function to copy the file with animated progress and overwrite the existing file by default.

    Thanks,

    Kanna.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help to modify copy file function

    Just OR these two flags.
    VB Code:
    1. Private Const FOF_NOCONFIRMMKDIR As Long = &H200 'For folder copies with sub directories.
    2. Private Const FOF_SIMPLEPROGRESS As Long = &H100 'Animated progress dialog
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Re: Need help to modify copy file function

    not sure if it helps you any, but it may be of interest.

    copyfile win32api function:

    VB Code:
    1. Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long

    use:

    VB Code:
    1. value& = 1
    2. c0py& = CopyFile("C:\Junk(1).txt","C:\Junk(2).txt",value&)

    here value& is defined as true(non zero), and will not overwrite an existing file. if defined as 0, it will. c0py& will return zero on failure, nonzero on success, and sets GetLastError api. while it doesn't provide animated progress, it is very quick.

  4. #4

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Re: Need help to modify copy file function

    Hi Rob,

    I can't get you. where I have to change in my code?

    Hi ghettohacker,

    I have your function already. i want to show the animation progress at the same time i don't like to ask the user to replace the existing file.

    thanks,

    Kanna

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help to modify copy file function

    The APIs we are using are the same ones that Windows uses when it displays the file copy progress dialog.

    Try adding the costs like so.

    VB Code:
    1. .fFlags = lflags OR FOF_SIMPLEPROGRESS OR FOF_NOCONFIRMMKDIR 'If your copying folders.
    2. 'Or another option...
    3. .fFlags = lflags OR FOF_SIMPLEPROGRESS 'For only showing the progressbar dialog.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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