How to Recursively Create a Folder in Win32

How to create a directory recursively in C++ that exceeds MAX_PATH?

You'll need to create each directory on the path individually. Parse the path into its directory components, and then work through them one by one. When you encounter a directory that does not exist, create it, and move on to the next level down.

The API function that you need to use here is CreateDirectory. Pass it the full path to the directory that you need to create. You'll need to use the Unicode version of the API, with the \\?\ prefix to disable the MAX_PATH length limit.

creating files, recursively creating directories

System.IO.Directory.CreateDirectory() will create all directories and subdirectories in a specified path, should they not already exist.

You can call it, passing the path, to ensure the folder structure is created prior to writing your file.

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

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.

Using recursion to get all files in some directory

First I wonder why you never use functions like wcscpy, wcscat or ... and always do all string and buffer manipulation by hand!? but beside that you have some problems in your code:

  • You use a single array of wide string pointers(allFlsArr) to return result of GetAllFiles to the caller and when you are iterating that result you will call GetAllFiles again that will overwrite allFlsArr.

  • You are freeing result of GetAllFiles using delete[] filePaths but that is actually allFlsArr that is a global variable and can't deleted. Try std::vector<std::wstring> that do not need delete/new.

  • Your array of file names is so small that can't accept the files in a folder that have more than 300 files or folders, Again try std::vector<std::wstring> that can grow in size and accept any number of items!

  • when you search a folder using *, Windows return to extra directory that you should never search them (., ..). You should just skip them (if(!wcscmp(file.cFileName, L".") || !wcscmp(file.cFileName, L"..")) continue)

  • You use CloseHandle to close the handle that returned from FindFirstFile while you should use FindClose.

So you may have something like this:

std::vector<std::wstring> list_files( wchar_t const* folder )
{
std::wstring root = folder;
if( folder.back() == '*' ) folder.erase( folder.end() - 1 );
if( folder.back() != '\\' ) folder.push_back( '\\' );

std::vector<std::wstring> res;
WIN32_FIND_DATA file;
HANDLE hSearch = FindFirstFileW( folder, &file );
if( hSearch != INVALID_HANDLE_VALUE ) {
do {
if( !wcscmp(file.cFileName, L".") ||
!wcscmp(file.cFileName, L"..") )
continue;
res.push_back( root + file.cFileName );
if( file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
// search for sub folders of this folder
std::vector<std::wstring> tmp =
list_files( (root + file.cFileName) + L"\\*" );
// all all items to end of our result!
res.insert( res.end(), tmp.begin(), tmp.end() );
}
} while( FindNextFileW(hSearch, &file) );
FindClose( hSearch );
}
return res;
}


Related Topics



Leave a reply



Submit