软件教程

ASP遍历所有文件及文件夹的方法

来源: 本站    类别: 软件教程    日期: 2016/2/2

 

本文章给大家介绍一篇asp中读取文件夹下的所有文件代码有需要的朋友可参考。
 代码如下

<%
'建立文件系统对象 
set file_system=createobject("scripting.filesystemobject") 
'建立建立当前目录对象 
set cur_folder=file_system.getfolder(c_path) 
'建立当前目录的子目录对象集合 
set sub_folders=cur_folder.subfolders 
'对子目录集合进行遍历 
for each each_sub_folder in sub_folders 
sub_folder_name=each_sub_folder.name '目录名 
sub_folder_path=c_path&""&each_sub_folder.name 
set cur_sub_folder=file_system.getfolder(sub_folder_path) 
'建立检索目录下的文件对象集合 
set sub_files=cur_sub_folder.files 
'遍历文件对象集合 
for each each_file in sub_files 
name=each_file.name'取文件名 
next

next 


*********************************************************************************************************


使用递归

set file_system=createobject("scripting.filesystemobject")

sub getFile(c_path) 
set cur_folder=file_system.getfolder(c_path) 
set sub_folders=cur_folder.subfolders

for each each_sub_folder in sub_folders 
set sub_files=cur_sub_folder.files 
for each each_file in sub_files 
name=each_file.name'取文件名 
next 
sub_folder_name=each_sub_folder.name '目录名 
sub_folder_path=c_path&""&each_sub_folder.name 
sub getFile(sub_folder_path) 
next

end sub


********************************************************************************


function search_folder(c_path) '目录检索函数 
'建立文件系统对象 
set file_system=createobject("scripting.filesystemobject") 
'建立建立当前目录对象 
set cur_folder=file_system.getfolder(c_path) 
'建立当前目录的子目录对象集合 
set sub_folders=cur_folder.subfolders 
'搜索当前目录中的文件 
p=search_file(c_path) 
'对子目录集合进行遍历 
for each each_sub_folder in sub_folders 
'确定子目录的属性为普通子目录 
sub_c_path=c_path&""&each_sub_folder.name 
'调用文件检索函数对当前子目录下的文件进行字符串匹配检索 
p=search_file(sub_c_path) 
'递归检索当前子目录的下一级目录 
p=search_folder(sub_c_path) 
next 
'清除所有服务器端对象 
set sub_files=nothing 
set each_sub_folder=nothing 
set sub_folders=nothing 
set cur_folder=nothing 
set file_system=nothing 
end if 
end function


**********************************************


set file_system=createobject("scripting.filesystemobject") 
set cur_folder=file_system.getfolder(c_path) 
'建立检索目录下的文件对象集合 
set sub_files=cur_folder.files 
'遍历文件对象集合 
for each each_file in sub_files 
name=each_file.name'取文件名 
next  
%>


相关文章


Copyright © 2004 - 2024 CNHWW Inc. All Rights Reserved
石家庄市征红网络科技有限公司版权所有 邮政编码:050051
服务电话:0311-85315152 13931185013 在线客服QQ:81447932 / 81447933 邮箱: cnhww@163.com