How to get notifified when copy is complete? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

How to get notifified when copy is complete?

09 May 2021, 00:29

Hi everybody,

i'm asking for your help. I want to be able to get notified after the copying of big sizes files or folders are completed using Filecopy or FileCopyDir. Is it possible to get an accurate bar progression with optionnaly the remainning time and the total copy time displayed?

Here is an example :

Code: Select all

!::
FileCopy, %A_Desktop%\Folder1, %A_Desktop%\Folder2
return
Thanks in advance for your help.
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: How to get notifified when copy is complete?  Topic is solved

09 May 2021, 02:54

necomerx wrote:
09 May 2021, 00:29
Hi everybody,

i'm asking for your help. I want to be able to get notified after the copying of big sizes files or folders are completed using Filecopy or FileCopyDir. Is it possible to get an accurate bar progression with optionnaly the remainning time and the total copy time displayed?

Here is an example :

Code: Select all

!::
FileCopy, %A_Desktop%\Folder1, %A_Desktop%\Folder2
return
Thanks in advance for your help.
Hello there. I have done some research for you. I found this great function.

Code: Select all

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

ShellFileOperation( 0x2, A_AhkPath, A_DeskTop )                     ; Copy a Single file
ShellFileOperation( 0x2, "C:\Program Files\AutoHotkey", A_DeskTop ) ; Copy the whole folder



/*
	Provides access to Windows’ built-in file operation system 
	(move / copy / rename / delete files or folders with the standard Windows dialog and error UI).  
	Utilizes the SHFileOperation shell function in Windows.

	For online documentation
	See http://www.autohotkey.net/~Rapte_Of_Suzaku/Documentation/files/ShellFileOperation-ahk.html
	
	Release #3
	
	Joshua A. Kinnison
	2010-09-29, 15:12
*/
ShellFileOperation( fileO=0x0, fSource="", fTarget="", flags=0x0, ghwnd=0x0 )     
{ ;dout_f(A_THisFunc)
	
	; AVAILABLE OPERATIONS
	static FO_MOVE                   = 0x1
	static FO_COPY                   = 0x2
	static FO_DELETE                 = 0x3
	static FO_RENAME                 = 0x4
	
	; AVAILABLE FLAGS
	static FOF_MULTIDESTFILES        = 0x1     ; Indicates that the to member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
	static FOF_CONFIRMMOUSE          = 0x2     ; ?
	static FOF_SILENT                = 0x4     ; Does not display a progress dialog box.
	static FOF_RENAMEONCOLLISION     = 0x8     ; Gives the file being operated on a new name (such as "Copy #1 of...") in a move, copy, or rename operation if a file of the target name already exists.
	static FOF_NOCONFIRMATION        = 0x10    ; Responds with "yes to all" for any dialog box that is displayed.
	static FOF_WANTMAPPINGHANDLE     = 0x20    ; returns info about the actual result of the operation
	static FOF_ALLOWUNDO             = 0x40    ; Preserves undo information, if possible. With del, uses recycle bin.
	static FOF_FILESONLY             = 0x80    ; Performs the operation only on files if a wildcard filename (*.*) is specified.
	static FOF_SIMPLEPROGRESS        = 0x100   ; Displays a progress dialog box, but does not show the filenames.
	static FOF_NOCONFIRMMKDIR        = 0x200   ; Does not confirm the creation of a new directory if the operation requires one to be created.
	static FOF_NOERRORUI             = 0x400   ; don't put up error UI
	static FOF_NOCOPYSECURITYATTRIBS = 0x800   ; dont copy file security attributes
	static FOF_NORECURSION           = 0x1000  ; Only operate in the specified directory. Don't operate recursively into subdirectories.
	static FOF_NO_CONNECTED_ELEMENTS = 0x2000  ; Do not move connected files as a group (e.g. html file together with images). Only move the specified files.
	static FOF_WANTNUKEWARNING       = 0x4000  ; Send a warning if a file is being destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.
	static FOF_NORECURSEREPARSE      = 0x8000  ; treat reparse points as objects, not containers ?
	
	; static items for builds without objects
	static _mappings                 = "mappings"
	static _error                    = "error"
	static _aborted                  = "aborted"
	static _num_mappings             = "num_mappings"
	static make_object               = "Object"
	
	fileO := %fileO% ? %fileO% : fileO
	
	If ( SubStr(flags,0) == "|" )
		flags := SubStr(flags,1,-1)
	
	_flags := 0
	Loop Parse, flags, |
		_flags |= %A_LoopField%	
	flags := _flags ? _flags : (%flags% ? %flags% : flags)
	
	If ( SubStr(fSource,0) != "|" )
		fSource := fSource . "|"

	If ( SubStr(fTarget,0) != "|" )
		fTarget := fTarget . "|"
	
	char_size := A_IsUnicode ? 2 : 1
	char_type := A_IsUnicode ? "UShort" : "Char"
	
	fsPtr := &fSource
	Loop % StrLen(fSource)
		if NumGet(fSource, (A_Index-1)*char_size, char_type) = 124
			NumPut(0, fSource, (A_Index-1)*char_size, char_type)

	ftPtr := &fTarget
	Loop % StrLen(fTarget)
		if NumGet(fTarget, (A_Index-1)*char_size, char_type) = 124
			NumPut(0, fTarget, (A_Index-1)*char_size, char_type)
	
	VarSetCapacity( SHFILEOPSTRUCT, 60, 0 )                 ; Encoding SHFILEOPSTRUCT
	NextOffset := NumPut( ghwnd, &SHFILEOPSTRUCT )          ; hWnd of calling GUI
	NextOffset := NumPut( fileO, NextOffset+0    )          ; File operation
	NextOffset := NumPut( fsPtr, NextOffset+0    )          ; Source file / pattern
	NextOffset := NumPut( ftPtr, NextOffset+0    )          ; Target file / folder
	NextOffset := NumPut( flags, NextOffset+0, 0, "Short" ) ; options

	code    := DllCall( "Shell32\SHFileOperation" . (A_IsUnicode ? "W" : "A"), UInt,&SHFILEOPSTRUCT )
	aborted := NumGet(NextOffset+0)
	H2M_ptr := NumGet(NextOffset+4)
	
	if !IsFunc(make_object)
		ret := aborted	; if build doesn't support object, just return the aborted flag
	else
	{	
		ret             := %make_object%()
		ret[_mappings]  := %make_object%()
		ret[_error]     := ErrorLevel := code
		ret[_aborted]   := aborted

		if (FOF_WANTMAPPINGHANDLE & flags)
		{
			; HANDLETOMAPPINGS 
			ret[_num_mappings]  := NumGet( H2M_ptr+0 )
			map_ptr             := NumGet( H2M_ptr+4 )
			
			Loop % ret[_num_mappings]
			{
				; _SHNAMEMAPPING
				addr := map_ptr+(A_Index-1)*16 ;
				old  := StrGet(NumGet(addr+0))
				new  := StrGet(NumGet(addr+4))
				
				ret[_mappings][old] := new
			}
		}
	}
	
	; free mappings handle if it was requested
	if (FOF_WANTMAPPINGHANDLE & flags)
		DllCall("Shell32\SHFreeNameMappings", int, H2M_ptr)
	
	Return ret
}
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: How to get notifified when copy is complete?

09 May 2021, 09:06

Hi hasantr!
I saw this ShellFileOperation method posted by SKAN in the old forum years ago but didn't fully understand how to use the function. I will try to understand your provided example. Thanks!

Edit: Thanks again hasantr, now that i have a much better understanding of this code, copy is much more simpler than using FileCopy and some other lines of codes for getting notified on the progression of the operation.
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: How to get notifified when copy is complete?

10 May 2021, 03:47

I have one last question for everyone who can help:
with this code above, i can copy only a single file or a folder with its files but is it possible to copy multiple files on the same time (not a folder)? i look into the documentation but don't see informations about this case.
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: How to get notifified when copy is complete?

10 May 2021, 04:48

necomerx wrote:
10 May 2021, 03:47
I have one last question for everyone who can help:
with this code above, i can copy only a single file or a folder with its files but is it possible to copy multiple files on the same time (not a folder)? i look into the documentation but don't see informations about this case.
Sample use. | Disconnect the file list with the sign.

Code: Select all

Filelist := "c:\afile.exe|c:\bfile.exe|d:\file.exe"

ShellFileOperation( 0x2, Filelist , A_DeskTop )  
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: How to get notifified when copy is complete?

10 May 2021, 08:42

Thanks you very much hasantr, that's awesome !

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Joey5, mebelantikjaya and 334 guests