Windows下のC言語検索ファイルの例


Windows下のC言語検索ファイルの例
// 2_4.cpp : Defines the entry point for the console application.
//
//========================================================================= 
//      :     
//      : [email protected]	
//      : http://blog.csdn.net/ssun125
//      : c      
//      : cmd search.exe      (      *、?)( :search.exe E: *.java)
//      : 2013 01 25 
//=========================================================================

#include "stdafx.h"
#include <STDIO.H>
#include <MALLOC.H>
#include <STRING.H>
#include <windows.h>

//              
typedef struct DirList{
	char name[256];
	DirList * next;
} *LpDirList;

DirList * first, * last; 

//        
void add(char * name)
{
	DirList * newDir = (LpDirList)malloc(sizeof(DirList));
	strcpy(newDir->name, name);
	newDir->next = NULL;
	last->next = newDir;
	last = newDir; 
}

void loopFind(char * dir, char * filename)
{
	//printf("       ...
"); char searchName[256] = {0}; char nextDir[256] = {0}; strcpy(searchName, dir); strcat(searchName, "\\**"); // WIN32_FIND_DATA findData; HANDLE hFindFile = FindFirstFile(searchName, &findData); while (FindNextFile(hFindFile, &findData)) { if(findData.cFileName[0] == '.') continue; if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { strcpy(nextDir, dir); strcat(nextDir, "\\"); strcat(nextDir, findData.cFileName); add(nextDir); memset(nextDir, 0x00, sizeof(nextDir)); } } // , char nextFileName[256] = {0}; memset(searchName, 0x00, sizeof(searchName)); strcpy(searchName, dir); strcat(searchName, "\\"); strcat(searchName, filename); hFindFile = FindFirstFile(searchName, &findData); while (FindNextFile(hFindFile, &findData)) { strcpy(nextFileName, dir); strcat(nextFileName, "\\"); strcat(nextFileName, findData.cFileName); printf("%s
", nextFileName); } } void search(char * dir, char * filename) { printf(" ...
"); first = (LpDirList)malloc(sizeof(DirList)); strcpy(first->name, dir); first->next = NULL; last = first; while (first != NULL) { loopFind(first->name, filename); first = first->next; } } int main(int argc, char* argv[]) { if(argv[1]==NULL || argv[2]==NULL) { printf(" !
"); return 0; } search(argv[1], argv[2]); return 0; }
結果のスクリーンショット:
Windows下C语言查找文件例子_第1张图片