天各一方 |
2012-07-18 21:08 |
ASP程序中网站地图生成代码
为了方便访客访问,很多网站都有一个网站地图,下面的代码可以帮你轻松生成你的网站地图。 将下面的代码保存为sitemap.asp,然后放到网站根目录下,设定要生成的文件夹名字,运行,就OK了。
- <%
' sitemap.asp session("count")=0 '你的域名 session("server")="http://你的网站地址" '制作生成SiteMap.xml的目录,后面不要带"/",大小写敏感 vDir = "/XXXXX" '设置你想要GOOGLE收录的文件文件名扩展名 Extensions = Array("png","gif","jpg","jpeg","zip","pdf","ps","html","htm","asp","php","wk1","wk2","wk3","wk4","wk5","wki","wks","wku","lwp","mw","xls","ppt","doc","swf","wks","wps","wdb","wri","rtf","ans","txt","shtml","shtm") '需要过滤的目录 PathExclusion=Array("\admin","\_vti_cnf","_vti_pvt","_vti_log","cgi-bin","aspnet_client") set objfso = CreateObject("Scripting.FileSystemObject") root = Server.MapPath(vDir) 'response.End '创建文件 objfso.createtextfile(server.mappath("/sitemap.xml")) response.write "<p><strong style='font-family:arial,sans-serif;font-size=12'>创建SiteMap.xml成功!</strong><I style='font-family:arial,sans-serif;font-size=12'> "&server.mappath("/sitemap.xml")&" </I></p>" response.write "<p><strong style='font-family:arial,sans-serif;font-size=12'>正在索引文件,请稍侯...</strong></p>" Set openfileobj=objfso.opentextfile(server.mappath("/sitemap.xml"),8) openfileobj.writeline"<?xml version='1.0' encoding='UTF-8'?>" openfileobj.writeline"<urlset xmlns='http://www.google.com/schemas/sitemap/0.84'>" Set objFolder = objFSO.GetFolder(root) Set colFiles = objFolder.Files For Each objFile In colFiles openfileobj.writeline getfilelink(objFile.Path,objfile.dateLastModified) Next ShowSubFolders(objFolder) openfileobj.writeline "</urlset>" response.write "<p><strong style='font-family:arial,sans-serif;font-size=12'>SiteMap.xml生成完毕,共<span style=color:#FF0000> "&session("count")&" </span>个文件被索引</strong></p>" set fso = nothing Sub ShowSubFolders(objFolder) Set colFolders = objFolder.SubFolders For Each objSubFolder In colFolders if folderpermission(objSubFolder.Path) then openfileobj.writeline getfilelink(objSubFolder.Path,objSubFolder.dateLastModified) Set colFiles = objSubFolder.Files For Each objFile In colFiles openfileobj.writeline getfilelink(objFile.Path,objFile.dateLastModified) Next ShowSubFolders(objSubFolder) end if Next End Sub Function getfilelink(file,datafile) file=replace(file,root,"") file=replace(file,"\","/") If FileExtensionIsBad(file) then Exit Function if month(datafile)<10 then filedatem="0" if day(datafile)<10 then filedated="0" filedate=year(datafile)&"-"&filedatem&month(datafile)&"-"&filedated&day(datafile) getfilelink = "<url>"&Chr("10")&"<loc>"&server.htmlencode(session("server")&vDir&file)&"</loc>"&Chr("10")&"<lastmod>"&filedate&"</lastmod>"&Chr("10")&"<changefreq>daily</changefreq>"&Chr("10")&"<priority>1.0</priority>"&Chr("10")&"</url>" session("count")=session("count")+"1" Response.Flush End Function Function Folderpermission(pathName) Folderpermission =True for each PathExcluded in PathExclusion if instr(ucase(pathName),ucase(PathExcluded))>0 then Folderpermission = False exit for end if next End Function Function FileExtensionIsBad(sFileName) Dim sFileExtension, bFileExtensionIsValid, sFileExt if len(trim(sFileName)) = 0 then FileExtensionIsBad = true Exit Function end if sFileExtension = right(sFileName, len(sFileName) - instrrev(sFileName, ".")) bFileExtensionIsValid = false for each sFileExt in extensions if ucase(sFileExt) = ucase(sFileExtension) then bFileExtensionIsValid = True exit for end if next FileExtensionIsBad = not bFileExtensionIsValid End Function %>
|
|