遍历目录,_findfirst,_findnext,复制文件

解决方案 »

  1.   

    #ifndef FILE_SEARCH
    #define FILE_SEARCH#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <direct.h>
    #include <io.h>#include "StackPP.h"typedef _finddata_t FINDDATA;
    typedef _finddata_t* LPFINDDATA;
    typedef StackTempl < _finddata_t* > FindDataStack;class FileSearch
    {protected:
    char* m_cPath;protected:
    ~FileSearch()
    {
    delete []m_cPath;
    m_cPath = NULL;
    }
    virtual void ProcessFile( LPFINDDATA p, char* workpath ) = 0;public:
    FileSearch( char* cPath )
    {
    m_cPath = NULL;
    if( cPath != NULL )
    {
    long lLength = strlen( cPath );
    m_cPath = new char[lLength+1];
    strcpy( m_cPath, cPath );
    }
    }
    void Run()
    {
    char oldworkpath[_MAX_PATH];
    _getcwd( oldworkpath, _MAX_PATH ); // ±
      

  2.   

    #ifndef FILE_SEARCH
    #define FILE_SEARCH#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <direct.h>
    #include <io.h>#include "StackPP.h"typedef _finddata_t FINDDATA;
    typedef _finddata_t* LPFINDDATA;
    typedef StackTempl < _finddata_t* > FindDataStack;class FileSearch
    {protected:
    char* m_cPath;protected:
    ~FileSearch()
    {
    delete []m_cPath;
    m_cPath = NULL;
    }
    virtual void ProcessFile( LPFINDDATA p, char* workpath ) = 0;public:
    FileSearch( char* cPath )
    {
    m_cPath = NULL;
    if( cPath != NULL )
    {
    long lLength = strlen( cPath );
    m_cPath = new char[lLength+1];
    strcpy( m_cPath, cPath );
    }
    }
    void Run()
    {
    char oldworkpath[_MAX_PATH];
    _getcwd( oldworkpath, _MAX_PATH ); // 保存当前工作路径

    FindDataStack stack;
    FINDDATA c_file;
    strcpy( c_file.name, m_cPath );

    LPFINDDATA p = new FINDDATA;
    memcpy( p, &c_file, sizeof(FINDDATA) );
    stack.push (p);

    char workpath[_MAX_PATH];
    while( (p = stack.pop()) != NULL )
    {
    _chdir( p->name );
    _getcwd( workpath, _MAX_PATH );
    strcat( workpath, "\\" );
    // printf( "working in: %s\n", workpath );
    long hFile;
    if( (hFile = _findfirst( "*", &c_file )) == -1L )
    {
    // do nothing
    }
    else
    {
    if( c_file.attrib & _A_SUBDIR )
    {
    if( strlen( c_file.name ) > 2 )
    {
    LPFINDDATA p = new FINDDATA;
    memcpy( p, &c_file, sizeof(FINDDATA) );
    strcpy( p->name, workpath );
    strcat( p->name, c_file.name );
    stack.push (p);
    }
    }
    else
    {
    ProcessFile( &c_file, workpath );
    }
    while( _findnext( hFile, &c_file ) == 0 )
    {
    if( c_file.attrib & _A_SUBDIR )
    {
    if( strlen( c_file.name ) > 2 )
    {
    LPFINDDATA p = new FINDDATA;
    memcpy( p, &c_file, sizeof(FINDDATA) );
    strcpy( p->name, workpath );
    strcat( p->name, c_file.name );
    stack.push (p);
    }
    }
    else
    {
    ProcessFile( &c_file, workpath );
    }
    }
    _findclose( hFile );
    }
    delete p;
    }
    _chdir( oldworkpath );
    return;
    }};#endif // FILE_SEARCH#include "FileSearch.h"
    class JPGFileSearch : public FileSearch
    {
    private:public:
    JPGFileSearch( char* cSourcePath, char* cDeskPath ):FileSearch(cSourcePath)
    {
    }
    ~JPGFileSearch()
    {
    }
    protected:
    void ProcessFile( LPFINDDATA p, char* workpath )
    {
    long filenamelen = strlen( p->name );
    if( filenamelen > 4)
    {
    if( (strcmp( p->name + filenamelen - 4, ".jpg" ) == 0 ) || ( strcmp( p->name + filenamelen - 4, ".jpe" ) ==0 ) )
    {
    long originpathlen = strlen( m_cPath );
    char relativepath[_MAX_PATH];
    strcpy( relativepath, workpath + originpathlen + 1 );
    long relativepathlen = strlen( relativepath );
    if( relativepathlen >1 && relativepath[relativepathlen-1] == '\\' ) relativepath[relativepathlen-1] = 0;
    DealWithFile( p->name, relativepath, workpath );
    }
    }
    }
    private: void DealWithFile( char* name, char* relativepath, char* workpath )
    { }
    };
      

  3.   

    你模仿JPGFileSearch 自己写一个avisearch吧#ifndef STACK_PP
    #define STACK_PP#define STACK_SIZE_STEP 1024
    typedef void* LPVOID;
    template < class LPTYPE >
    class StackTempl
    {
    private:
    long m_lGrowthStep;
    LPTYPE* pptr;
    long lCurrentSize;
    long sp;
    void Init( long lStackGrowthStep )
    {
    pptr = NULL;
    m_lGrowthStep = lStackGrowthStep;
    lCurrentSize = m_lGrowthStep;
    pptr = new LPTYPE[m_lGrowthStep];
    sp = 0;
    }public:
    StackTempl()
    {
    Init(STACK_SIZE_STEP);
    }
    StackTempl( long lStackGrowthStep )
    {
    Init( lStackGrowthStep );
    }
    ~StackTempl()
    {
    if( pptr != NULL )
    {
    delete []pptr;
    }
    }
    public:
    void push(LPTYPE p)
    {
    if(sp<lCurrentSize)
    pptr[sp++] = p;
    else
    {
    LPTYPE* pptrTemp = new LPTYPE[lCurrentSize+m_lGrowthStep];
    memcpy( pptrTemp, pptr, lCurrentSize*sizeof(LPTYPE) );
    delete []pptr;
    lCurrentSize += m_lGrowthStep;
    pptr = pptrTemp;
    pptr[sp++] = p;
    }
    }
    LPTYPE pop(void)
    {
    if( sp==0 )
    return NULL;
    else
    return pptr[--sp];
    }
    };#endif