文件读写方法小记2

①遍历文件夹并且计算文件夹个数

    int iCount = 0;
    CString strTempPath;
    CString strFileName;
    strTempPath = CBApp::getCurrModulePath() + _T("ini");
    BOOL bWorking = finder.FindFile(strTempPath + _T("\\*.*")); 
    while (bWorking) 
    { 
        bWorking = finder.FindNextFile();
        if(!finder.IsDots())
        {
            iCount++;
        }
            strFileName = finder.GetFilePath();
    }

②删除非空的文件夹

bool CBSetting::DeleteDirectory(CString sPath)  
{  
    CFileFind Finder; 
    CString sDir;
    sDir.Format(_T("%s\\*.*"),sPath);
    BOOL IsFinded = Finder.FindFile(sDir);  
    while (IsFinded)  
    {  
        IsFinded = Finder.FindNextFile();
        if (!Finder.IsDots())  
        {  
            if (Finder.IsDirectory())  
                DeleteDirectory(Finder.GetFilePath());   
            else  
                DeleteFile(Finder.GetFilePath());  
        }  
    }  
    Finder.Close();  
    if(!RemoveDirectory(sPath))  
    {  
        return FALSE;  
    }  
    return TRUE;  
} 

③文件的删除和重命名

//删除预设文件
void CBSetting::deleteSettingFile(CString &sBoyeApp, CBObList &settingList, int iIndex)
{
    CFileFind Finder; 
    CString sName;
    sName.Format(_T("\\%d.ini"), iIndex);
    CString strPath = sBoyeApp + sName;
    if(Finder.FindFile(strPath))
    {
        DeleteFile(strPath);
    }
    else
        return;
    CString sOldName, sOldPath;
    for (POSITION pos = settingList.FindIndex(iIndex); pos != NULL; iIndex ++)
    {
        settingList.GetNext(pos);
        sOldName.Format(_T("\\%d.ini"), iIndex + 1);
        sOldPath = sBoyeApp + sOldName;
        if(Finder.FindFile(sOldPath)) 
            CFile::Rename(sOldPath, strPath); //重命名文件
        strPath = sOldPath;
    }
}

④文件夹的删除和重命名

//删除对应的设备保存文件
void CBSetting::deleteDeviceFile(int iIndex)
{
    CString sPath;
    CString sOldPath;
    CFileFind Finder; 
    sPath.Format(_T("ini\\%d"), iIndex);
    sPath = CBApp::getCurrModulePath() + sPath;
    if(Finder.FindFile(sPath))
    {
        DeleteFolder(sPath);
    }
    else
        return;
    
    for (POSITION pos = m_DeviceList.FindIndex(iIndex); pos != NULL; iIndex ++)
    {        
        m_DeviceList.GetNext(pos);
        sOldPath.Format(_T("ini\\%d"), iIndex + 1);
        sOldPath = CBApp::getCurrModulePath() + sOldPath;
        if(Finder.FindFile(sOldPath))
            ReNameFolder(sOldPath, sPath);    
        sPath = sOldPath;
    }
    Finder.Close();
}


//删除文件夹
void CBSetting::DeleteFolder(CString sOldPath) 
{ 
    int nLength = sOldPath.GetLength();
    TCHAR *pNewPath = new TCHAR[nLength+2];
    memcpy(pNewPath, sOldPath.GetBuffer(), nLength * sizeof(TCHAR));
    pNewPath[nLength] = '\0';
    pNewPath[nLength+1] = '\0';


    SHFILEOPSTRUCT FileOp; 
    ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));


    FileOp.fFlags = FOF_NOCONFIRMATION; 
    FileOp.hNameMappings = NULL; 
    FileOp.hwnd = NULL; 
    FileOp.lpszProgressTitle = NULL; 
    FileOp.pFrom = pNewPath; 
    FileOp.pTo = NULL; 
    FileOp.wFunc = FO_DELETE;   
    SHFileOperation(&FileOp);
    delete pNewPath;
}


//重命名文件夹
void CBSetting::ReNameFolder(CString sOldName,CString sNewName)
{
    int nLength = sOldName.GetLength();
    TCHAR *pOldPathName = new TCHAR[nLength + 2];
    memcpy(pOldPathName, sOldName.GetBuffer(), nLength * sizeof(TCHAR));
    pOldPathName[nLength] = '\0';
    pOldPathName[nLength + 1] = '\0';


    SHFILEOPSTRUCT FileOp; 
    ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));


    FileOp.fFlags = FOF_NOCONFIRMATION ; 
    FileOp.hNameMappings = NULL; 
    FileOp.hwnd = NULL; 
    FileOp.lpszProgressTitle = NULL; 
    FileOp.pFrom = pOldPathName; 
    FileOp.pTo = sNewName; 
    FileOp.wFunc = FO_RENAME; 
    
    SHFileOperation(&FileOp);
    delete pOldPathName;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值