Function Reference


FileMove

Moves one or more files.

FileMove ( "source", "dest" [, flag = 0] )

Parameters

source The source path and filename of the file to move. (* wildcards accepted - See Remarks)
dest The destination path and filename of the moved file. (* wildcards accepted - See Remarks)
flag [optional] this flag determines whether to overwrite files if they already exist:
Can be a combination of the following:
    $FC_NOOVERWRITE (0) = (default) do not overwrite existing files.
    $FC_OVERWRITE (1) = overwrite existing files.
    $FC_CREATEPATH (8) = Create destination directory structure if it doesn't exist (See Remarks).

Constants are defined in FileConstants.au3.

Return Value

Success: 1.
Failure: 0 if source cannot be moved or if dest already exists and flag=0.

Remarks

See FileFindFirstFile() for a discussion about wildcards.

If the source and destination paths are on different volumes a copy and delete operation is performed rather than a move.

Because AutoIt lacks a "FileRename" function, use FileMove() to rename a file!

The destination directory must already exist, except using with flag value $FC_CREATEPATH (8).
For instance the combined flag $FC_OVERWRITE (1) + $FC_CREATEPATH (8) overwrites the target file and prechecks for the destination directory structure and if it doesn't exist creates it automatically.

Some file attributes can make the overwriting impossible, if this is the case look at FileSetAttrib() to change the attributes of a file.

Related

DirMove, FileCopy, FileDelete, FileRecycle

Example

Example 1

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir)

    ; Create a temporary file to move.
    If Not FileWrite($sFilePath, "This is an example of using FileMove.") Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf

    ; Move Au3 files in the temporary directory to a new folder/directory called Au3Files.
    FileMove(@TempDir & "\*.au3", @TempDir & "\Au3Files\", $FC_OVERWRITE + $FC_CREATEPATH)

    ; Display the temporary directory.
    ShellExecute(@TempDir)
EndFunc   ;==>Example

Example 2

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepaths that will be renamed.
    Local Const $sSource = _WinAPI_GetTempFileName(@TempDir), _
            $sDestination = _WinAPI_GetTempFileName(@TempDir)

    ; Create a temporary file to rename.
    If Not FileWrite($sSource, "This is an example of using FileMove.") Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf

    ; Rename a file using FileMove and overwrite the new file if it exists.
    FileMove($sSource, $sDestination, $FC_OVERWRITE)

    ; Display results that the destination file was renamed.
    MsgBox($MB_SYSTEMMODAL, "", "Does the source file exist?: " & FileExists($sSource) & @CRLF & _ ; FileExists should return 0.
            "Does destination file exist?: " & FileExists($sDestination) & @CRLF) ; FileExists should return 1.

    ; Delete the temporary files. FileDelete checks if the file exists.
    FileDelete($sSource)
    FileDelete($sDestination)
EndFunc   ;==>Example