当前位置:首页 » 知网查重 » c遍历目录

c遍历目录

发布时间: 2021-03-14 13:44:20

❶ 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

❷ C语言遍历文件目录

//没调试,简单写了下

int text()
{
DIR *pd = NULL;
struct dirent *ptr = NULL;

//打开目录,pd为该目录句柄
if ( (pd = opendir("d:/")) == NULL)
{
puts("open failed");
return -1;
}

//遍历目录
while ( (ptr = readdir(pd)) != NULL)
{
//输出目录下文件名
puts(ptr->d_name);
}

return 0;
}

❸ windows c 怎么遍历目录文件

1、操作系统中有相关的API函数,可以读取目录中所有的文件名字,以及时间属性信息,把这些信息读出来,直接依次遍历即可。
2、例程:
#include"stdio.h"
#include"io.h"
int main()
{
struct _finddata_t files;
int File_Handle;
int i=0;
File_Handle = _findfirst("c:/temp/*.txt",&files);
if(File_Handle==-1)
{
printf("error\n");
return 0;
}
do
{
printf("%s \n",files.name);
i++;
}while(0==_findnext(File_Handle,&files));
_findclose(File_Handle);
printf("Find %d files\n",i);
return 0;
}

❹ C语言遍历目录

vc6编译运行通过内容
#include"stdio.h"
#include"io.h"
int main()
{
struct _finddata_t files;
int File_Handle;
int i=0;

File_Handle = _findfirst("c:/temp/*.txt",&files);
if(File_Handle==-1)
{
printf("error\n");
return 0;
}
do
{
printf("%s \n",files.name);
i++;
}while(0==_findnext(File_Handle,&files));
_findclose(File_Handle);
printf("Find %d files\n",i);
return 0;
}

❺ 如何用C实现遍历文件夹

在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);
}

❻ C/C++中的readdir遍历目录中的子目录的问题

这个要正对不同的操作系统,不同的操作系统,文件节点的存放方式不一样,读取的内方式就不一容样了。
可以利用for循环,像Linux dir=opendir (dirname)打开文件夹,返回目录指针,dp=readdir(dir)利用读目录,返回一行行读取目录的文件结构体指针,指针中存的有文件属性,是文件,还是文件夹。
通过判断是文件或者文件夹:
如果是文件,就就输出文件名dp->name
否则,就是一个文件夹 继续dir1=opendir(dp->name),dp1=readdir(dir1)..
一直循环到判断不到目录了。
windows应该也有类似的函数吧,这个我就么有用过了,你找找。

❼ C语言遍历目录树下所有文件

完全可以啊 vs2012 CFile类可以搞定

❽ C语言如何实现遍历目录的功能

不同系统 使用的接口函数可能不同
Linux要用Linux接口
windows要用win api

基本思路就是用opendir打开目录
然后循环版readdir 直到null
如果要递归,那么对权于每个read到的文件夹 都要再调用一次遍历函数。

❾ windows下使用C/C++怎么遍历目录并读取目录下的文件列表

用C的函数实现,windows ,linux 都能用

#include<iostream>
#include<io.h>
#include<string>
usingnamespacestd;
voiddir(stringpath)
{
longhFile=0;
struct_finddata_tfileInfo;
stringpathName,exdName;
//\*代表要遍历所有的类型
if((hFile=_findfirst(pathName.assign(path).append("\*").c_str(),&fileInfo))==-1){
return;
}
do
{
//判断文件的属性是文件夹还是文件
cout<<fileInfo.name<<(fileInfo.attrib&_A_SUBDIR?"[folder]":"[file]")<<endl;
}while(_findnext(hFile,&fileInfo)==0);
_findclose(hFile);
return;
}
intmain()
{
//要遍历的目录
stringpath="E:\work\\test4";
dir(path);
system("pause");
return0;
}

❿ C语言如何遍历目录 (C++也可以) findfirst findnext怎么用

WIN32_FIND_DATAfd;
HANDLEhdFind;
hdFind=FindFirstFile(strDirPath,&fd);//strDirPath为目录路径
if(hdFind!=INVALID_HANDLE_VALUE)
{
do
{
//做你需要的操作。
}while(FindNextFile(hdFind,&fd));
}

热点内容
涂鸦论文 发布:2021-03-31 13:04:48 浏览:698
手机数据库应用 发布:2021-03-31 13:04:28 浏览:353
版面217 发布:2021-03-31 13:04:18 浏览:587
知网不查的资源 发布:2021-03-31 13:03:43 浏览:713
基金赎回参考 发布:2021-03-31 13:02:08 浏览:489
悬疑故事范文 发布:2021-03-31 13:02:07 浏览:87
做简单的自我介绍范文 发布:2021-03-31 13:01:48 浏览:537
战略地图参考 发布:2021-03-31 13:01:09 浏览:463
收支模板 发布:2021-03-31 13:00:43 浏览:17
电气学术会议 发布:2021-03-31 13:00:32 浏览:731