Click here to Skip to main content
16,016,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How can I create a directory with unicode format?
Something like this:
C++
/*
The exists directory path is: "C:\A\B\"
I would create the folderName path ("C:\A\B\C\D\") if not exists.
*/
wchar_t* folderName = L"C:\A\B\C\D\";
bool result = createFolder(folderName);

...

bool createFolder(wchar_t* _folderName)
{
//Return TRUE, if already exists or create is successful
}


Thx,
Bejglee
Posted

Hi,
please refer to the blow code.


C++
#include "stdafx.h"
#include <windows.h>


void CreateDirectoryWithChecking(const wchar_t* i_LPSTR_FolderPath)
{
	::WIN32_FIND_DATA data;
	::HANDLE handle = ::FindFirstFile(i_LPSTR_FolderPath, &data);
	if (handle == INVALID_HANDLE_VALUE)
		::CreateDirectory(i_LPSTR_FolderPath, NULL);
	else
		FindClose(handle);
}


bool createFolderStrecture(const wchar_t* _folderName)
{
	if (!_folderName)return false;

	size_t len = 0;
	while (_folderName[len] != '\0')++len;
	

	for (size_t index = 0; index < len; index++)
	{
		if (_folderName[index] =='\\' && index>3)
		{
			wchar_t curFolderPath[_MAX_PATH] = { 0 };
			::memcpy_s(curFolderPath, _MAX_PATH*sizeof(wchar_t), _folderName, (index)*sizeof(wchar_t));
			CreateDirectoryWithChecking(curFolderPath);
		}
	}

	return true;	
}

int _tmain(int argc, _TCHAR* argv[])
{
	wchar_t* folderName = L"F:\\A\\B\\C\\D\\";
	bool result = createFolderStrecture(folderName);

	return 0;
}
 
Share this answer
 
v4
Thanks nvtkrishna!

It is great!
I have made some changes (see bold text), because my builder do not compiled it.
But the point is Yours!
You think this is okay?

Regards,
Bejglee

C++
void CreateDirectoryWithChecking(const wchar_t* i_LPCWSTR_FolderPath)
{
	::WIN32_FIND_DATAW data;
	::HANDLE handle = ::FindFirstFileW(i_LPCWSTR_FolderPath, &data);
	if (handle == INVALID_HANDLE_VALUE)
		::CreateDirectoryW(i_LPCWSTR_FolderPath, NULL);
	else
		FindClose(handle);
}
 
bool createFolderStrecture(const wchar_t* _folderName)
{
	if (!_folderName)return false;
 
	size_t len = 0;
	while (_folderName[len] != '\0') ++len;
	
	for (size_t index = 0; index < len; index++)
	{
		if (_folderName[index] == L'\\' && index > 3)
		{
			wchar_t curFolderPath[_MAX_PATH];
                        SecureZeroMemory(curFolderPath, _MAX_PATH * sizeof(wchar_t));
			::wmemcpy_s(curFolderPath, _MAX_PATH*sizeof(wchar_t), _folderName, (index)*sizeof(wchar_t));
			CreateDirectoryWithChecking(curFolderPath);
		}
	}
 
	return true;	
}
 
Share this answer
 
Comments
nvtkrishna 15-Apr-14 8:27am    
OK, may be your builder is using "Use Multi-Byte Character Set", if u change it to "Use Unicode Character Set" it will work fine.
Laurie Stearn 10-Oct-15 0:23am    
My attempt at a C version of this method failed. There was a solution, however.
http://stackoverflow.com/questions/33018732/could-not-find-path-specified-createdirectoryw/33050214#33050214
@nvtkrishna: Have you tested this for very long path strings? Once created, these paths may not be easy to remove.
llllskywalker 23-Feb-16 19:00pm    
Those solutions will work if you input folder path is ASCII. They will fail if your GUI used a unicode-compatible edit box and let the user choose a path. The user might enter characters beyond the typical ASCII set. For example: "C:\A\Ŝ\C\D\" would crash since Ŝ is 015c, which contains 5c (the backward slash)! To avoid this, you need to use a library like "UTF-8 CPP", "ICU", etc to skip to the next forward-slash instead of doing it byte-by-byte.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900