FileCopy doesn't work as expected Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

FileCopy doesn't work as expected

04 Jun 2019, 13:31

The example for FileCopy doesn't work as expected.

Link to original code: https://www.autohotkey.com/docs/commands/FileCopy.htm

The version with simplified directory names (Foo and Bar):

Code: Select all

; The following example copies all files and folders inside a folder to a different folder:
ErrorCount := CopyFilesAndFolders("C:\Foo\*.*", "C:\Bar")
if (ErrorCount != 0)
    MsgBox %ErrorCount% files/folders could not be copied.

CopyFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = false)
; 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
}
Test directory:

Code: Select all

Foo\
|-- Folder 1\
|-- Folder 2\
|-- File 1.txt
|-- File 2.txt
Expected behavior: the script should copy everything from Foo to Bar.

Instead, only the directories are copied. To copy the files you will need to run this script twice.

AHK version 1.1.30.03. Is there some error/bug in this example?
Last edited by john_c on 04 Jun 2019, 16:36, edited 1 time in total.
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: FileCopy doesn't work as expected

04 Jun 2019, 17:55

(I haven't look on your program sorry!)
Maybe this solution will work for you? Copying a large file - How do I track progress?
Another link! Copy files fast -> AHK FileCopy - DOS XCopy
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: FileCopy doesn't work as expected

04 Jun 2019, 18:00

@Albireo It's not some "program", it is just a copy-paste from docs. But for some reason this copy-paste doesn't work. I cannot understand is it a bug, an error in the docs or something different.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: FileCopy doesn't work as expected

04 Jun 2019, 18:14

@john_c: I tried the script, and it works. sorry, this is not helpful, but at least it is the information you asked for.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: FileCopy doesn't work as expected

04 Jun 2019, 18:24

@wolf_II

The problem with the example posted in the docs is that _on my computer_ it tries to use FileCopy before the new directory is created - and, of course, it fails.

If you move FileCopy after the loop - everything works.

But still, it is strange that:

1) The posted version doesn't properly work for me.

2) It works for you.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: FileCopy doesn't work as expected  Topic is solved

04 Jun 2019, 18:36

@john_c: I tried with an pre-existing destination Folder.

There is no hint, that the script would compensate for a missing destination folder.
; Copies all files and folders matching SourcePattern into the folder named DestinationFolder and
; returns the number of files/folders that could not be copied.
This sounds to me like a requirement.
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: FileCopy doesn't work as expected

04 Jun 2019, 18:42

You have right!
It's something with the loop command. look at .: LoopFile

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, Chunjee, WAZAAAAA and 412 guests