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
FileCopy Parameter "0" Topic is solved
Re: FileCopy Parameter "0" Topic is solved
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.
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.
Re: FileCopy Parameter "0"
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?
Re: FileCopy Parameter "0"
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%