FileCopy Parameter "0" Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SorrymyEN
Posts: 35
Joined: 30 Apr 2017, 23:22

FileCopy Parameter "0"

Post by SorrymyEN » 17 Jan 2022, 08:57

Hi, I'm trying to use FileCopy to backup a file whenever I press a key.

According to the AHK documentation, the parameter "1" guides to overwrite the original file and "0" not to overwrite it. It happens that, in the case of "0", the expected action without overwriting the file would be to create a copy with the sequential numbering of the system. But "0" does nothing: no copy, no duplicate, no overwrite.

So I'd like to know what it's for or if I'm missing something here. Thanks

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: FileCopy Parameter "0"  Topic is solved

Post by mikeyww » 17 Jan 2022, 09:03

Sequential numbering is something that you would code. AutoHotkey does not do it for you automatically.

To see whether the file exists, you can use FileExist.

Checking the ErrorLevel is also useful.
ErrorLevel is set to the number of files that could not be copied due to an error, or 0 otherwise. In either case, if the source file is a single file (no wildcards) and it does not exist, ErrorLevel is set to 0. To detect this condition, use FileExist() or IfExist on the source file prior to copying it. Unlike FileMove, copying a file onto itself is always counted as an error, even if the overwrite mode is in effect. If files were found, A_LastError is set to 0 (zero) or the result of the operating system's GetLastError() function immediately after the last failure. Otherwise A_LastError contains an error code that might indicate why no files were found.

SorrymyEN
Posts: 35
Joined: 30 Apr 2017, 23:22

Re: FileCopy Parameter "0"

Post by SorrymyEN » 17 Jan 2022, 09:17

Does that mean the parameter "0" is like it doesn't even need to exist? Wouldn't it be more practical to duplicate files with system numbering? Or would there be other implications here?

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: FileCopy Parameter "0"

Post by mikeyww » 17 Jan 2022, 10:03

By default, FileCopy will not overwrite. What should happen if the file already exists is up to you and your script.

Code: Select all

dir       = %A_ScriptDir%
source    = %dir%\temp32.ahk
targetDir = %dir%\t
n         = 0
SplitPath, source,,, ext, fnBare
If !FileExist(source) {
 MsgBox, 48, Error, Source file not found. Aborting.`n`n%source%
 Return
} Else If !FileExist(targetDir) {
 FileCreateDir, %targetDir%
 If ErrorLevel {
  MsgBox, 48, Error, Target directory could not be created. Aborting.`n`n%targetDir%
  Return
 }
}
While FileExist(target := targetDir "\" fnBare suffix "." ext)
 suffix := "-" ++n
FileCopy, %source%, %target%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred while copying the file.Source: %source%`nTarget: %target%
Else MsgBox, 64, Success, Done!`n`n%target%

Post Reply

Return to “Ask for Help (v1)”