win-filePath

分割路径

_splitpath_s

BOOL SetCurrentPathToModulePath(HMODULE hModule)
{
TCHAR szPath[MAX_PATH];
if(::GetModuleFileName(hModule, szPath, MAX_PATH))
{
TCHAR drive[MAX_PATH], dir[MAX_PATH], fname[MAX_PATH], ext[MAX_PATH];
_tsplitpath_s(szPath,drive, MAX_PATH,dir, MAX_PATH,fname, MAX_PATH,ext, MAX_PATH);
lstrcpy(szPath, drive);
lstrcat(szPath, dir);

return ::SetCurrentDirectory(szPath);
}

return FALSE;
}

路径是否存在

if (FALSE == PathFileExists( (m_configPath + _T("logs")).c_str))
{
// directory not exist
DWORD dwErrCode = GetLastError();
}

crate folder

//create a path with title name
if ( FALSE == CreateDirectory(tmpSrcPath,NULL) )
{
AfxMessageBox(TEXT("Create directory failed"));
return;
}

文件相关处理,copy,move,delete

CopyFile(),DeleteFile(),MoveFile()


#define OP_RENAME 0
#define OP_COPY 1
#define OP_DELETE 2
#define OP_MOVE 3
/*
rename file or directory
@param iType [in] 0 rename, 1 copy,2 delete,3 move
0 success
not zero failed.
*/
int filePathProc(const wchar_t *pFrom, const wchar_t *pTo, int iType)
{
SHFILEOPSTRUCT FileOp = { 0 };
wchar_t src[MAX_PATH] = { '\0' };
wchar_t dst[MAX_PATH] = { '\0' };
if ( NULL == pTo )
{
FileOp.pTo = NULL;
} else
{
_tcscpy(dst, pTo);
FileOp.pTo = dst;
}
if ( NULL == pFrom )
{
return 1;
}
_tcscpy(src, pFrom);

switch (iType)
{
case 0:
FileOp.wFunc = FO_RENAME;
break;
case 1:
FileOp.wFunc = FO_COPY;
break;
case 2:
FileOp.wFunc = FO_DELETE;
FileOp.pTo = NULL;
break;
case 3:
FileOp.wFunc = FO_MOVE;
default:
break;
}
FileOp.fFlags = FOF_NOCONFIRMATION | FOF_ALLOWUNDO | FOF_NOCONFIRMMKDIR; //不出现确认对话框
FileOp.pFrom = src;

return SHFileOperation(&FileOp);
}

int bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("456")).c_str(), OP_RENAME);
_ASSERT(false == bRet);

string strUtf8;
UnicodeConverter::toUTF8(utf16Path + TEXT("456"), strUtf8);
File f1( strUtf8 );
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("456")).c_str(), (utf16Path + TEXT("123")).c_str(), OP_RENAME);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1234")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1234")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_MOVE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233\\1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1233")).c_str(), NULL, OP_DELETE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());