使用wcstok_s函数分割路径, 创建多级目录

实现代码如下:
 
 
enum {RCODE_PASS = 0,RCODE_FAIL = 255};


int CreateMutilFolder(LPTSTR szFolder);

int JudgeFolderIsExist(LPTSTR szFolder)
{	
	if (!PathIsDirectory(szFolder))
	{
		CreateMutilFolder(szFolder);
	}

	return RCODE_PASS;
}

int CreateMutilFolder(LPTSTR szFolder)
{
	int m_ret = RCODE_PASS;
	size_t m_len = wcslen(szFolder);
	TCHAR* m_szFolder = new TCHAR[m_len+1];
	TCHAR* m_szTemp = new TCHAR[m_len+10];
	ZeroMemory(m_szFolder,m_len+1);
	ZeroMemory(m_szTemp,m_len+10);

	if(swprintf_s(m_szFolder,m_len+1,L"%s",szFolder) != m_len)
	{
		wprintf(L"[Error]: JudgeFolderIsExist-swprintf_s fail(errorcode == %ld)\n",GetLastError());
		m_ret = RCODE_FAIL;
		goto _func_exit_;

	}
	//wprintf(L"path: %s\n",m_szFolder);
	
	TCHAR *seps = L"/\\";
	TCHAR *token = NULL;
	TCHAR *next_token = NULL;
	token = wcstok_s(m_szFolder,seps,&next_token);
	while (NULL != token)
	{
	//	wprintf(L"%s\n",token);	
		if(wcscat_s(m_szTemp,m_len+10,token) == 0 && wcscat_s(m_szTemp,m_len+10,L"\\") == 0)
		{
			if (!PathIsDirectory(m_szTemp))
			{
				if (!CreateDirectory(m_szTemp,NULL))
				{
					wprintf(L"[Error]: CreateMutilFolder-CreateDirectory(%s) fail(errorcode == %ld)\n",m_szTemp,GetLastError());
					m_ret = RCODE_FAIL;
					goto _func_exit_;
				}
			}
		}
		else
		{
			wprintf(L"[Error]: CreateMutilFolder-wcscat_s fail(errorcode == %ld)\n",GetLastError());
			m_ret = RCODE_FAIL;
			goto _func_exit_;
		}

		token = wcstok_s(NULL,seps,&next_token);
	}

_func_exit_:
	if (NULL != m_szFolder)
	{
		delete m_szFolder;
		m_szFolder = NULL;
	}

	if (NULL != m_szTemp)
	{
		delete m_szTemp;
		m_szTemp = NULL;
	}

	return RCODE_PASS;
}






   

你可能感兴趣的:(vc++,创建多级目录)