導航:首頁 > 文件類型 > 遍歷文件夾vc

遍歷文件夾vc

發布時間:2023-02-26 12:11:50

① 請我如何c語言遍歷文件

現成的沒有,給你參考下:

c++:
1、CFileFind類實現遍歷文件,FindNext()查找下一個文件
2、比較文件後綴,符合要求就寫入txt

有不懂得話加Q:2311776177!

② 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) '加入控制項中
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

③ VC++程序如何遍歷一個文件夾下面的所有子文件夾下的文件

find(char * lpPath)
{
char szFind[MAX_PATH];
WIN32_FIND_DATA FindFileData;
strcpy(szFind,lpPath);
strcat(szFind,"*.*");
HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
if(INVALID_HANDLE_VALUE == hFind) return;
while(TRUE)
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(FindFileData.cFileName[0]!='.')
{
strcpy(szFile,lpPath);
strcat(szFile,"");
strcat(szFile,FindFileData.cFileName);
find(szFile);
}
}
else
{
cout << FindFileData.cFileName;
}
if(!FindNextFile(hFind,&FindFileData)) break;
}
FindClose(hFind);
}

#include <afxwin.h>
#include <iostream>

using namespace std;

void Recurse(LPCTSTR pstr)
{
CFileFind finder;

// build a string with wildcards

CString strWildcard(pstr);
strWildcard += _T("\\*.*");

// start working for files

BOOL bWorking = finder.FindFile(strWildcard);

while (bWorking)
{
bWorking = finder.FindNextFile();

// skip . and .. files; otherwise, we'd

// recur infinitely!

if (finder.IsDots())
continue;

CString sFileName = finder.GetFileName();
cout << (LPCTSTR)sFileName << endl;//輸出查找文件夾下的所有文件名

}

finder.Close();
}

int main()
{
if (!AfxWinInit(GetMoleHandle(NULL), NULL, GetCommandLine(), 0))//初始化MFC

cout << "panic!" << endl;
else
Recurse(_T("C:"));
return 0;
}

④ 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;//返迴文件總個數
}

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

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

⑥ 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相關的資料

熱點內容
iphone手機文件保存在哪裡 瀏覽:817
解壓文件後要刷新 瀏覽:786
cc資料庫怎麼獲得時間 瀏覽:226
ug3d硬料開出怎麼編程 瀏覽:151
如何獲取文件Linux命令 瀏覽:981
大智慧軟體哪個版本最好 瀏覽:698
狼人殺自動主持app叫什麼 瀏覽:949
checkbox怎麼綁定資料庫 瀏覽:945
編程怎麼設置一分鍾開燈 瀏覽:754
如何把桌面文件發送到自己郵箱 瀏覽:498
校園網站怎麼看選修的課 瀏覽:59
大數據專業哪個最好 瀏覽:467
一個文件內容替換另一個文件 瀏覽:288
ios8最好的版本 瀏覽:400
錄屏決定文件大小的是什麼 瀏覽:322
數據用不了是哪裡壞掉了 瀏覽:310
百度網盤文件傳輸格式 瀏覽:455
蘋果系統txt文件 瀏覽:629
家裝網站怎麼設計 瀏覽:202
adc指令微程序 瀏覽:487

友情鏈接