欧卡2中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

需要三步,才能开始

只需两步,慢速开始

玩欧卡就用莱仕达V10方向盘欧卡2入门方向盘选莱仕达V9莱仕达折叠便携游戏方向盘支架欢迎地图Mod入驻
查看: 3754|回复: 0
收起左侧

c++实现复制指定文件夹下视频文件

[复制链接]
script 发表于 2011-11-10 21:01 | 显示全部楼层 |阅读模式
#include <windows.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;

#define LEN 1024
// 深度优先递归遍历目录中所有的文件
BOOL  DirectoryList(LPCSTR Path,char *ch)
{
  WIN32_FIND_DATA FindData;
  HANDLE hError;
  int FileCount = 0;
  char FilePathName[LEN];
  // 构造路径
  char FullPathName[LEN];
  strcpy(FilePathName, Path);
  strcat(FilePathName, "\\*.*");
  hError = FindFirstFile(FilePathName, &FindData);
  if (hError == INVALID_HANDLE_VALUE)
  {
    cout<<"搜索失败!"<<endl;
    return 0;
  }
  while(::FindNextFile(hError, &FindData))
  {
    // 过虑.和..
    if (strcmp(FindData.cFileName, ".") == 0
     || strcmp(FindData.cFileName, "..") == 0 )
    {
    continue;
    }
   
    // 构造完整路径
   wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);
  FileCount++;
   // 输出本级的文件
   cout<<FileCount<<" "<<FullPathName<<endl;
   string fullpath=FullPathName;
   string extension;
   unsigned int loc=fullpath.rfind(".");
   if(loc!=string::npos)
   extension=fullpath.substr(loc,fullpath.length()-loc);
   //文件扩展名,可根据需要自行添加
   vector<string>extName;
   {
   extName.push_back(".avi");
   extName.push_back(".AVI");
   extName.push_back(".RMVB");
   extName.push_back(".rmvb");
   extName.push_back(".rm");
   extName.push_back(".RM");
   extName.push_back(".wmv");
   extName.push_back(".WMV");
   extName.push_back(".MP4");
   extName.push_back(".mp4");
   extName.push_back(".mov");
   extName.push_back(".MOV");
   extName.push_back(".3gp");
   extName.push_back(".3GP");
   extName.push_back(".MKV");
   extName.push_back(".mkv");
   extName.push_back(".flv");
   extName.push_back(".FLV");
   }
   for(unsigned int i=0;i<extName.size();i++)
   {
    if(extension==extName[i])
    {
     char *cpy=new char[100];
     strcpy(cpy,"copy ");
     strcat(FullPathName," ");
     strcat(cpy,FullPathName);
     strcat(cpy,ch);
     system(cpy);
    }
   }

    if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
     cout<<"<Dir>";
     DirectoryList(FullPathName,ch);
    }

  }
  return 0;
}

//检查指定目录是否存在
bool  CheckFolderExist(const string &strPath)
{
    WIN32_FIND_DATA  wfd;
    bool rValue = false;
    HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
    if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
        rValue = true;  
    }
    FindClose(hFind);
    return rValue;
}

int main()
{
  cout<<"请输入要搜索的文件夹路径:";
  char *path=new char[100];
  cin>>path;
  cout<<"请输入要复制到的文件夹路径:";
  char *dir=new char[100];
  cin>>dir;
  string folder=dir;
 //检查指定目录是否存在
    if (!CheckFolderExist(folder))
    {
  char *mdfolder=new char[50];
  strcpy(mdfolder,"md ");
  strcat(mdfolder,dir);
        cout<<"指定目录不存在..."<<endl;
  cout<<"是否创建该目录?(Y/N)"<<endl;
  string choice;
  cin>>choice;
  if(choice=="N"||choice=="n")
  {
   cout<<"程序即将退出..."<<endl;
   system("pause");
   exit(0);
  }
  else
  {
   system(mdfolder);
   DirectoryList(path,dir);
  }
  
    }
 else
  DirectoryList(path,dir);
  cout<<endl;
  system("pause");
  return 0;
}

联系我们|手机版|欧卡2中国 ( 湘ICP备11020288号-1 )

GMT+8, 2024-3-29 21:15 , Processed in 0.031882 second(s), 9 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表