遍历文件夹中的文件(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);

       }

      }

}

转载于:https://www.cnblogs.com/AlwinXu/archive/2009/03/20/1417572.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值