海阔天空

海阔凭鱼跃 天高任鸟飞

 

文件目录常用操作积累

代码

// 获取目录大小
void CDlgLocalMain::GetSpecialDirSize(const CString& sPathDir, CString& sSize)
{
    m_ulDirSize 
= 0;
    RecurseComputeDirSize(sPathDir);
    sSize.Format(_T(
"%I64i"), m_ulDirSize);
}


// 获取目录大小的内层递归
void CDlgLocalMain::RecurseComputeDirSize(const CString& sPathDir)
{
    TCHAR szPathDir[
256];
    memset(szPathDir, 
0256*sizeof(TCHAR) );

    CString sTmpPathDir 
= sPathDir;
    sTmpPathDir 
= sTmpPathDir + _T("\\*.*");
    strcpy_s(szPathDir, 
255, sTmpPathDir.GetBuffer( sTmpPathDir.GetLength()) );


    WIN32_FIND_DATAA FindFileData;
    HANDLE hFind;

    hFind 
= FindFirstFile(szPathDir, &FindFileData); 
    
if( hFind == INVALID_HANDLE_VALUE)
    {
        
// 没有找到
        return;
    }
    
else
    {
        
while(1)
        {
            CString sTmp;
            
if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                TCHAR szFileName[
256];
                memset(szFileName, 
0256*sizeof(TCHAR) );
                strcpy_s(szFileName, 
255, FindFileData.cFileName);

                
if0 == strcmp(szFileName, _T(".") ) || 0 == strcmp(szFileName, _T("..") ) )
                {}
                
else
                {
                    
// 目录,继续递归
                    sTmp = sPathDir;
                    sTmp 
= sTmp + _T("\\"+ FindFileData.cFileName;
                    RecurseComputeDirSize(sTmp);
                }
            }
            
else
            {
                TCHAR szFileName[
256];
                memset(szFileName, 
0256*sizeof(TCHAR) );
                strcpy_s(szFileName, 
255, FindFileData.cFileName);

                
if0 == strcmp(szFileName, _T(".")) || 0 == strcmp(szFileName, _T("..")) )
                {
                }
                
else
                {
                    DWORD dwFileSizeHigh 
= FindFileData.nFileSizeHigh;
                    DWORD dwFileSizeLow  
= FindFileData.nFileSizeLow;

                    ULONGLONG ulFileSize 
= ( dwFileSizeHigh*MAXDWORD ) + dwFileSizeLow;
                    m_ulDirSize 
= m_ulDirSize + ulFileSize;
                }
            }

            BOOL bFlag 
= FindNextFile(hFind, &FindFileData);
            
if0 == bFlag )
            {
                
break;
            }
        }
        FindClose(hFind);
    } 

    
return ;
}

 // 向指定文件中追加写数据

// sPathFile    输入的全路径文件名称
// sContent     写入的内容,如果需要换行,写入的内容中需要追加"\r\n"
void WriteFile( CString& sPathFile, CString& sContent)
{
    sContent.Trim();
    
if( sPathFile.IsEmpty() || sContent.IsEmpty() || _T(""== sContent )
    {
        
return;
    }

    CString sTmpPathFile 
= sPathFile;
    CString sTmpContent 
= sContent;


    FILE 
*fp = NULL;
    CString sMode 
= _T("a");
    
if!fopen_s(&fp, sTmpPathFile.GetBuffer( sTmpPathFile.GetLength() ), sMode.GetBuffer( sMode.GetLength() ) ) )
    {
        fprintf( fp, _T(
"%s"), sTmpContent.GetBuffer(sTmpContent.GetLength()) );
        fclose(fp);
        fp 
= NULL;
    }
}


// 从目标文件中逐行的获取内容
void ReadFile( CString& sPathFile )
{
    FILE 
*fp = NULL;
    CString sTmpPathFile 
= sPathFile;
    
if!fopen_s(&fp, sTmpPathFile.GetBuffer( sTmpPathFile.GetLength() ), _T("r") ) )
    {
        
while!feof( fp ) )
        {
            TCHAR szlinecontent[
1024];
            memset(szlinecontent, 
0sizeof(szlinecontent) );

            fgets(szlinecontent, 
sizeof(szlinecontent), fp);
            CString sTmpline 
= szlinecontent;

            sTmpline.Trim();
            
if!sTmpline.IsEmpty() )
            {
                CString sJpgPathFile 
= sTmpline;

                
if1 == CheckFileExit(sJpgPathFile) )
                {
                    
// 文件存在,显示文件
                }
            }
        }
        fclose(fp);
    }
}


// 检查文件或者目录是否存在
// 0    不存在
// 1    存在
int CheckFileExit(const CString& sPathFile)
{
    
int iFileExitFlag = 1;
    CFileStatus status;
    
if( CFile::GetStatus( sPathFile, status) )
    {
    }
    
else
    {
        
if(sPathFile.GetLength() > 3 )
        {
            iFileExitFlag 
= 0;
        }
    }

    
return iFileExitFlag;
}


// 递归制定目录中的所有JPG图片
void RecurseComputeDirSize(const CString& sPathDir)
{
    TCHAR szPathDir[
256];
    memset(szPathDir, 
0256*sizeof(TCHAR) );

    CString sTmpPathDir 
= sPathDir;
    sTmpPathDir 
= sTmpPathDir + _T("\\*.*");
    strcpy_s(szPathDir, 
255, sTmpPathDir.GetBuffer( sTmpPathDir.GetLength()) );


    WIN32_FIND_DATAA FindFileData;
    HANDLE hFind;

    hFind 
= FindFirstFile(szPathDir, &FindFileData);
    
if( hFind == INVALID_HANDLE_VALUE)
    {
        
// 没有找到
        return;
    }
    
else
    {
        
while(1)
        {
            CString sTmp;
            
if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                TCHAR szFileName[
256];
                memset(szFileName, 
0256*sizeof(TCHAR) );
                strcpy_s(szFileName, 
255, FindFileData.cFileName);

                
if0 == strcmp(szFileName, _T(".") ) || 0 == strcmp(szFileName, _T("..") ) )
                {}
                
else
                {
                    
// 目录,继续递归
                    sTmp = sPathDir;
                    sTmp 
= sTmp + _T("\\"+ FindFileData.cFileName;
                    RecurseComputeDirSize(sTmp);
                }
            }
            
else
            {
                TCHAR szFileName[
256];
                memset(szFileName, 
0256*sizeof(TCHAR) );
                strcpy_s(szFileName, 
255, FindFileData.cFileName);

                
if0 == strcmp(szFileName, _T(".")) || 0 == strcmp(szFileName, _T("..")) )
                {
                }
                
else
                {
                    
/*
                    // 需要修改的地方
                    // 获取全路径
                    CString sContent = szPathDir + _T("\\") + szFileName + _T("\r\n");
                    JpgWriteFile( sPathFile, sContent);
                    
*/
                }
            }

            BOOL bFlag 
= FindNextFile(hFind, &FindFileData);

            
if0 == bFlag )
            {
                
break;
            }
        }
        FindClose(hFind);
    }

    
return ;
}

 

posted on 2010-05-06 14:22  liuym  阅读(309)  评论(0编辑  收藏  举报

导航