ASP获取文件所在文件夹名称、复制文件、创建、删除文件夹代码


  1. <%



  2. '获取几级文件夹或文件名称

  3. arr=Split(Request.ServerVariables("SCRIPT_NAME"),"/")

  4. wenjianjia_name=arr(2)

  5. response.Write(wenjianjia_name&"<br />")



  6. '文件所在文件夹名称

  7. cur_URL = Server.MapPath(".")

  8. arr_curURL = split(cur_URL,"\")

  9. len_arr = ubound(arr_curURL)

  10. this_curURL= arr_curURL(len_arr)

  11. response.Write(this_curURL&"<br />")



  12. '文件名称

  13. cur_URL = Request.ServerVariables("url")

  14. arr_curURL = split(cur_URL,"/")

  15. len_arr = ubound(arr_curURL)

  16. this_curURL= arr_curURL(len_arr)

  17. response.Write(this_curURL&"<br />")



  18. folder="www.yuzhiguo.com"'需要创建的文件夹名称



  19. Call createfolder(folder)



  20. Function createfolder(folder)

  21. set fs=createobject("scripting.filesystemobject")

  22. url=server.mappath(folder)

  23. if Not fs.folderexists(url) then

  24. fs.createfolder(url)

  25. Response.write url&"创建成功!<br />"

  26. else

  27. Response.write url&"存在!<br />"

  28. end if

  29. End Function





  30. '-------------------复制文件--------

  31. TempSource= Server.MapPath("/yuzhiguo.jpg")'源文件

  32. TempEnd=Server.MapPath(folder&"/yuzhiguo.jpg")'移动目标地

  33. Call CopyFiles(TempSource,TempEnd)



  34. Function CopyFiles(TempSource,TempEnd)

  35. Dim FSO

  36. Set FSO = Server.CreateObject("Scripting.FileSystemObject")

  37. IF FSO.FileExists(TempEnd) then

  38. Response.Write "目标备份文件 <b>" & TempEnd & "</b> 已存在,请先删除!<br />"

  39. Set FSO=Nothing

  40. Exit Function

  41. End IF

  42. IF FSO.FileExists(TempSource) Then

  43. Else

  44. Response.Write "要复制的源数据文件 <b>"&TempSource&"</b> 不存在!<br />"

  45. Set FSO=Nothing

  46. Exit Function

  47. End If

  48. FSO.CopyFile TempSource,TempEnd

  49. Response.Write "已经成功复制文件 <b>"&TempSource&"</b> <b>"&TempEnd&"</b><br />"

  50. Set FSO = Nothing

  51. End Function



  52. '-------------------删除文件夹--------

  53. strFolder="www.yuzhiguo.com"'需要删除的文件夹名称

  54. Call DeleteFolder(strFolder)



  55. Function DeleteFolder(strFolder)

  56. '首选判断要建立的文件夹是否已经存在

  57. Dim strTestFolder, objFSO

  58. strTestFolder = Server.Mappath(strFolder)

  59. Set objFSO = CreateObject("Scripting.FileSystemObject")

  60. ' 检查文件夹是否存在

  61. If objFSO.FolderExists(strTestFolder) Then

  62. objFSO.DeleteFolder(strTestFolder)

  63. response.write "<font color=red>删除成功!</font><br />"

  64. Else

  65. response.write "<font color=red>该文件夹不存在!</font>"

  66. End If

  67. Set objFSO = Nothing

  68. End Function

  69. %>
posted @ 2011-09-23 17:18  #i小龙#  阅读(4177)  评论(0编辑  收藏  举报