Copying a large file - How do I track progress? - SOLVED Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SputnikDX
Posts: 9
Joined: 05 Nov 2015, 09:54

Copying a large file - How do I track progress? - SOLVED

05 Nov 2015, 10:07

Hey all, part of a script I'm writing is to copy a big file (about 1.2gb) to a local server. I've already tested the Copy process and of course it works and works easily, but the script just seems to hang up and look like nothing's happening for about 2-4 minutes while the file copies. I really don't like this, and was hoping someone could help me write a simple GUI script to at least just have something appear while the copy is in progress, and have it disappear when it finishes.

What I'm looking for: Copy big file. Know at a glance if it's still copying. Script continues when it's done. For any examples just use "source.zip" and "destination\source.zip". I've been working on over 500 lines for days so my brain don't good. :?

I'm 100% inexperienced with the GUI portion of AHK, so everything about it baffles me, and I think this would be a great place to start. So if you think GUI is my best option, I'd appreciate any help I can get simplifying it.

And I've already looked a lot at stuff linked on the old forums like ShellFileOperation(), but all of the examples do a ton of stuff that I don't need to do, and I can't decipher what needs to be removed and what needs to be changed to make my simple action work. If that's the path I should go down, I'd appreciate if someone can help me understand that function.

Thanks.
Last edited by SputnikDX on 05 Nov 2015, 14:19, edited 1 time in total.
Dylan87
Posts: 9
Joined: 03 Nov 2015, 11:17

Re: Copying a large file - How do I track progress?

05 Nov 2015, 10:28

Have you tried using the features in LoopFile? http://ahkscript.org/docs/commands/LoopFile.htm

You can pull the size of the file in KB with A_LoopFileSizeKB

I just posted a script a couple of hours ago that does something similar but looking at file create date. You could probably modify it pretty easy to pull the data you want.
SputnikDX
Posts: 9
Joined: 05 Nov 2015, 09:54

Re: Copying a large file - How do I track progress?

05 Nov 2015, 10:37

No, I haven't. This would actually be a little helpful since I have a big .zip and a tiny .txt to copy, but they both have the exact same filename.
So something like

Code: Select all

Loop, Files, \\filename.*
{
   totalSize := A_LoopFileSizeKB ;Is this how you do it? The help file is vague
   /*
   GUI of the copy using totalSize
   the actual fileCopy line
   */
}
Now my issue is making use of that size variable (the size of both files combined). Like I said, I have 0 experience with GUI and it seems like making a fileCopy line freezes the script on that line until it's finished.
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Copying a large file - How do I track progress?  Topic is solved

05 Nov 2015, 10:50

You might want to try this one:

Code: Select all

#NoEnv
FileToCopy := "Your\File's\Complete\Path"
TargetPath := "Path\Of\Your\Target\Folder"
MsgBox, % SHFileOperation([FileToCopy], TargetPath, 2) ; 2 = copy
ExitApp
; ==================================================================================================================================
; Copy or move files and folders using the SHFileOperation() function.
; SHFileOperation -> msdn.microsoft.com/en-us/library/bb762164(v=vs.85).aspx
; Parameters:
;     SourcesArray   -  Array of fully qualified source pathes.
;     TargetPath     -  Fully qualified path of the destination folder.
;     Operation      -  FO_MOVE = 1, FO_COPY = 2, other operations are not supported.
;     HWND           -  A handle to the window which will own the dialog.
;     Flags          -  Any combination of the FOF_ flags -> msdn.microsoft.com/en-us/library/bb759795(v=vs.85).aspx
;                       Default: FOF_NOCONFIRMMKDIR = 0x0200.
; Return values:
;     Returns 1 (True) if successful; otherwise 0 (False).
; ==================================================================================================================================
SHFileOperation(SourcesArray, TargetPath, Operation, HWND := 0, Flags := 0x0200) {
   Static TCS := A_IsUnicode ? 2 : 1 ; size of a TCHAR
   If Operation Not In 1,2
      Return False
   ; Count files and total string length
   TotalLength := 0
   Files := []
   For Each, FilePath In SourcesArray {
      If (Length := StrLen(FilePath))
         Files.Push({Path: FilePath, Len: Length + 1})
      TotalLength += Length
   }
   FileCount := Files.Length()
   If !(FileCount && TotalLength)
      Return
   ; Store the source pathes in Sources (the string must be double-null terminated)
   VarSetCapacity(Sources, (TotalLength + FileCount + 1) * TCS, 0)
   Offset := 0
   For Each, File In Files
      Offset += StrPut(File.Path, &Sources + Offset, File.Len) * TCS
   ; Store the target path in Target (the string must be double-null terminated)
   TargetLen := StrLen(TargetPath) + 2
   VarSetCapacity(Target, TargetLen * TCS, 0)
   Target := TargetPath
   ; Create and fill the SHFILEOPSTRUCT
   SHFOSLen := A_PtrSize * (A_PtrSize = 8 ? 7 : 8)
   VarSetCapacity(SHFOS, SHFOSLen, 0) ; SHFILEOPSTRUCT
   NumPut(HWND, SHFOS, 0, "UPtr") ; hwnd
   NumPut(Operation, SHFOS, A_PtrSize, "UInt") ; wFunc
   NumPut(&Sources, SHFOS, A_PtrSize * 2, "UPtr") ; pFrom
   NumPut(&Target, SHFOS, A_PtrSize * 3, "UPtr") ; pTo
   NumPut(Flags, SHFOS, A_PtrSize * 4, "UInt") ; fFlags
   If (A_IsUnicode)
      Return !DllCall("Shell32.dll\SHFileOperationW", "Ptr", &SHFOS, "Int")
   Else
      Return !DllCall("Shell32.dll\SHFileOperationA", "Ptr", &SHFOS, "Int")
}
If you have any further questions, ask.
SputnikDX
Posts: 9
Joined: 05 Nov 2015, 09:54

Re: Copying a large file - How do I track progress?

05 Nov 2015, 11:38

just me wrote:You might want to try this one:

Code: Select all

loads of code
If you have any further questions, ask.
This worked perfectly! Do I just need to pop that function anywhere into my script? It's weird that it worked considering the function being called is below the caller. I always thought code used very strict top-to-bottom logic.

I'll tinker with it for now though and get it working how I'd like it. The function will remain untouched though, that's way out of my element. Thanks again.
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Copying a large file - How do I track progress?

05 Nov 2015, 12:06

Copy the function to any place you want and call it wherever you want. It's important to enclose the first parameter in square brackets or to pass an array, because the function expects the first parameter to be an array.
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: Copying a large file - How do I track progress?

22 Nov 2017, 06:36

I know the solution is old - but...
Does this solution work on newer computers and OS? (I have not tested on Win7 or Win10 or...)

I like the solution, but is it hard to change the code, so only newer files are copied?
I do not want any questions before or after the copy is complete.
(Now the application may ask "Do you want to overwrite existing directory or file")
Only if the copying of the file is going wrong some error message is opened

Something like this .:

Code: Select all

If SHFileOperation([FileToCopy], TargetPath, 2) <> 1		; TargetPath, 1) = Move & TargetPath, 2) = copy
	MsgBox 64, Rad .: %A_LineNumber% -> %A_ScriptName%, Copy Error.....

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], gsxr1300, HiSoKa and 267 guests