Page 1 of 1

multi file copy functions in single AHK

Posted: 26 Oct 2020, 16:44
by drewjbx1
I'm new to AHK and hoping to get some help...it would seem pretty simple what I am trying to do. I basically want to copy a couple of specific files to a backup folder... then copy all files/folders to the destination folder where I need it, overwrite is enabled. The first 2 lines of code I attempted this but it doesn't work.. its being ignored, do I need to add a pause or what am I missing.

Code: Select all

FileCopy, D:\Launchbox\Data\Playlists\Favorites.xml, D:\[ LB Data Backup ]\Data\Playlists\
FileCopy, D:\Launchbox\Data\Playlists\Family Favorites.xml, D:\[ LB Data Backup ]\Data\Playlists\

ErrorCount := CopyFilesAndFolders("D:\[ LB Data Backup ]\Data\*.*", "D:\Launchbox\Data")
if (ErrorCount != 0)
    MsgBox %ErrorCount% files/folders could not be copied.

CopyFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = true)
; Copies all files and folders matching SourcePattern into the folder named DestinationFolder and
; returns the number of files/folders that could not be copied.
{
    ; First copy all the files (but not the folders):
    FileCopy, %SourcePattern%, %DestinationFolder%, %DoOverwrite%
    ErrorCount := ErrorLevel
    ; Now copy all the folders:
    Loop, %SourcePattern%, 2  ; 2 means "retrieve folders only".
    {
        FileCopyDir, %A_LoopFileFullPath%, %DestinationFolder%\%A_LoopFileName%, %DoOverwrite%
        ErrorCount += ErrorLevel
        if ErrorLevel  ; Report each problem folder by name.
            MsgBox Could not copy %A_LoopFileFullPath% into %DestinationFolder%.
    }
    return ErrorCount
}

Re: multi file copy functions in single AHK

Posted: 26 Oct 2020, 16:49
by boiler
Try without the \ on the end of the destination path. The name of the destination folder does not include a \ on the end. Make sure the destination folder exists.

Re: multi file copy functions in single AHK

Posted: 26 Oct 2020, 16:56
by mikeyww
Since your first line failed, this should be easy to debug. You can try the following to break it down.

Code: Select all

source = D:\Launchbox\Data\Playlists\Favorites.xml
target = D:\[ LB Data Backup ]\Data\Playlists
MsgBox, 0, Info, % "Source: " FileExist(source) "`n`nTarget: " FileExist(target)
You might see something like the following.

Re: multi file copy functions in single AHK

Posted: 26 Oct 2020, 17:23
by drewjbx1
boiler wrote:
26 Oct 2020, 16:49
Try without the \ on the end of the destination path. The name of the destination folder does not include a \ on the end. Make sure the destination folder exists.
That didn't seem to work
mikeyww wrote: Since your first line failed, this should be easy to debug. You can try the following to break it down.

Code: Select all

source = D:\Launchbox\Data\Playlists\Favorites.xml
target = D:\[ LB Data Backup ]\Data\Playlists
MsgBox, 0, Info, % "Source: " FileExist(source) "`n`nTarget: " FileExist(target)
You might see something like the following.
Yes I do see the same dialogue box... what exactly does that mean?

Re: multi file copy functions in single AHK

Posted: 26 Oct 2020, 18:25
by Chunjee
One thing I did that worked well was used ahk to launch a CLI instance of RoboCopy (comes with Win 7, etc) https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

It was something like Run, %COMSPEC% /c Robocopy %fromDir%, %destinationDir%, %options%


It was a little more robust than ahk trying to copy-paste files. Since it was over a network connection it needed some retry options built in.

Re: multi file copy functions in single AHK

Posted: 26 Oct 2020, 18:35
by mikeyww
I use Robocopy routinely, too. It works well and quickly, and also has built-in options to update files while not overwriting newer ones. The console display can be handy as well. ComSpec might not always be necessary; Robocopy can be found at %A_WinDir%\System32\Robocopy.exe.