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.

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[])
{
wprintf(L\n);
// Verify
if( argc != 3 )
{
wprintf(LError: Incorrect number of argument!\n);
wprintf(LDescription: Moves a directory and its contents\n);
wprintf(LUsage: %s [source_dir] [target_dir]\n, argv[0]);
wprintf(LExample: %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))
{
DisplayErrorBox(LMoveFileEx());
return 1;
}
else
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();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(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, LError, MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
Build and run the project. The following screenshots are sample outputs.

