Jump to content
Sign in to follow this  
kman

File Copy and Progress Bar

Recommended Posts

kman

I have been looking through the forums for a way to copy all the contents of a folder on a server to replace existing files on the local computer with a progress bar. JdeB's looks to be a nice and simple way to go that I have found, but I run into a problem with it wanting to confirm the File Replace. It is not selecting "yes to all". Can anyone see what I am doing wrong. I thought the "Local $FOF_RESPOND_YES = 16" would handle this. Any ideas or suggestions would be greatly appreciated. Sorry for my newbness!! :)

_FileCopy("\\folder\folder\folder\*.*","C:\folder")

Func _FileCopy($fromFile,$tofile)

Local $FOF_RESPOND_YES = 16

Local $FOF_SIMPLEPROGRESS = 256

$winShell = ObjCreate("shell.application")

$winShell.namespace($tofile).CopyHere($fromFile,$FOF_RESPOND_YES)

EndFunc

Here is the code Im taking from:

CODE: AutoIt

;~ 4 Do not display a progress dialog box.

;~ 8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.

;~ 16 Respond with "Yes to All" for any dialog box that is displayed.

;~ 64 Preserve undo information, if possible.

;~ 128 Perform the operation on files only if a wildcard file name (*.*) is specified.

;~ 256 Display a progress dialog box but do not show the file names.

;~ 512 Do not confirm the creation of a new directory if the operation requires one to be created.

;~ 1024 Do not display a user interface if an error occurs.

;~ 2048 Version 4.71. Do not copy the security attributes of the file.

;~ 4096 Only operate in the local directory. Don't operate recursively into subdirectories.

;~ 9182 Version 5.0. Do not copy connected files as a group. Only copy the specified files.

_FileCopy("C:\Installed Apps\Patches\WindowsXP-KB835935-SP2-ENU.exe","C:\temp")

Func _FileCopy($fromFile,$tofile)

Local $FOF_RESPOND_YES = 16

Local $FOF_SIMPLEPROGRESS = 256

$winShell = ObjCreate("shell.application")

$winShell.namespace($tofile).CopyHere($fromFile,$FOF_RESPOND_YES)

EndFunc

Share this post


Link to post
Share on other sites
lod3n

Programs or scripts that use the CopyHere method might not copy items that are specified by using a wildcard character

http://support.microsoft.com/?kbid=835926

Either apply the required hotfix or latest service pack. OR, copy the files one by one (can't use *.* with FOF_RESPOND_YES)


[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites
kman

Thanks for the reply! Nice catch on the Microsoft issue too!

All servers and PC's are up to date, and I am using *.* with FileCopy earlier in the script but only for a few small files so I dont need a progress bar with that, but with this having more files thats why the need for a progress bar. The issue then is with the Wildcard and FOF_RESOPOND_YES. Any other ideas to modify or another way to go?

Thanks

Share this post


Link to post
Share on other sites
lod3n

I'd make an array of all files to copy ($aSrc), and all destinations ($aDest), and copy them one by one.

for $i = 0 to Ubound($aSrc)
$winShell.namespace($aDest[$i]).CopyHere($aSrc[$i],$FOF_RESPOND_YES)
next

Using CopyHere, I don't see what other choices you have, considering the bug.


[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites
lod3n

I just did some testing, and it looks like if you drop the *.*, it works just fine! So if you want top copy

\\server\share\FolderName

to

C:\FolderName

use

_FileCopy("\\server\share\FolderName\","C:\")

The only downside is if you don't want your destination folder name to match the source folder name. Perhaps this could be solved by renaming after the fact.


[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites
kman

I was able to get that with copying folder to folder but I have about 400 files on a server that need to overwrite files in a folder on the local PC and without a wildcard option thats messin with me.

Share this post


Link to post
Share on other sites
lod3n

Okay, it looks like CopyHere is an abstraction of a Windows API call called SHFileOperation.

SumTingWong posted an implementation of SHFileOperation back in 2005, but there were some problems. For one, he wasn't handling errors properly, and he was using the old method of removing structs. I've updated it for 2007, so here you go:

#region consts
Global Const $FO_MOVE                   = 0x0001
Global Const $FO_COPY                   = 0x0002
Global Const $FO_DELETE                 = 0x0003
Global Const $FO_RENAME                 = 0x0004

Global Const $FOF_MULTIDESTFILES        = 0x0001
Global Const $FOF_CONFIRMMOUSE          = 0x0002
Global Const $FOF_SILENT                = 0x0004
Global Const $FOF_RENAMEONCOLLISION     = 0x0008
Global Const $FOF_NOCONFIRMATION        = 0x0010
Global Const $FOF_WANTMAPPINGHANDLE     = 0x0020
Global Const $FOF_ALLOWUNDO             = 0x0040
Global Const $FOF_FILESONLY             = 0x0080
Global Const $FOF_SIMPLEPROGRESS        = 0x0100
Global Const $FOF_NOCONFIRMMKDIR        = 0x0200
Global Const $FOF_NOERRORUI             = 0x0400
Global Const $FOF_NOCOPYSECURITYATTRIBS = 0x0800
Global Const $FOF_NORECURSION           = 0x1000
Global Const $FOF_NO_CONNECTED_ELEMENTS = 0x2000
Global Const $FOF_WANTNUKEWARNING       = 0x4000
Global Const $FOF_NORECURSEREPARSE      = 0x8000
#endregion consts


$result = _CopyWithProgress("\\folder\folder\folder\*.*","C:\folder")

Func _CopyWithProgress($sFrom, $sTo)
    ; version 1 by SumTingWong on 5/26/2006
    ; http://www.autoitscript.com/forum/index.php?showtopic=11888
    ; updated by lod3n on 6/5/2007
    
    Local $SHFILEOPSTRUCT
    Local $pFrom
    Local $pTo
    Local $aDllRet
    Local $nError = 0
    Local $i
    
    $SHFILEOPSTRUCT = DllStructCreate("int;uint;ptr;ptr;uint;int;ptr;ptr")
    If @error Then Return "nostruct"        
; hwnd
    DllStructSetData($SHFILEOPSTRUCT, 1, 0)
; wFunc
    DllStructSetData($SHFILEOPSTRUCT, 2, $FO_COPY)
; pFrom
    $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
; pFrom will now be null-terminated at StringLen($sFrom)+1
    DllStructSetData($pFrom, 1, $sFrom)
    For $i = 1 To StringLen($sFrom)+2
        If DllStructGetData($pFrom, 1, $i) = 10 Then DllStructSetData($pFrom, 1, 0, $i)
    Next
; We need a second null at the end
    DllStructSetData($pFrom, 1, 0, StringLen($sFrom)+2)
    DllStructSetData($SHFILEOPSTRUCT, 3, DllStructGetPtr($pFrom))
; pTo
    $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
; pTo will now be null-terminated at StringLen($sTo)+1
    DllStructSetData($pTo, 1, $sTo)
; We need a second null at the end
    DllStructSetData($pTo, 1, 0, StringLen($sTo)+2)
    DllStructSetData($SHFILEOPSTRUCT, 4, DllStructGetPtr($pTo))
; fFlags
    DllStructSetData($SHFILEOPSTRUCT, 5, BitOR($FOF_NOCONFIRMMKDIR, _
        $FOF_NOCONFIRMATION, _
        $FOF_NOERRORUI))
; fAnyOperationsAborted
    DllStructSetData($SHFILEOPSTRUCT, 6, 0)
; hNameMappings
    DllStructSetData($SHFILEOPSTRUCT, 7, 0)
; lpszProgressTitle
    DllStructSetData($SHFILEOPSTRUCT, 8, 0)
    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    $retcode = $aDllRet[0]
    $pFrom = 0
    $pTo = 0
    $SHFILEOPSTRUCT = 0
    If $retcode <> 0 Then
        ConsoleWrite(hex($retcode) & ": " & SHFileOperationErrDecode($retcode) & @crlf)
        SetError($nError)
        Return False
    EndIf
    Return True
EndFunc


func SHFileOperationErrDecode($errNum)
    Switch $errNum
        case 113    
            return "The source and destination files are the same file."
        case 114    
            return "Multiple file paths were specified in the source buffer, but only one destination file path."
        case 115    
            return "Rename operation was specified but the destination path is a different directory. Use the move operation instead."
        case 116    
            return "The source is a root directory, which cannot be moved or renamed."
        case 117    
            return "The operation was cancelled by the user, or silently cancelled if the appropriate flags were supplied to SHFileOperation."
        case 118    
            return "The destination is a subtree of the source."
        case 120    
            return "Security settings denied access to the source."
        case 121    
            return "The source or destination path exceeded or would exceed MAX_PATH."
        case 122    
            return "The operation involved multiple destination paths, which can fail in the case of a move operation."
        case 124    
            return "The path in the source or destination or both was invalid."
        case 125    
            return "The source and destination have the same parent folder."
        case 126    
            return "The destination path is an existing file."
        case 128    
            return "The destination path is an existing folder."
        case 129    
            return "The name of the file exceeds MAX_PATH."
        case 130    
            return "The destination is a read-only CD-ROM, possibly unformatted."
        case 131    
            return "The destination is a read-only DVD, possibly unformatted."
        case 132    
            return "The destination is a writable CD-ROM, possibly unformatted."
        case 133    
            return "The file involved in the operation is too large for the destination media or file system."
        case 134    
            return "The source is a read-only CD-ROM, possibly unformatted."
        case 135    
            return "The source is a read-only DVD, possibly unformatted."
        case 136    
            return "The source is a writable CD-ROM, possibly unformatted."
        case 183    
            return "MAX_PATH was exceeded during the operation."
        case 1026   
            return "An unknown error occurred. This is typically due to an invalid path in the source or destination. This error does not occur on Microsoft Windows Vista and later."
        case 65536  
            return "An unspecified error occurred on the destination."
        case 65652  
            return "Destination is a root directory and cannot be renamed."         
    EndSwitch
    
    return "SHFileOperation returned errorcode " & hex($errNum) & ", which is not recognized"
EndFunc
Edited by lod3n

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites
kman

Worked like a dream!! Copied over all the files and even the folders I needed!! Thanks so much for your time and effort!! :)

Share this post


Link to post
Share on other sites
lod3n

You're welcome. :)

I've actually been looking for something like this too, so I also had a personal motivation for sorting it out.


[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×