[RESOLVED] Need help to modify copy file function
Hi all,
Here is the code I have to copy the file:
VB Code:
Option Explicit
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
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private Const FO_COPY = &H2
Public Function ShellFileCopy(src As String, dest As String, Optional NoConfirm As Boolean = False) As Boolean
'PURPOSE: COPY FILES VIA SHELL API
'THIS DISPLAYS THE COPY PROGRESS DIALOG BOX
'PARAMETERS: src: Source File (FullPath)
'dest: Destination File (FullPath)
'NoConfirm (Optional): If set to true, no confirmation box is displayed when overwriting
'existing files, and no copy progress dialog box is displayed.
'Returns (True if Successful, false otherwise)
'EXAMPLE:
'dim bSuccess as boolean
'bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt")
'bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt", False)
Dim WinType_SFO As SHFILEOPSTRUCT
Dim lRet As Long
Dim lflags As Long
lflags = FOF_ALLOWUNDO
If NoConfirm Then lflags = lflags & FOF_NOCONFIRMATION
With WinType_SFO
.wFunc = FO_COPY
.pFrom = src
.pTo = dest
.fFlags = lflags
End With
lRet = SHFileOperation(WinType_SFO)
ShellFileCopy = (lRet = 0)
End Function
Private Sub Form_Load()
Dim bsuccess As Boolean
bsuccess = ShellFileCopy("C:\Temp\Excel[1].zip", "C:\Temp\Excel[2].zip", False)
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.
Re: Need help to modify copy file function
Just OR these two flags.
VB Code:
Private Const FOF_NOCONFIRMMKDIR As Long = &H200 'For folder copies with sub directories.
Private Const FOF_SIMPLEPROGRESS As Long = &H100 'Animated progress dialog
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:
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:
value& = 1
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.
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
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:
.fFlags = lflags OR FOF_SIMPLEPROGRESS OR FOF_NOCONFIRMMKDIR 'If your copying folders.
'Or another option...
.fFlags = lflags OR FOF_SIMPLEPROGRESS 'For only showing the progressbar dialog.
:)