C++文件夹遍历代码11251001221

#include
#include
#include "windows.h"
using namespace std;

char* FindPath(const char* StartPath,const char* FileName)
{
_finddata_t FileInfo;
char str_1[128],str_2[128],str_3[128];
str_3[0] = '\0';

strcpy(str_1,StartPath);
strcat(str_1,"\\*");
long Handle = _findfirst(str_1,&FileInfo);
if(Handle == -1L)
return 0;

strcpy(str_2,StartPath);
strcat(str_2,"\\");
while(!_findnext(Handle,&FileInfo))
{
if(FileInfo.attrib &_A_SUBDIR)
{
if(strcmp(https://www.360docs.net/doc/435466979.html,,"..") && strcmp(https://www.360docs.net/doc/435466979.html,,"."))
{
strcat(str_2,https://www.360docs.net/doc/435466979.html,);
char* ch = FindPath(str_2,FileName);
if(ch != 0)
{
strcpy(str_3,ch);
break;
}
}
}
else
{
if(!strcmp(https://www.360docs.net/doc/435466979.html,,FileName))
{
strcat(str_2,https://www.360docs.net/doc/435466979.html,);
return str_2;
}
}
strcpy(str_2,StartPath);
strcat(str_2,"\\");
}
_findclose(Handle);
if(str_3[0] != 0)
return str_3;
else
return 0;
}

void Open(const char* ProgramRoute,const char* FileRoute)
{
STARTUPINFO s;
PROCESS_INFORMATION p;
s.cb = sizeof(s);
memset(&s,0,s.cb);
memset(&p,0,sizeof(p));

char cmdline[256];
strcpy(cmdline,ProgramRoute);
strcat(cmdline," ");
strcat(cmdline,FileRoute);
if(!CreateProcess(NULL,cmdline,NULL,NULL,false,0,NULL,NULL,&s,&p))
cout<<"Failed to create process.\nError code: "<}

void united(char* ProRoute,char* FileRoute,char* program,char* file)
{
if(FindPath(ProRoute,program) && FindPath(FileRoute,file))
{
char programRoute[128],fileRoute[128];
strcpy(programRoute,FindPath(ProRoute,program));
strcpy(fileRoute,FindPath(FileRoute,file));
Open(programRoute,fileRoute);
}
else
cout<<"Not Found.\n";
}


int main()
{
char* ProRoute = "C:";
char* FileRoute = "C:";
char* program = "WINWORD.EXE";
char* file = "assignment.doc"; //文件名中不要有空格,否则会打开出错 xp下运行不能
united(ProRoute,FileRoute,program,file);
return 0;
}

相关文档
最新文档