移動目錄
若要將目錄移至另一個位置,以及其中包含的檔案和子目錄,請呼叫 MoveFileEx、 MoveFileWithProgress或 MoveFileTransacted 函式 。 MoveFileWithProgress函式的功能與MoveFileEx相同,不同之處在于MoveFileWithProgress可讓您指定回呼常式,以接收作業進度的通知。 MoveFileTransacted 函式可讓您以交易作業的形式執行作業。
下列範例示範如何搭配目錄使用 MoveFileEx 函式。
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int __cdecl _tmain(int argc, TCHAR *argv[])
{
printf("\n");
if( argc != 3 )
{
printf("ERROR: Incorrect number of arguments\n\n");
printf("Description:\n");
printf(" Moves a directory and its contents\n\n");
printf("Usage:\n");
_tprintf(TEXT(" %s [source_dir] [target_dir]\n\n"), argv[0]);
printf(" The target directory cannot exist already.\n\n");
return;
}
// Move the source directory to the target directory location.
// The target directory must be on the same drive as the source.
// The target directory cannot already exist.
if (!MoveFileEx(argv[1], argv[2], MOVEFILE_WRITE_THROUGH))
{
printf ("MoveFileEx failed with error %d\n", GetLastError());
return;
}
else _tprintf(TEXT("%s has been moved to %s\n"), argv[1], argv[2]);
}