遍历文件夹中的文件(c++)

Source code:

/*----------------------------------------------------------------

// Copyright (C) 2008 ShanDong University At Weihai

// FileNameStruct_type.cpp

// Function Descriptionthis program can imitate search virus from the given folder

// Created ByAlwin Xu

// Created Date: 2009-2-4 ~ 2009-2-5

// Modified By

// Modified Description

// Modified Date:

//----------------------------------------------------------------*/

#include "stdafx.h"

#include <iostream>

#include <windows.h>

#include <string.h>

#include <fstream>

using namespace std;

 

/////////////////////////////////

char virusArr[10][10];

static int count=0;

void initArr();

void printVirus(string strsrc,char* filename);

void searchVirus(char *filename);

void SearchAllFiles(char foldername[]);

bool CheckName(char *isFolder);

char* CutPath(char *foldername);

char* CutPath(char *foldername,int number);

 

//////////main()///////////////////

int _tmain(int argc, _TCHAR* argv[])

{

      char check;

      int flag = 1;    

      while(flag)

      {

            if(count<10)

            {

                  cout<<"add a new virus:"<<endl;//add new virus.

                  cin>>virusArr[count];

                  count++;

                  cout<<"add virus again(Y or N):"<<endl;

                  cin>>check;

                  if('Y'==check || 'y'==check)

                        flag = 1;

                  else

                        flag = 0;

            }          

      }

      char foldername[20];

      cout<<"enter you foldername(10):   ";

      cin>>foldername;

      SearchAllFiles(foldername);

      return 0;

}

 

//////////initialize virusArr./////////////////

void initArr()

{

      memset(virusArr,0,100);

}

 

////////////check folder//////////////////////////////////////

bool CheckName(char *isFolder)//check if the name is a folder's name

{

      bool sign =false;

      int len = strlen(isFolder);

      int count=0;

      while(*isFolder++ != '.' && count++ <len);

      if(count < len-1)

      {

            sign = true;

      }

      return sign;

}

//////////cut the last """/////////////////////////////////////////////

char* CutPath(char *foldername)

{

      char *temp = foldername;

      int len = strlen(foldername);

    for(int i=len;i>0;i--)

      {

            if(foldername[i]=='""')

            {

                  foldername[i+1]='"0';

                  i=0;

            }

      }

    return temp;

}

 

/////////cut path according number///////////////////////////////////

char* CutPath(char *foldername,int number)

{

      char *temp = foldername;

      int len = strlen(foldername);

      int count = 0;

    for(int i=len;i>0;i--)

      {

            if(foldername[i]=='""')

            {

                  count++;

            }

            if(number == count)

            {

                  foldername[i+1]='"0';

                  i = 0;

            }

      }

    return temp;

}

//////////////search virus from a file//////////////////////

void searchVirus(char *filename)

{

      if(!filename)

      {

            exit(0);

      }

      ifstream isopen(filename);//open file

      if(isopen.fail())

      {

            cout<<"the file may be not exsit,please check it."<<endl;

            exit(0);

      }

      else

      {

            char ch;

            string filecontent="";

            while(!isopen.eof())

            {

                  isopen.get(ch);

                  filecontent += ch;

            }

            printVirus(filecontent,filename);//print virus we have found.

            filecontent="";

            isopen.close();

      }

}

 

 

///////////////print////////////////////////

void printVirus(string strsrc,char* filename)

{

      cout<<"file path: "<<filename<<endl;

      bool haveOrNot = false;

      for(int i=0;i<count;i++)

      {

            int length = strsrc.length();

            if(strsrc.find(virusArr[i])<length)

            {

                  printf(""a");

                  cout<<"find virus: "<<virusArr[i]<<endl;

                  haveOrNot = true;

            }

      }

      if(false == haveOrNot)

      {

            cout<<"no virus!"<<endl;

      }

}

 

/////////////search all files from a given folder./////////////////////////

void SearchAllFiles(char foldername[])

{

      WIN32_FIND_DATA FindFileData;

      HANDLE hFind = INVALID_HANDLE_VALUE;

      char DirSpec[MAX_PATH]; // directory specification

      DWORD dwError;

      char temp[20];

      int sign =0;

      strcpy(temp,foldername);

      strcat(foldername,"""*.*");

      hFind = FindFirstFile(foldername, &FindFileData);

      if (hFind == INVALID_HANDLE_VALUE)

      {

       printf ("Invalid file handle. Error is %u"n", GetLastError());

      }

      else

      {

         std::wcout<<FindFileData.cFileName<<endl;

         FindNextFile(hFind, &FindFileData);

       while (FindNextFile(hFind, &FindFileData) != 0)

       {

             char transfer[15]={0};

             strcpy(transfer,FindFileData.cFileName);

             if(CheckName(transfer))

             {

                   if(sign == 0)

                   {

                         strncat(temp,"""",2);

                         strncat(temp,transfer,15);

                         searchVirus(temp);

                         sign = 1;

                   }

                   else

                   {

                         CutPath(temp);

                         strncat(temp,transfer,15);

                         searchVirus(temp);

                   }

             }

             else

             {

                   if(sign == 0)

                   {

                         strncat(temp,"""",2);

                         strncat(temp,transfer,15);

                         SearchAllFiles(temp);

                         CutPath(temp,2);

                         sign = 1;

                   }

                   else

                   {                    

                         CutPath(temp);

                         strncat(temp,transfer,15);

                         SearchAllFiles(temp);

                         CutPath(temp,2);

                   }

             }

       }

       dwError = GetLastError();

       FindClose(hFind);

       if (dwError != ERROR_NO_MORE_FILES)

       {

             printf ("FindNextFile error. Error is %u"n", dwError);

       }

      }

}

posted on 2009-03-20 12:30  Alvin_Xu  阅读(2492)  评论(0编辑  收藏  举报