返回列表 回复 发帖

用fso建立任意层深的子目录

作者:狗狗儿
FileSystemObject中有一个CreateFolder方法可以用来建立目录,但只能建立一层的目??
当我们想建
立很多层的目录(如/web/bbs/include/bak)时这个方法就无能为力了.为解决这个问题, 我写
了个BuildPath(strPath)过程,可以完美的解决这个问题.代码如下:
  1. Sub BuildPath(strPath)
  2. On Error Resume Next
  3. Dim nPos,fso,strFolder
  4. nPos = Len(Server.MapPath("/"))
  5. Set fso = CreateObject("Scripting.FileSystemObject")
  6. Do
  7.    nPos = InStr(nPos + 1,strPath,"/")
  8.    If nPos = 0 Then
  9.       strFolder = strPath
  10.    Else
  11.       strFolder = Left(strPath,nPos - 1)
  12.    End If
  13.    If fso.FolderExists(strFolder) Then
  14.        Response.Write "Err: Folder " & strFolder & " Existed.<br>"
  15.    Else
  16.        fso.CreateFolder(strFolder)
  17.        If Err Then
  18.           Response.Write err.description
  19.        Else
  20.           Response.Write "Folder " & strFolder & " Created.<br>"
  21.        End If
  22. End If
  23. Loop Until nPos = 0
  24. End Sub
  25. asp实例:
  26. <%
  27. 'BuildPath.asp
  28. 'Written By Googler
  29. 'www.Googler.uni.cc
  30. Option Explicit
  31. On Error Resume Next
  32. Dim StrPath
  33. strPath = Request("strPath")
  34. If strPath <> "" Then
  35. BuildPath Replace(Server.MapPath(strPath),"\","/")
  36. End If
  37. '---------------------------------------------------------------------------
  38. Sub BuildPath(strPath)
  39. On Error Resume Next
  40. Dim nPos,fso,strFolder
  41. nPos = Len(Server.MapPath("/"))
  42. Set fso = CreateObject("Scripting.FileSystemObject")
  43. Do
  44.    nPos = InStr(nPos + 1,strPath,"/")
  45.    If nPos = 0 Then
  46.       strFolder = strPath
  47.    Else
  48.       strFolder = Left(strPath,nPos - 1)
  49.    End If
  50.    If fso.FolderExists(strFolder) Then
  51.        Response.Write "Err: Folder " & strFolder & " Existed.<br>"
  52.    Else
  53.        fso.CreateFolder(strFolder)
  54.        If Err Then
  55.           Response.Write err.description
  56.        Else
  57.           Response.Write "Folder " & strFolder & " Created.<br>"
  58.        End If
  59. End If
  60. Loop Until nPos = 0
  61. End Sub
  62. %>
  63. <br>
  64. <hr>
  65. <form method=get>
  66. <input type=text name=strPath Lenght=15>
  67. <input type=submit value=Create...>
  68. </form>
复制代码

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表