FileMove()

Namespace ›› System ›› Mfunc ›› Methods ››
Parent Previous Next

FileMove()

Mfunc.FileMove(DestPattern [, Flag])

Mfunc.FileMove(DestPattern [, Flag])

Moves or renames one or more files.

Parameters

SourcePattern

The name of a single file or folder, or a wildcard pattern such as C:\Temp*.tmp. SourcePattern is assumed to be in %A_WorkingDir% if an absolute path isn't specified.

DestPattern

The name or pattern of the destination, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified. To perform a simple copy -- retaining the existing file name(s) -- specify only the folder name as shown in these functionally identical examples:

Mfunc.FileMove("C:\*.txt", "C:\My Folder")
Mfunc.FileMove("C:\*.txt", "C:\My Folder\*.*")

Flag

(optional) this flag determines whether to overwrite files if they already exist:
0 = (default) do not overwrite existing files
1 = overwrite existing files
This parameter can be an expression, even one that evaluates to true or false (since true and false are stored internally as 1 and 0).

Throws

Throws MfException on error if A_LastError is non-zero then InnerException message is set to the value of A_LastError.

Remarks

Wrapper for AutoHotkey Docs - FileMove.
Static method.

Unlike Mfunc.FileCopy, moving a file onto itself is always considered successful, even if the overwrite mode is not in effect.

FileMove moves files only. To instead move the contents of a folder (all its files and subfolders), see the examples section below. To move or rename a single folder, use Mfunc.FileMoveDir.

The operation will continue even if error(s) are encountered.

Although this command is capable of moving files to a different volume, the operation will take longer than a same-volume move. This is because a same-volume move is similar to a rename, and therefore much faster.

Any and/or all parameter for this function can be instance of MfString or var containing string.

Also See:AutoHotkey Docs - FileMove.

Example

Mfunc.FileMove("C:\My Documents\List1.txt", "D:\Main Backup\")  ; Move the file without renaming it.
Mfunc.FileMove("C:\File Before.txt", "C:\File After.txt")  ; Rename a single file.
Mfunc.FileMove("C:\Folder1\*.txt", "D:\New Folder\*.bkp")  ; Move and rename files to a new extension.

; The following example moves all files and folders inside a folder to a different folder:
ErrorCount := MoveFilesAndFolders("C:\My Folder\*.*", "D:\Folder to receive all files & folders")
if ErrorCount <> 0
    MsgBox %ErrorCount% files/folders could not be moved.

MoveFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = false)
; Moves all files and folders matching SourcePattern into the folder named DestinationFolder and
; returns the number of files/folders that could not be moved. This function requires v1.0.38+
; because it uses FileMoveDir's mode 2.
{
	ErrorCount := 0
    if DoOverwrite = 1
        DoOverwrite = 2  ; See FileMoveDir for description of mode 2 vs. 1.
    ; First move all the files (but not the folders):
	try {
		Mfunc.FileMove(SourcePattern, DestinationFolder, DoOverwrite)
	} catch e {
		ErrorCount := ErrorLevel
	}
    
    
    ; Now move all the folders:
    Loop, %SourcePattern%, 2  ; 2 means "retrieve folders only".
    {
		try {
			Mfunc.FileMoveDir(A_LoopFileFullPath, DestinationFolder . "\" . A_LoopFileName, DoOverwrite)
		} catch e {
			ErrorCount += ErrorLevel
		}
        if ErrorLevel  ; Report each problem folder by name.
            MsgBox Could not move %A_LoopFileFullPath% into %DestinationFolder%.
    }
    return ErrorCount
}