Windows Directory Management 5






Moving Directories Program Example


To move a directory to another location, along with the files and subdirectories contained within it, call the MoveFileEx(), MoveFileWithProgress(), or MoveFileTransacted() function. The MoveFileWithProgress() function has the same functionality as MoveFileEx(), except that MoveFileWithProgress() enables you to specify a callback routine that receives notifications on the progress of the operation. The MoveFileTransacted() function enables you to perform the operation as a transacted operation. The following example demonstrates the use of the MoveFileEx() function with a directory.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.


Moving Directories Program Example: Creating new Win32 C++ console application project in Visual C++ .NET


Then, add the source file and give it a suitable name.

Next, add the following source code.


#include <windows.h>

#include <stdio.h>

// Safer versions

#include <strsafe.h>


// A prototype that receives a function name, displaying

// system error code and its respective message

void DisplayErrorBox(LPTSTR lpszFunction);


int wmain(int argc, WCHAR *argv[])



    // Verify

    if( argc != 3 )


        wprintf(L"Error: Incorrect number of argument!\n");

        wprintf(L"Description: Moves a directory and its contents\n");

        wprintf(L"Usage: %s [source_dir] [target_dir]\n", argv[0]);

        wprintf(L"Example: %s C:\\testdir C:\\newtestdir\n", argv[0]);

        wprintf(L"       The target directory must not exist.\n\n");

        return 1;



    // 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))



        return 1;



        wprintf(L"%s has been moved to %s\n", argv[1], argv[2]);

      return 0;



void DisplayErrorBox(LPTSTR lpszFunction)


    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;

    LPVOID lpDisplayBuf;

    DWORD dw = GetLastError();









        (LPTSTR) &lpMsgBuf,

        0, NULL );


    // Display the error message and clean up

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(WCHAR));

    StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(WCHAR),  L"%s failed with error %d: %s", lpszFunction, dw, lpMsgBuf);

    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK);






Build and run the project. The following screenshots are sample outputs.


Moving Directories Program Example: A sample console program output in action 1



Moving Directories Program Example: A sample console program output in action 2. File already exist





< Windows Directory 4 | Win32 Programming | Win32 Directory Index | Windows Directory 6 >