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 theFOF_NOCONFIRMMKDIR
flag infFlags
.
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 ofGetAllFiles
to the caller and when you are iterating that result you will callGetAllFiles
again that will overwriteallFlsArr
.You are freeing result of
GetAllFiles
usingdelete[] filePaths
but that is actuallyallFlsArr
that is a global variable and can't deleted. Trystd::vector<std::wstring>
that do not needdelete/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 fromFindFirstFile
while you should useFindClose
.
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
Does Std::Cout Have a Return Value
How to Load a Bmp on Glut to Use It as a Texture
Strange "Unsigned Long Long Int" Behaviour
How to Save Hicon to an .Ico File
What Happens When a Function That Returns an Object Ends Without a Return Statement
Traceback a Pointer in C++ Code Gdb
How to Change Text or Background Color in a Windows Console Application
Memory Alignment:How to Use Alignof/Alignas
Enumdisplaydevices VS Wmi Win32_Desktopmonitor, How to Detect Active Monitors
Best Bignum Library to Solve Project Euler Problems in C++
Specialization of 'Template<Class _Tp> Struct Std::Less' in Different Namespace
How to Load & Call a Vbscript Function from Within C++
Overload Resolution: Assignment of Empty Braces