⑴ ASP遍歷文件問題
<%
sub bianli(path)
dim fso
dim objFolder
dim objSubFolder
dim objFile
set fso=server.CreateObject("scripting.filesystemobject")
on error resume next
set objFolder=fso.GetFolder(path)
for each objFile in objFolder.Files
Response.Write path & "\\" & objFile.name & "<br>---"
next
Response.Write "<p>"
for each objSubFolder in objFolder.Subfolders
nowpath=path & "\\" & objSubFolder.name
Response.Write nowpath & "<br>"
bianli nowpath
next
set objFolder=nothing
set fso=nothing
end sub
bianli "D:\shuaigua\56bohai"
%>
看明白沒有?你要先顯示當前文件夾(注意當前)中的文件集合,再遍歷文件夾中的子文件夾集合。而你的做法則是顯示子文件夾(注意子字)中的文件集合,再去遍歷子文件夾集合,這樣的話一開始的那個文件夾(即D:\shuaigua\56bohai)的文件就無法顯示了!
⑵ 一個asp遍歷文件夾的問題
那些格式的文件打不開,可能你電腦沒有相應打開的軟體吧,找找再安裝上那些相應的軟體,一樣也可以打開的。。
估計是你自己電腦的軟體匹配問題了,因為我試過你的代碼是可以的。。。
⑶ 請教ASP中FSO遍歷文件的問題
Javascript遍歷文件夾下的文件?如果這都行的話,我就去遍歷QQ的根目錄,找到資料庫文件,然後下載……---當然,在客戶端,JS還是有FSO應用的。但是僅能遍歷客戶端的文件。但是也有很多限制,而且是通過ActiveXObject("Scripting.FileSystemObject")來創建。這里有一個枚舉客戶C盤文件的例子:<html>
<head></head>
<body>
<input type="button" value="枚舉C盤文件" onclick="init()">
<div id=t></div>
<script language="javascript">
function $(id){return document.getElementById(id);};
function init(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var folder = fso.GetFolder("C:\\");
var arr=[];
var fenum = new Enumerator(folder.Files);
for (var i = 0 ; !fenum.atEnd(); fenum.moveNext(),i++){
arr.push(fenum.item().Name);
}
$('t').innerHTML=arr.join('<br>')
}
</script>
</body>
</html>
⑷ ASP.NET如何遍歷伺服器端文件夾內的文件然後綁定資料庫
這個不是用sql來搞定的吧。
個人感覺應該用伺服器端代碼來實現。
DirectoryInfodirInfo=newDirectoryInfo("路徑");
//參數一:文件類型(如果所有文件就是*)
//參數二:是否查找目錄下的子目錄
FileSystemInfo[]files=dirInfo.GetFileSystemInfos("*.jpg",DirectoryInfo.GetFileSystemInfos);
DataGrid.DataSource=files;
DataGrid1.DataBind();
//剩下的,你只要前台把文件的個個屬性對應上就可以了。FileSystemInfo類有哪些屬性,建議去msdn上面查一下。
⑸ asp遍歷文件後,如何分頁顯示
只能提供下思路,代碼自己碼
我覺得可以用循環的方式累計文件數,把文件名存入數組,然後再根據文件數分頁輸出,這樣的方式在文件少的話還能勉強使用,文件多的話肯定效率太低了,還有一種辦法是你生成文件的時候把文件名插入到資料庫裡面,這樣每次讀取直接從資料庫讀取就好了。
⑹ asp遍歷某目錄下的所有文件夾和文件,分頁顯示
由於是從我程序里提取出來的,代碼里可能有些沒用的東西,你自己修改一下。
<%@ LANGUAGE = VBScript CodePage = 936%>
<%
Option Explicit
Response.Buffer = True
dim MaxPerPage,MyPath
dim CurrentPage,PageCounts,FolderCounts,path,goparent,pathurl,s_folderpath
dim obj_fso,obj_folder,s_folder
dim cname,ename
dim bg
MaxPerPage=20
MyPath="e:\音樂"
path=trim(request.querystring("path"))
CurrentPage=trim(request.querystring("page"))
if path="" or instr(path," ")>0 or instr(path,vbcrlf)>0 or instr(path,":")>0 or instr(path,"\")>0 then
path="/"
elseif right(path,1)<>"/" then
path=path&"/"
elseif left(path,1)<>"/" then
path="/"&path
end if
If CurrentPage<>"" And isNumeric(CurrentPage) Then
CurrentPage=Cint(CurrentPage)
Else
CurrentPage=1
End If
if path="/" then
goparent="根目錄"
else
goparent="<a href=""?path="&server.urlencode(left(path,instrrev(path,"/",len(path)-1)))&""">返回上級目錄"
end if
pathurl=server.urlencode(path)
's_folderpath=server.mappath(path)
s_folderpath=MyPath&replace(path,"/","\")
set obj_fso=server.createobject("scripting.filesystemobject")
%>
<html>
<head>
<title>文件管理</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<link rel="stylesheet" href="inc/style.css" type="text/css">
<head>
<body topmargin=0 leftmargin=3 rightmargin=3>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#E3EBF9">
<tr>
<td height="28"> 當前位置:<%=path%></td>
</tr>
</table><%
if obj_fso.folderexists(s_folderpath) then
%>
<table width="100%" border=0 cellpadding=0 cellspacing=0 bgcolor="#FFFFFF">
<tr>
<td width="258" valign="top">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#C8D6F0">
<tr height="28">
<td><b><%=goparent%></b></td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#E8F1FF" style="table-layout:fixed;"><%
dim s_classname,s_classpath,s_classurl
dim i
i=1
FolderCounts=0
set obj_folder=obj_fso.getfolder(s_folderpath)
for each s_folder in obj_folder.subfolders
s_classname=s_folder.name
s_classpath=path&s_classname
s_classurl=server.urlencode(s_classpath)
if FolderCounts mod 2=0 then
bg=" bgcolor='#ffffff'"
else
bg=""
end if
%>
<tr height="22" align="center"<%=bg%>>
<td align="left" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><a href="?path=<%=pathurl&server.urlencode(s_classname)%>" title="進入子目錄「<%=s_classname%>」"><%=s_classname%></a></td>
</tr><%
FolderCounts=FolderCounts+1
i=i+1
next
%>
<tr>
<td height=22 bgcolor="#C8D6F0">本目錄共有<b><%=FolderCounts%></b>個子目錄。</td>
</tr>
</form>
</table>
</td>
<td width="5"></td>
<td height="100%" valign="top">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#E8F1FF">
<tr height="28" bgcolor="#C8D6F0" align="center">
<td width="38%"><b>文件名</b></td>
<td width="9%"><b>文件大小</b></td>
<td width="20%"><b>最後修改時間</b></td>
</tr><%
dim startnum,TotleSize,s_file,s_filename,FileCounts,FileName,FileExt,s_pathurl
i=1
startnum=(CurrentPage-1)*MaxPerPage
TotleSize=0
for each s_file in obj_folder.files
FileCounts=FileCounts+1
next
if FileCounts mod MaxPerPage=0 then
PageCounts=FileCounts\MaxPerPage
else
PageCounts=FileCounts\MaxPerPage+1
end if
If CurrentPage<1 Then
CurrentPage=1
End If
if CurrentPage>PageCounts then
CurrentPage=PageCounts
end if
for each s_file in obj_folder.files
s_filename=s_file.name
s_pathurl=server.urlencode(path&s_filename)
if (i-1) mod 2=0 then
bg=" bgcolor='#ffffff'"
else
bg=""
end if
FileName=GetFileName(s_filename)
FileExt=GetFileExt(s_filename)
if i>startnum then
TotleSize=TotleSize+s_File.Size
%>
<tr height="22" align="center"<%=bg%>>
<td align="left"><%=s_file.name%></td>
<td><%=ByteNum(s_file.size)%></td>
<td><%=s_file.datelastmodified%></td>
</tr><%
end if
if i>startnum+MaxPerPage then
exit for
end if
i=i+1
next
%>
<tr>
<td height="22" colspan=3 align=right bgcolor="#C8D6F0"><%
if CurrentPage>1 then
response.write "<a href='?path="&pathurl&"&page="&(CurrentPage-1)&"'>上一頁</a> "
end if
response.write "本目錄共有"&obj_folder.files.count&"個文件 本頁文件為"&ByteNum(TotleSize)&"B 當前第 "
response.write "<select name='jtp' style='line-height:12px;border:none;height:12px;padding:0' onchange="&chr(34)&"window.location.href='?page='+(this.options.selectedIndex+1)+'&path="&pathurl&"'"&chr(34)&">"&vbcrlf
for i=1 to PageCounts
if i=CurrentPage then
response.write "<option selected>"&i&vbcrlf
else
response.write "<option>"&i&vbcrlf
end if
next
response.write "</select> 頁 共"&PageCounts&"頁"
if CurrentPage<PageCounts then
response.write "<a href='?path="&pathurl&"&page="&(CurrentPage+1)&"'>下一頁</a>"
end if
%>
</td>
</tr>
<tr>
<td height="100%" colspan=3 bgcolor="#F8FAFE"></td>
</tr>
</form>
</table>
</td>
</tr>
</table><%
set obj_folder=nothing
else
%>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#E8F1FF">
<tr>
<td height="80" align="center" bgcolor="#FFFFFF" style="color:#ff3333;font-weight:600">目錄不存在!</td>
</tr>
</table><%
end if
set obj_fso=nothing
%>
</center>
</body>
</html><%
function getname(s_string,s_clipchar)
n_strpos=instrrev(s_string,s_clipchar)
getname=lcase(right(s_string,len(s_string)-n_strpos))
end function
Function GetFileName(lpFullFileName)
Dim nDotPos
If InStr(lpFullFileName,".")=0 Then
GetFileName=lpFullFileName
Exit Function
End If
nDotPos=InStrRev(lpFullFileName,".")
GetFileName=Left(lpFullFileName,nDotPos-1)
End Function
Function GetFileExt(lpFullFileName)
Dim fnArray
If InStr(lpFullFileName,".")=0 Then
Exit Function
End If
fnArray=Split(lpFullFileName,".")
GetFileExt=fnArray(UBound(fnArray))
End Function
'--------格式文件尺寸顯示
Function ByteNum(num)
if Num=0 then
ByteNum="0K"
elseif Num<1024 then
ByteNum="1K"
else
ByteNum=formatNumber(int(Num/1024),0)&"K"
end if
' ByteNum=Num&" Byte"
'elseif Num<1048576 then
' ByteNum=formatNumber(int(Num/1024),0)&" KB"
'elseif Num<1073374812 then
' ByteNum=formatNumber(int(Num/1048576),0)&" MB"
'elseif Num<1073374812 then
'else
' ByteNum=formatNumber(int(Num/1073374812),0)&" GB"
'end if
End function
%>
=============================================
剛剛在實際應用中寫了個這樣的程序,代碼在辦公室。