Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ Copy file with automatic recursive folder/directory creation

In Win32 API, there is CopyFile that literally copies a file. However, this API doesn't create folders. For example, I'd like to copy C:\Data\output.txt to D:\Temp\Data\output.txt. But, the target folders, D:\Temp and D:\Temp\Data', do not exist. In this case, this API just fails.

Is there a handy API that can automatically and recursively create directory structure on copy? Definitely, I can make such function, but I expect someone already made the function.

like image 690
minjang Avatar asked Dec 21 '09 07:12

minjang


1 Answers

SHFileOperation should do the trick. From MSDN:

Copy and Move operations can specify destination directories that do not exist. In those cases, the system attempts to create them and normally displays a dialog box to ask the user if they want to create the new directory. To suppress this dialog box and have the directories created silently, set the FOF_NOCONFIRMMKDIR flag in fFlags.

like image 81
Aaron Klotz Avatar answered Sep 30 '22 05:09

Aaron Klotz