1.头文件:
#include <direct.h>//此头文件用来取自身的程序路径,在此文件中可以不要;
#include <cout.h>//这个是自己手动编写的MFC里可以用来控制台输出的头文件;
#include <thread>//多线程头文件;
using namespace std;
2.<cout.h>头文件的代码如下:
#ifndef Mycout
#define Mycout
#include "stdafx.h"
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <iostream>
using namespace std;
void initcout()//可以在自己程序中调用此函数,就可以实现控制台输出;
{
int nCrt = 0;
FILE* fp;
AllocConsole();
nCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
fp = _fdopen(nCrt, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
}
#endif
3.在自己的Button按钮里加入以下代码
char buff[1024];
_getcwd(buff, sizeof(buff));
cout << buff << endl;
strcat_s(buff, "\\my.ini");
if (_access(buff, 0) == -1)
{
char rootPath[10] = { 0 };
char threadpath[10] = { 0 };
for (char x = 'A'; x <= 'Z'; x++)//搜索多个盘符;
{
memset(rootPath, 0, 10);
memset(threadpath, 0, 10);
sprintf_s(rootPath, "%c:\\", x);
sprintf_s(threadpath, "%c:", x);
UINT nType = GetDriveType(rootPath);
if (nType != DRIVE_NO_ROOT_DIR)
{
thread th(th1, threadpath);//开启一个线程来搜索
th.detach();
cout << rootPath << "开始运行" << endl;
}
}
}
cout << "init运行完" << endl;
4.自定义的函数:
void th1(CString a)
{
findfile(a);
cout << a << "运行完"<<endl;
}
void findfile(CString path)//搜索文件;
{
WIN32_FIND_DATA p;
HANDLE h = FindFirstFile(path + "\\cl.exe", &p);
if (h != INVALID_HANDLE_VALUE)
{
puts(path + "\\" + p.cFileName);
CLpath = path + "\\" + p.cFileName;
Batpath = path + "\\" + "vcvars32.bat";
CMDpath = path;
ShellExecute(NULL, "open", Batpath, "", "", SW_HIDE);
stop = FALSE;
return ;
}
CFileFind finder;
CString strWildcard(path);
strWildcard += _T("\\*.*");
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking && stop)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
CString str = finder.GetFilePath();
if (finder.IsDirectory())
{
//cout << str << endl;
findfile(str);
}
/*else
{
cout << str << endl;
}*/
}
finder.Close();
}