我想将一个文件夹内的所有.txt文件名都放到一个文本里。我提问的时候看了下相关问题,都是C#或VB的。具体方法能不能介绍一下。谢谢!

解决方案 »

  1.   

    就是遍历文件夹内的文件?用API就可以。FindFirstFile, FindNextFile。
    或者你想有一个直接完成的方法?命令行也可以用CreateProcess运行下列命令
    dir *.txt > result.txt
      

  2.   

    HANDLE  hFile = NULL; 
    WIN32_FIND_DATA  fd = {0}; 
    _tcscpy(tsTemp, strFolder); 
    _tcscat(tsTemp, _T("*.*")); 
    hFile = ::FindFirstFile(tsTemp, &fd); 
    if(hFile != INVALID_HANDLE_VALUE) 

    do 

    if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 
    if(_tcscmp(fd.cFileName, _T(".")) == 0 || _tcscmp(fd.cFileName, _T("..")) == 0) 

    continue; 

    ... 
                    ... 

    while(::FindNextFile(hFile,&fd)); 
    ::FindClose(hFile); 这段代码可以遍历strFolder目录下的所有文件 
      

  3.   

    find(char * lpPath)
    {
        char szFind[MAX_PATH];
        WIN32_FIND_DATA FindFileData;    strcpy(szFind,lpPath);
        strcat(szFind,"\\*.*");    HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
        if(INVALID_HANDLE_VALUE == hFind)    return;
        
        while(TRUE)
        {
            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if(FindFileData.cFileName[0]!='.')
                {
                    strcpy(szFile,lpPath);
                    strcat(szFile,"\\");
                    strcat(szFile,FindFileData.cFileName);
                    find(szFile);
                }
            }
            else
            {
                cout << FindFileData.cFileName;
            }
            if(!FindNextFile(hFind,&FindFileData))    break;
        }
        FindClose(hFind);
    }
    文章出处:DIY部落(http://www.diybl.com/course/3_program/vc/vc_js/20081010/149673.html)
      

  4.   

    一般使用findfile就可以了。
      

  5.   

    我只会在该文件夹内查找.txt文件,然后写文本
    不知道是否有其它方法,
    看楼下
      

  6.   

    void GlobalEnumFile( CTreeCtrl *treectrl, CString str_path, HTREEITEM tree, int deep )
    {
    try
    {
    CFileFind finder;
    char ttt[ 1024 ] = "";
    HTREEITEM tmp;
    CString strWildcard( str_path ); if( deep >= 2 )
    {
    return;
    }
    //CTreeCtrl *t = ( CTreeCtrl * )( this->GetDlgItem( IDC_TREE1 ) );
    strWildcard += _T( "\\*.*" );
    TRACE( strWildcard );
    TRACE( g____crlf ); // start working for files
    BOOL bWorking = finder.FindFile( strWildcard ); while ( bWorking )
    {
    bWorking = finder.FindNextFile();
    if ( finder.IsDots() )
    continue;
    if( NULL == tree )
    {
    tmp = treectrl->InsertItem( finder.GetFilePath() );
    }
    else
    {
    memset( ttt, 0, sizeof( ttt ) );
    strcat( ttt, finder.GetFileName() ); //文件名+扩展名
    //strcat( ttt, finder.GetFileTitle() ); //文件名
    strcat( ttt, "                " ); //16个
    strcat( ttt, "                " ); //16个
    strcat( ttt, "                " ); //16个
    strcat( ttt, "                " ); //16个
    strcat( ttt, "                " ); //16个
    strcat( ttt, "                " ); //16个
    ttt[ SPLITSIZE ] = 0;
    strcat( ttt, "(" );
    strcat( ttt, finder.GetFilePath() );
    strcat( ttt, ")" );
    tmp = treectrl->InsertItem( ttt, tree );
    }
    if ( finder.IsDirectory() )
    {
    GlobalEnumFile( treectrl, finder.GetFilePath(), tmp, deep + 1 );
    }
    } finder.Close();
    }
    catch( ... )
    {
    throw;
    }
    SortTree( treectrl, treectrl->GetRootItem() );
    }一个参考。
      

  7.   

    看来是用CFileFind比较简单些,
    请问是怎么给他制定文件的路径的,直接看代码我还是看不清楚,水平有限。
      

  8.   


    哦 明白了 是在finder.FindFile( )里面设置文件路径