當前位置:首頁 » 文件管理 » c遍歷指定文件夾中的所有文件

c遍歷指定文件夾中的所有文件

發布時間: 2022-09-19 00:41:56

A. 請我如何c語言遍歷文件夾

現成的沒有,給你參考下:
c++:
1、CFileFind類實現遍歷文件,FindNext()查找下一個文件
2、比較文件後綴,符合要求就寫入txt
有不懂得話加Q:2311776177!

B. C語言如何實現遍歷文件夾下的所有txt文件並在文件中搜索字元串

用 FINDFile和FindNextFile可以遍歷整個文件夾,然後取出文件名判斷是否txt,再打開文件讀取內容進行查找。

C. 用C語言編出遍歷出某個目錄以及其子目錄下所有以TXT為擴展名的文本文件

#include"StdAfx.h"
#include"FindFile.h"

//這里只是測試函數
//一般我們遍歷文件都是有目的同這個寫類似病毒一些東西
voidTest(WIN32_FIND_DATA*fd)
{
MessageBox(0,fd->cFileName,0,0);
}

BOOLFindFile(char*pFileName,char*FindFileType)
{
WIN32_FIND_DATAfd;
HANDLEhFind;
charType[10]={0};
sprintf(Type,".%s",FindFileType);
//HANDLEhFindType;//查找對應文件類型
charFileName[260]={0};

sprintf(FileName,"%s\*.*",pFileName);
ZeroMemory(&fd,sizeof(fd));

if(0==strlen(FileName))
{
MessageBox(0,"文件名不能為空",0,0);
returnFALSE;
}
else
{
hFind=FindFirstFile(FileName,&fd);
if(INVALID_HANDLE_VALUE==hFind)
{
returnFALSE;
}
else
{
do
{
//查找文件名字中有沒與.文件後綴注意要加點因為點在文件名字是特殊符號
//一般是不可以用的。但可以用特殊方法來添加。
//U盤免疫就是用這中方法來來建立一個不能用普通方法刪除autorun.inf的文件夾
if(strstr(fd.cFileName,Type))
{
Test(&fd);
}
else
{
//當該文檔是文件時候進行下列操作
if(fd.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
{
//除掉.表示當前目錄和..上一個目錄這個在dos中可以看到
if(strcmp(fd.cFileName,".")!=0&&strcmp(fd.cFileName,"..")!=0)
{
charTemp[256]={0};
sprintf(Temp,"%s\%s",pFileName,fd.cFileName);
FindFile(Temp,FindFileType);

}

}
}

//繼續查找下一個文件
}while(FindNextFile(hFind,&fd));
}
}
re

D. 怎麼用C語言編程遍歷文件夾下所有文件名


/**************************************************
這是CBrowseDir的類定義文件BrowseDir.h

/**************************************************
#include"stdlib.h"

classCBrowseDir
{
protected:
//存放初始目錄的絕對路徑,以''結尾
charm_szInitDir[_MAX_PATH];

public:
//預設構造器
CBrowseDir();

//設置初始目錄為dir,如果返回false,表示目錄不可用
boolSetInitDir(constchar*dir);

//開始遍歷初始目錄及其子目錄下由filespec指定類型的文件
//filespec可以使用通配符*?,不能包含路徑。
//如果返回false,表示遍歷過程被用戶中止
boolBeginBrowse(constchar*filespec);

protected:
//遍歷目錄dir下由filespec指定的文件
//對於子目錄,採用迭代的方法
//如果返回false,表示中止遍歷文件
boolBrowseDir(constchar*dir,constchar*filespec);

//函數BrowseDir每找到一個文件,就調用ProcessFile
//並把文件名作為參數傳遞過去
//如果返回false,表示中止遍歷文件
//用戶可以覆寫該函數,加入自己的處理代碼
virtualboolProcessFile(constchar*filename);

//函數BrowseDir每進入一個目錄,就調用ProcessDir
//並把正在處理的目錄名及上一級目錄名作為參數傳遞過去
//如果正在處理的是初始目錄,則parentdir=NULL
//用戶可以覆寫該函數,加入自己的處理代碼
//比如用戶可以在這里統計子目錄的個數
virtualvoidProcessDir(constchar
*currentdir,constchar*parentdir);
};


/*********************************************/

這是CBrowseDir的類實現文件BrowseDir.cpp

/***********************************************/
#include"stdlib.h"
#include"direct.h"
#include"string.h"
#include"io.h"

#include"browsedir.h"

CBrowseDir::CBrowseDir()
{
//用當前目錄初始化m_szInitDir
getcwd(m_szInitDir,_MAX_PATH);

//如果目錄的最後一個字母不是'',則在最後加上一個''
intlen=strlen(m_szInitDir);
if(m_szInitDir[len-1]!='\')
strcat(m_szInitDir,"\");
}

boolCBrowseDir::SetInitDir(constchar*dir)
{
//先把dir轉換為絕對路徑
if(_fullpath(m_szInitDir,dir,_MAX_PATH)==NULL)
returnfalse;

//判斷目錄是否存在
if(_chdir(m_szInitDir)!=0)
returnfalse;

//如果目錄的最後一個字母不是'',則在最後加上一個''
intlen=strlen(m_szInitDir);
if(m_szInitDir[len-1]!='\')
strcat(m_szInitDir,"\");

returntrue;
}

boolCBrowseDir::BeginBrowse(constchar*filespec)
{
ProcessDir(m_szInitDir,NULL);
returnBrowseDir(m_szInitDir,filespec);
}

boolCBrowseDir::BrowseDir
(constchar*dir,constchar*filespec)
{
_chdir(dir);

//首先查找dir中符合要求的文件
longhFile;
_finddata_tfileinfo;
if((hFile=_findfirst(filespec,&fileinfo))!=-1)
{
do
{
//檢查是不是目錄
//如果不是,則進行處理
if(!(fileinfo.attrib&_A_SUBDIR))
{
charfilename[_MAX_PATH];
strcpy(filename,dir);
strcat(filename,fileinfo.name);
if(!ProcessFile(filename))
returnfalse;
}
}while(_findnext(hFile,&fileinfo)==0);
_findclose(hFile);
}

//查找dir中的子目錄
//因為在處理dir中的文件時,派生類的ProcessFile有可能改變了
//當前目錄,因此還要重新設置當前目錄為dir。
//執行過_findfirst後,可能系統記錄下了相關信息,因此改變目錄
//對_findnext沒有影響。
_chdir(dir);
if((hFile=_findfirst("*.*",&fileinfo))!=-1)
{
do
{
//檢查是不是目錄
//如果是,再檢查是不是.或..
//如果不是,進行迭代
if((fileinfo.attrib&_A_SUBDIR))
{
if(strcmp(fileinfo.name,".")!=0&&strcmp
(fileinfo.name,"..")!=0)
{
charsubdir[_MAX_PATH];
strcpy(subdir,dir);
strcat(subdir,fileinfo.name);
strcat(subdir,"\");
ProcessDir(subdir,dir);
if(!BrowseDir(subdir,filespec))
returnfalse;
}
}
}while(_findnext(hFile,&fileinfo)==0);
_findclose(hFile);
}
returntrue;
}

boolCBrowseDir::ProcessFile(constchar*filename)
{
returntrue;
}

voidCBrowseDir::ProcessDir(constchar
*currentdir,constchar*parentdir)
{
}


/*************************************************
這是例子example.cpp

/*************************************************
#include"stdio.h"

#include"BrowseDir.h"

//從CBrowseDir派生出的子類,用來統計目錄中的文件及子目錄個數
classCStatDir:publicCBrowseDir
{
protected:
intm_nFileCount;//保存文件個數
intm_nSubdirCount;//保存子目錄個數

public:
//預設構造器
CStatDir()
{
//初始化數據成員m_nFileCount和m_nSubdirCount
m_nFileCount=m_nSubdirCount=0;
}

//返迴文件個數
intGetFileCount()
{
returnm_nFileCount;
}

//返回子目錄個數
intGetSubdirCount()
{
//因為進入初始目錄時,也會調用函數ProcessDir,
//所以減1後才是真正的子目錄個數。
returnm_nSubdirCount-1;
}

protected:
//覆寫虛函數ProcessFile,每調用一次,文件個數加1
virtualboolProcessFile(constchar*filename)
{
m_nFileCount++;
returnCBrowseDir::ProcessFile(filename);
}

//覆寫虛函數ProcessDir,每調用一次,子目錄個數加1
virtualvoidProcessDir
(constchar*currentdir,constchar*parentdir)
{
m_nSubdirCount++;
CBrowseDir::ProcessDir(currentdir,parentdir);
}
};

voidmain()
{
//獲取目錄名
charbuf[256];
printf("請輸入要統計的目錄名:");
gets(buf);

//構造類對象
CStatDirstatdir;

//設置要遍歷的目錄
if(!statdir.SetInitDir(buf))
{
puts("目錄不存在。");
return;
}

//開始遍歷
statdir.BeginBrowse("*.*");

//統計結果中,子目錄個數不含.及..
printf("文件總數:%d 子目錄總數:
%d ",statdir.GetFileCount(),
statdir.GetSubdirCount());
}

E. 如何用C代碼遍歷整個windows文件夾查找某個特定文件

#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;
}
//結合網上資料寫出的,作者--楊克群^_^

F. C# 遍歷文件夾下所有子文件夾中的文件,得到文件名

輸入某文件夾路徑,遍歷該文件夾及其子文件夾(包括子文件夾的子文件夾等),獲取其中所有文件的函數:

/// <summary>

/// 查找指定文件夾下指定後綴名的文件

/// </summary>

/// <param name="directory">文件夾</param>

/// <param name="pattern">後綴名</param>

/// <returns>文件路徑</returns>

public void GetFiles(DirectoryInfo directory, string pattern, ref List<string> fileList)

foreach (FileInfo info in directory.GetFiles(pattern))

catch (System.Exception ex)

foreach (DirectoryInfo info in directory.GetDirectories())//獲取文件夾下的子文件夾

語言結構

類:一個基本的C#類中包含數據成員、屬性、構造器和方法。屬性可以是靜態或實例成員。在C#中類的聲明與C++和Java很相似。但是,不像C++,C#結構體與類是不支持繼承多個父類。但是,與Java相同的是,一個結構體可以實現介面(interface)。Java的關鍵字import已經被替換成using,它起到了同樣的作用。

以上內容參考:網路-c#

G. 遍歷給定目錄下的所有文件 c

Dim MyFile As String,sPath As String
sPath="c:\windows"
MyFile = Dir(sPath, vbDirectory Or vbHidden Or vbSystem Or vbReadOnly)
Do While MyFile <> ""
If (GetAttr(sPath & MyFile) And vbDirectory) = vbDirectory Then
List1.AddItem MyFile
Else
List2.AddItem MyFile
End If
MyFile = Dir
Loop
Debug.Print "共計:" & List1.ListCount & "個目錄 " & List2.ListCount & "個文件"

H. 關於遍歷文件夾的C語言問題,急求,謝謝。

#include "dir.h"
#include "stdio.h"
/*
int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件,成功
返回0
pathname為指定的目錄名和文件名,如"C:\\WPS\\TXT"
ffblk為指定的保存文件信息的一個結構,定義如下:
┏━━━━━━━━━━━━━━━━━━┓
┃struct ffblk ┃
┃{ ┃
┃ char ff_reserved[21]; //DOS保留字┃
┃ char ff_attrib; //文件屬性 ┃
┃ int ff_ftime; //文件時間 ┃
┃ int ff_fdate; //文件日期 ┃
┃ long ff_fsize; //文件長度 ┃
┃ char ff_name[13]; //文件名 ┃
┃} ┃
┗━━━━━━━━━━━━━━━━━━┛
attrib為文件屬性,由以下字元代表
┏━━━━━━━━━┳━━━━━━━━┓
┃FA_RDONLY 只讀文件┃FA_LABEL 卷標號┃
┃FA_HIDDEN 隱藏文件┃FA_DIREC 目錄 ┃
┃FA_SYSTEM 系統文件┃FA_ARCH 檔案 ┃
┗━━━━━━━━━┻━━━━━━━━┛
*/
size_t findFunc(void)
{
struct ffblk ff;
int flag=0;
int attrib=FA_DIREC;
size_t ct=0;
flag=findfirst("*",&ff,attrib);//返回0成功,-1失敗
while(flag==0){
ct++;
...//你的處理函數
flag=findnext(&ff);//返回0成功,-1失敗
}
return ct;
}

I. 用C/C++如何遍歷某文件夾內的所有文件

C語言一般使用findfirst和findnext函數。
函數原型:
int findfirst(char *fname,struct ffblk *ptr,int attrib)

int findnext(struct ffblk *ffblk);

J. C語言:如何遍歷指定的文件夾(可以包括子文件夾)中的每一個文件名

Function SearchFiles(Path As String, FileType As String)
Dim Files() As String '文件路徑
Dim Folder() As String '文件夾路徑
Dim a, b, c As Long
Dim sPath As String

sPath = Dir(Path & FileType) '查找第一個文件

Do While Len(sPath) '循環到沒有文件為止
a = a + 1
ReDim Preserve Files(1 To a)
Files(a) = Path & sPath '將文件目錄和文件名組合,並存放到數組中
List1.AddItem Files(a) '加入list控制項中
sPath = Dir '查找下一個文件
DoEvents '讓出控制權
Loop

sPath = Dir(Path & "\", vbDirectory) '查找第一個文件夾

Do While Len(sPath) '循環到沒有文件夾為止
If Left(sPath, 1) <> "." Then '為了防止重復查找
If GetAttr(Path & "\" & sPath) And vbDirectory Then '如果是文件夾則。。。。。。
b = b + 1
ReDim Preserve Folder(1 To b)
Folder(b) = Path & sPath & "\" '將目錄和文件夾名稱組合形成新的目錄,並存放到數組中
End If
End If
sPath = Dir '查找下一個文件夾
DoEvents '讓出控制權
Loop

For c = 1 To b '使用遞歸方法,遍歷所有目錄
SearchFiles Folder(c), FileType
Next

End Function

Private Sub Command1_Click() '調用
SearchFiles "e:\", "*.exe"
End Sub

熱點內容
餐館許可證編號密碼是什麼 發布:2024-10-10 18:17:52 瀏覽:269
我的世界網易電腦版怎麼玩手機伺服器 發布:2024-10-10 18:06:16 瀏覽:28
客戶伺服器怎麼調查 發布:2024-10-10 17:56:05 瀏覽:35
軟體反編譯教程 發布:2024-10-10 17:50:14 瀏覽:12
uc瀏覽器android 發布:2024-10-10 17:50:13 瀏覽:16
字母大小寫編程 發布:2024-10-10 17:25:40 瀏覽:632
安卓大陸用戶怎麼玩傳說對決 發布:2024-10-10 17:11:56 瀏覽:432
上傳照片登錄 發布:2024-10-10 17:00:27 瀏覽:829
用友nc伺服器的ip地址是什麼 發布:2024-10-10 17:00:27 瀏覽:293
雲伺服器雙線 發布:2024-10-10 16:52:18 瀏覽:164