移动目录

若要将目录以及其中包含的文件和子目录移到另一个位置,请调用 MoveFileExMoveFileWithProgressMoveFileTransacted 函数。 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]);
}