您的位置:首页 > 其它

查找程序所在上一级目录中是否存在指定文件夹,不存在就建立,并在其中新建一个txt文件

2012-10-22 09:36 826 查看
WIN32_FIND_DATA fileFind;
HANDLE hFirstFile;
CString strFolder = "TargetFolder";
CString strCurrentDir,strForeDir;
int flagFind = 0;
CString strdir;
char buf[MAX_PATH];
int nIndex;

memset(buf,0,MAX_PATH);
GetCurrentDirectory(MAX_PATH,buf);
strCurrentDir.Format(_T("%s"),buf);
strForeDir = strCurrentDir;
nIndex = strForeDir.ReverseFind('\\');
strForeDir.Delete(nIndex+1,strForeDir.GetLength()-nIndex-1);
strdir = strForeDir + _T("*");
hFirstFile = FindFirstFile(strdir,&fileFind);

do {
CString strText;
strText.Format(_T("%s"),fileFind.cFileName);
if(strText == strFolder)
{
flagFind = 1;
AfxMessageBox(strForeDir + strFolder + ":The folder is exist.");
}
} while(FindNextFile(hFirstFile,&fileFind));

if(flagFind == 0)
{
if(!CreateDirectory(strForeDir + strFolder,NULL))
{
AfxMessageBox("Create folder error!");
return;
}
SetCurrentDirectory(strForeDir + strFolder);
CFile file;
char str[] = "if you want show someting in the program you should add some txt file in to current folder ";
file.Open("readme.txt",CFile::modeCreate | CFile::modeWrite,NULL);
file.Write(str,strlen(str));
file.Close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐