導航:首頁 > 版本升級 > vc遍歷磁碟查找文件

vc遍歷磁碟查找文件

發布時間:2024-04-11 01:15:20

⑴ C/C++編程遍歷文件夾,統計當前文件個數,輸出文件名

標准C是沒有目錄相關的函數的
CFree是16位的吧,那就更不用想了.貌似只能內嵌匯編使用dos中斷來完成.

還是換編譯器吧devcpp codeblock vc8 之類的都很好

【cmail】:
這個要用到windows API

HANDLE FindFirstFile(
LPCTSTR lpFileName,
LPWIN32_FIND_DATA lpFindFileData
);

BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);

WIN32_FIND_DATA

【CHROX】:
在Win32平台下不用windows api,有好多功能實現起來都很費勁,特別是系統相關的
再說用api又不是什麼丟人的事。開發可移植程序除外。
用FindFirstFile和FindNextFile兩個api很容易實現
////////////////////////////////////////////////
void FindFile(LPCTSTR lpszPath) {
TCHAR szFind[MAX_PATH];
lstrcpy(szFind,lpszPath);

if(!IsRoot(szFind)) lstrcat(szFind,"\\");
lstrcat(szFind,"*.*");
WIN32_FIND_DATA wfd;
HANDLE hFind=FindFirstFile(szFind,&wfd);

if(hFind==INVALID_HANDLE_VALUE) return;
do{
if(lstrcmp(wfd.cFileName,".")==0||lstrcmp(wfd.cFileName,"..")==0) continue;
char szFile[MAX_PATH];
lstrcpy(szFile,lpszPath);
if(!IsRoot(szFile)) lstrcat(szFile,"\\");
lstrcat(szFile,wfd.cFileName);
if((GetFileAttributes(szFile)&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY){
FindFile(szFile); //遞歸
}
else {
} //Do your things
}
} while (FindNextFile(hFind,&wfd));
CloseHandle(hFind);
}

【Geomatic】:
原來FindFirstFile和FindNextFile是WINDOWS的API 我暈 我以為是C++的函數庫里的東西呢
我明白了 我找到的代碼和CHROX的差不多
#include <stdio.h>
#include <windows.h>

BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}

void FindInAll(::LPCTSTR lpszPath)
{TCHAR szFind[MAX_PATH];<br>lstrcpy(szFind, lpszPath);<br>if (!IsRoot(szFind))<br>lstrcat(szFind, "\\");<br>lstrcat(szFind, "*.*"); // 找所有文件<br>WIN32_FIND_DATA wfd;<br>HANDLE hFind = FindFirstFile(szFind, &wfd);<br>if (hFind == INVALID_HANDLE_VALUE) // 如果沒有找到或查找失敗<br>return;<br><br>do<br>{<br>if (wfd.cFileName[0] == '.')<br>continue; // 過濾這兩個目錄<br>if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)<br>{<br>TCHAR szFile[MAX_PATH];<br>if (IsRoot(lpszPath))<br>wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);<br>else<br>wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);<br>FindInAll(szFile); // 如果找到的是目錄,則進入此目錄進行遞歸<br>}
else
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
// 對文件進行操作
}
} while (FindNextFile(hFind, &wfd));
FindClose(hFind); // 關閉查找句柄

}
int main(int argc, char* argv[])
{
FindInAll("g://music");
return 0;
}
謝謝大家的幫助

【Geomatic】:
這個怎麼給你們給分啊
那個分有用嗎
剛申請的 好多功能不會用啊

【cmail】:
點上面的「管理」。

【CHROX】:
分可是個好東西。呵呵,你以後就明白了。

【jixingzhong】:
DEV C++, 利用鏈表實現目錄內所有文件列表顯示

#include <stdio.h>
#include <dirent.h>
/*#include <alloc.h>*/
#include <string.h>

void main(int argc,char *argv[])
{
DIR *directory_pointer;
struct dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
if (argc!=2)
{
printf("Must specify a directory\n");
exit(1);
}
if ((directory_pointer=opendir(argv[1]))==NULL)
printf("Error opening %s\n",argv[1]);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node->next=(struct FileList *)malloc(sizeof(struct FileList));
node=node->next;
strcpy(node->filename,entry->d_name);
node->next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf("%s\n",node->filename);
node=node->next;
}
}
}

【jixingzhong】:
linux下面的,作者不是我

A Demo written by camelrain

/*
the program find a file from current directory or your defined directory
commond optinon [path] filename
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define LENGTH 256

/* just if is a directory*/
static int IsDir (char * name);
/* search target file, arg1 is current path, arg2 is target file*/
static void search_file (char * path, char * name);

static int search_flag=0;

/*just if is a directory*/
static int IsDir (char * name) {
struct stat buff;

if (lstat(name,&buff)<0)
return 0; //if not exist name ,ignore

/*if is directory return 1 ,else return 0*/
return S_ISDIR(buff.st_mode);
}

/*search target file*/
static void search_file (char * path, char * name) {
DIR *directory;
struct dirent * dir_entry;
char buffer[LENGTH];

if ((directory=opendir(path)) == NULL) {
fprintf(stderr, "%s", path);
perror(" ");
return;
}

while (dir_entry=readdir(directory)) {
if (!strcmp(dir_entry->d_name,".")||!strcmp(dir_entry->d_name,"..")) {
/* do nothing*/
}
else {
/* if is boot directory add "/" */
if ((strcmp(path,"/"))==0)
sprintf(buffer,"%s%s",path,dir_entry->d_name);
/* if is not boot directory do not add "/"*/
else
sprintf(buffer,"%s/%s",path,dir_entry->d_name);

//printf("Now file : %s\n",buffer);
if (IsDir(buffer))
search_file (buffer , name);
else {
if (strcmp(dir_entry->d_name,name)==0)
{
printf("%s\n",buffer);
search_flag=1;
}
}
}
}
closedir(directory);
}

int main(int argc, char *argv[])
{
static char * current_dir;
static char * filename;
int length;

if (argc==1) {
printf("A few parameters!!\n");
return 0;
}

if (argc==2) {
current_dir=(char * )getcwd(current_dir,LENGTH);
filename=argv[1];
}

if (argc==3) {
length=strlen(argv[1]);

if (length>1 && (argv[1][length-1]=='/')) {
argv[1][length-1]='\0';
//printf("%d\n",strlen(argv[1]));
}

current_dir=argv[1];
filename=argv[2];
}

search_file(current_dir,filename);

if (!search_flag)
printf("Not found this(%s) file!\n",filename);

return 0;
}

【jixingzhong】:
VC 下的:

long handle;
struct _finddata_t filestruct;
char path_search[_MAX_PATH];
handle = _findfirst("目錄",&filestruct);
if((handle == -1)) return;
if( ::GetFileAttributes(filestruct.name)& FILE_ATTRIBUTE_DIRECTORY )
{
if( filestruct.name[0] != '.' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
}
else
{
if( !stricmp(filestruct.name, szFilename) )
{
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
while(!(_findnext(handle,&filestruct)))
{
if( ::GetFileAttributes(filestruct.name) &FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '.')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
}

⑵ 在windows下 怎麼用c語言遍歷文件夾要用純c的

什麼叫純C?
用C語言遍歷文件肯定需要用到函數,標准C下貌似沒有這個函數內,但容是使用VC的函數庫可能可以實現,如果實在不行可以用第三方函數庫,,,還不行的話用system("command");引用dos命令可以遍歷,

⑶ 如何遍歷目錄,找出磁碟所有目錄的dll文件

這是 a.bat 批命令程序,遍歷目錄,找出磁碟所有目錄的dll文件,存放結果到 temp.txt 文件里。內temp.txt 文件可供C程序進一步處容理用,一行一個文件名,含全路徑名。

C 程序 用
system("a.bat"); 調用即可。
若從某盤根目錄進入尋找,添:
cd C:\\
D:
cd D:\\
這類命令即可。

@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /b /s "*.dll"') do (
echo %%i
set str=%%i
set str=!str:\=\\!
echo !str! >> temp.txt)
@echo on

⑷ vc如何遍歷全盤(包括子目錄)

int count_file(char *dir)
{
_finddata_t p;//定義一個結構體存放文件屬性
int n,n1;
char *a;
n=strlen(dir);a=new char [n+100];n1=0;
strcpy(a,dir);strcat(a,"\\*.*");
if((n=_findfirst(a,&p))!=-1L)//調用文件查找函數
{
if(strcmp(p.name,".")&&strcmp(p.name,".."))
{
if((p.attrib&_A_SUBDIR))//文件是否為文件夾
{
int n2=strlen(dir)+strlen(p.name);
char *b=new char [n2+10];strcpy(b,dir);
strcat(b,"\\");strcat(b,p.name);
n1+=count_file(b);//遞歸調用 作用是返迴文件夾內的文件總數
n1++;
delete(b);
}
}
while(_findnext(n,&p)==0)//尋找下一個文件
{
if(strcmp(p.name,".")&&strcmp(p.name,".."))
{
if((p.attrib&_A_SUBDIR))
{
int n2=strlen(dir)+strlen(p.name);
char *b=new char [n2+10];strcpy(b,dir);
strcat(b,"\\");strcat(b,p.name);
n1+=count_file(b);delete(b);n1++;
}
else n1++;
}
}
_findclose(n);//結束尋找
}
delete(a);return n1;//返迴文件總個數
}

⑸ VC6.0編譯環境下遍歷文件夾的源代碼

#include "stdafx.h"
#include <windows.h>

BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}

void FindInAll(::LPCTSTR lpszPath)
{TCHAR szFind[MAX_PATH];
lstrcpy(szFind, lpszPath);
if (!IsRoot(szFind))
lstrcat(szFind, "\\");
lstrcat(szFind, "*.*"); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind == INVALID_HANDLE_VALUE) // 如果沒有找到或查找失敗
return;

do
{
if (wfd.cFileName[0] == '.')
continue; // 過濾這兩個目錄
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
FindInAll(szFile); // 如果找到的是目錄,則進入此目錄進行遞歸
}
else
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
// 對文件進行操作
}
} while (FindNextFile(hFind, &wfd));
FindClose(hFind); // 關閉查找句柄

}
int main(int argc, char* argv[])
{
FindInAll("e:\\result");
return 0;
}
//結合網上資料寫出的,作者--楊克群^_^

閱讀全文

與vc遍歷磁碟查找文件相關的資料

熱點內容
蘋果6p升級後卡刷機有用嗎 瀏覽:816
hhi分析的什麼數據指標 瀏覽:705
蘋果文件word文檔 瀏覽:119
網路力量大是什麼原因 瀏覽:449
蘋果電腦編輯文件在哪裡設置 瀏覽:91
java基本類型數組拼接 瀏覽:330
網頁嵌套靜態數據文件 瀏覽:487
圓弧長弦長計算用什麼APP 瀏覽:403
獨占方式鎖定此配置文件失敗 瀏覽:256
手機word文件在哪裡可以找到 瀏覽:741
linux支持exe嗎 瀏覽:672
蘋果手機組裝機是iso系統么 瀏覽:807
win10pe光碟 瀏覽:181
雙易淘是什麼網站 瀏覽:380
天正文件怎麼打開 瀏覽:590
從文件夾安裝工具104 瀏覽:589
蘋果系統怎麼安裝icc配置文件 瀏覽:119
linuxshell下載 瀏覽:513
每日養生app是怎麼玩 瀏覽:96
extjsdateformat 瀏覽:774

友情鏈接