读桥埋困取的代码方式如下:
intmain()
{
longfile
struct_finddata_tfind
_chdir("d:\\")
if((file=_findfirst("*.*",&find))==-1L)
{
printf("空白!\n")
exit(0)
}
printf("%s\n",find.name)
while(_findnext(file,&find)==0)
{
printf("%s\n",find.name)
}
_findclose(file)
return0
}
1、如果是在window环境下,可以用一敏念下方法:
使用stdlib.h头文件声明的system()函数,调用液祥系统命令dir,把c:目录下文件列表写入文件dir.txt中
2、使用dirent.h头文件中声明的opendir(),readdir()函数;
3、如果没有dirent.h,可以使用io.h头文件中声明的_findfirst(),_findnext()函数:
C语谈明旦言本身没提供象dir_list()
函数
列
某
目录
所
文件
利用C语言
几
目录函数
自
编写
dir_list()函数
首先
文件dos.h定义
find_t结构
描述DOS
文件信息
包括文件名、
间、
期、
属性
其
C编译程序库
_dos_findfirst()
_dos_findnext()
两
函数
利用
找
某
目录
符合查找要求
第
或
文件
dos_findfirst()函数
三
参数
第
参数指明要查找
文件名
例
用
*.*
指明要查找某
目录
所
文件
第二
参数指明要查找
文件属性
例
指明
查找隐含文件或
目录
第三
参数
指向
find_t变量
指针
查找
文件
关信息
存放
该变量
dos_findnext()函数
相应
目录
继续查找由_dos_findfirst()函数含扰
第
参数指明
文件
_dos_findnext()函数
参数
同
指向
find_t变量
指针
查找
刚文件
关信息同
存放
该变量
利用
述两
函数
find_t结构
遍历磁盘
某
目录
并列
该目录
所
文件
请看
例:
#include
<stdio.h>
#include
<direct.h>
#include
<dos.h>
#include
<malloc.h>
#include
<memory.h>
#include
<string.h>
typedef
struct
find_t
FILE_BLOCK
void
main(void)
void
main(void){FILE_BLOCK
f-block
/*
Define
the
find_t
structure
variable
*
/
int
ret_code
/
*
Define
a
variable
to
store
the
return
codes
*
/
/
*
Use
the
"*.*"
file
mask
and
the
0xFF
attribute
mask
to
list
all
files
in
the
directory,
including
system
files,
hidden
files,
and
subdirectory
names.
*
/
ret_code
=
_dos_findfirst("槐举
*.
*
",
0xFF,
&f_block)
/*
The
_dos_findfirst()
function
returns
a
0
when
it
is
successful
and
has
found
a
valid
filename
in
the
directory.
*
/
while
(ret_code
==
0){/*
the
file's
name
*
/
printf("
%-12s
,
f_block,
name)
/
*
Use
the
-dos_findnext()
function
to
look
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)