| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1201 人关注过本帖
标题:随机读取文件名
只看楼主 加入收藏
hebss
Rank: 2
等 级:论坛游民
帖 子:19
专家分:23
注 册:2009-10-12
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:12 
随机读取文件名
文件夹下的名字不是根据数据库生成的,并且多为汉字,后缀为htm,请问怎样才能随机读取5条文件名?文件夹内都是htm文件,求ASP代码
搜索更多相关主题的帖子: 随机 文件名 
2010-12-07 15:07
hams
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:18
帖 子:912
专家分:3670
注 册:2008-7-30
收藏
得分:0 
for i=1 to 5
判断记录是否到底,如果到底,返回记录头
读一条记录
随机数,并判断是否读取
如果不读取,i=i-1
next

俺不高手,俺也是来学习的。
俺的意见不一定就对,当你不认同时请点忽视按钮。
当走到十字路口不知该如何走时,可在论坛问下路,但你若希望别人能一路把你送到目的地,显然是不现实的,因为别人也有自己要走的路。
2010-12-07 17:23
hebss
Rank: 2
等 级:论坛游民
帖 子:19
专家分:23
注 册:2009-10-12
收藏
得分:0 
好像只有数字的才可以吧,或者说我没弄好

[url=www.]www.[/url]   [url=www.]www.[/url]
2010-12-08 10:08
hams
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:18
帖 子:912
专家分:3670
注 册:2008-7-30
收藏
得分:0 
字符串就读不出来么

俺不高手,俺也是来学习的。
俺的意见不一定就对,当你不认同时请点忽视按钮。
当走到十字路口不知该如何走时,可在论坛问下路,但你若希望别人能一路把你送到目的地,显然是不现实的,因为别人也有自己要走的路。
2010-12-08 11:28
hebss
Rank: 2
等 级:论坛游民
帖 子:19
专家分:23
注 册:2009-10-12
收藏
得分:0 
麻烦hams版主看下
<%
Sub AllFiles(dirPath)
On Error Resume Next
if right(dirPath,1)<>"\" then dirPath=dirPath&"\"
Set FSO = CreateObject("scripting.filesystemobject")
Set f = FSO.GetFolder(dirPath)
Set fs = f.files
dim Str,Arr,a,Content,url,ShowContent
For Each fileN in fs
if  RegExpfind(".html",fileN.name) then
Str=fileN.name
 response.write str
 response.write "<br>"
end if
Next
Set FSO = Nothing
End sub
Function RegExpfind(patrn, strng)
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
RegExpfind = regEx.Test(strng)
End Function
AllFiles(server.mappath(""))
%>
以上代码可以读出所有html文件,就是不知道该添加什么代码才能做到随机读取5条

[ 本帖最后由 hebss 于 2010-12-8 13:40 编辑 ]

[url=www.]www.[/url]   [url=www.]www.[/url]
2010-12-08 13:35
sky222
Rank: 4
等 级:业余侠客
威 望:1
帖 子:152
专家分:225
注 册:2010-3-11
收藏
得分:5 
<%
Sub AllFiles(dirPath)
On Error Resume Next
if right(dirPath,1)<>"\" then dirPath=dirPath&"\"
Set FSO = CreateObject("scripting.filesystemobject")
Set f = FSO.GetFolder(dirPath)
Set fs = f.files
dim Str,Arr,a,Content,url,ShowContent
i=0
For Each fileN in fs
i=i+1
if  RegExpfind(".html",fileN.name) then
Str=fileN.name
response.write str
response.write "<br>"
end if
Next
if i>4 then exit for
Set FSO = Nothing
End sub
Function RegExpfind(patrn, strng)
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
RegExpfind = regEx.Test(strng)
End Function
AllFiles(server.mappath(""))
%>


[ 本帖最后由 sky222 于 2010-12-8 14:20 编辑 ]
2010-12-08 14:18
hams
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:18
帖 子:912
专家分:3670
注 册:2008-7-30
收藏
得分:0 
楼上的思路是对的,但还差两个东西
1、没有随机数
2、没有考虑到,如果记录数不足5个怎么处理?就算是记录超过5位,但经过随机数选择之后,就有可能不够。

俺不高手,俺也是来学习的。
俺的意见不一定就对,当你不认同时请点忽视按钮。
当走到十字路口不知该如何走时,可在论坛问下路,但你若希望别人能一路把你送到目的地,显然是不现实的,因为别人也有自己要走的路。
2010-12-08 15:29
hams
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:18
帖 子:912
专家分:3670
注 册:2008-7-30
收藏
得分:0 
另外
if i>4 then exit for
放错了地方,应该要放在FOR内

俺不高手,俺也是来学习的。
俺的意见不一定就对,当你不认同时请点忽视按钮。
当走到十字路口不知该如何走时,可在论坛问下路,但你若希望别人能一路把你送到目的地,显然是不现实的,因为别人也有自己要走的路。
2010-12-08 15:31
hams
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:18
帖 子:912
专家分:3670
注 册:2008-7-30
收藏
得分:15 
<%
Sub AllFiles(dirPath)
i=0
Randomize
On Error Resume Next
if right(dirPath,1)<>"\" then dirPath=dirPath&"\"
Set FSO = CreateObject("scripting.filesystemobject")
Set f = FSO.GetFolder(dirPath)
Set fs = f.files
dim Str,Arr,a,Content,url,ShowContent
For Each fileN in fs
  if  RegExpfind(".html",fileN.name) then
    Str=fileN.name
    if Rnd>0.6 then
      i=i+1
      response.write str&"<br>"
    end if
  end if
  if i>4 then exit for
Next
Set FSO = Nothing
End sub

Function RegExpfind(patrn, strng)
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
RegExpfind = regEx.Test(strng)
End Function
AllFiles(server.mappath(""))
%>
以上代码同样没有解决不足五位的情况,思路是把读文件的指针恢复到开头,具体代码想不起来了,实在不行就做个循环把文件名再读一次,再到够5个。

俺不高手,俺也是来学习的。
俺的意见不一定就对,当你不认同时请点忽视按钮。
当走到十字路口不知该如何走时,可在论坛问下路,但你若希望别人能一路把你送到目的地,显然是不现实的,因为别人也有自己要走的路。
2010-12-08 15:41
hebss
Rank: 2
等 级:论坛游民
帖 子:19
专家分:23
注 册:2009-10-12
收藏
得分:0 
谢谢hams版主,已经成功了,同时也感谢sky222。

[url=www.]www.[/url]   [url=www.]www.[/url]
2010-12-08 15:56
快速回复:随机读取文件名
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012676 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved