ファイルをコピーする
FileCopy, SourcePattern, DestPattern [, Flag]
| 引数名 | 説明 |
|---|---|
| SourcePattern |
ファイル名もしくはワイルドカード。 相対パスで指定した場合は、%A_WorkingDir%を基準としたパスとなる。 |
| DestPattern | コピー後のファイル名もしくはワイルドカード。 相対パスで指定した場合は、%A_WorkingDir%を基準としたパスとなる。 |
| Flag | 「1」を指定すると、同名のファイルが存在したときに上書きを行う。 省略時や「0」を指定した場合は、同名のファイルが存在したときはコピーしない。 |
コピーに失敗したファイルの数になる
コピー中にエラーが発生しても、処理は続行される
ファイルをそのファイル自身に上書きコピーしようとした場合、エラーとみなされる。
サブフォルダを含めフォルダ内のファイルをすべてコピーするには、FileCopyDirを使用する。
FileMove, FileCopyDir, FileMoveDir, FileDelete
FileCopy, C:\My Documents\List1.txt, D:\Main Backup\ ; Make a copy but keep the orig. file name. FileCopy, C:\My File.txt, C:\My File New.txt ; Copy a file into the same folder by providing a new name. FileCopy, C:\Folder1\*.txt, D:\New Folder\*.bkp ; Copy to new location and give new extension.
; The following example copies all files and folders inside a folder to a different folder:
ErrorCount := CopyFilesAndFolders("C:\My Folder\*.*", "D:\Folder to receive all files & folders")
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
}