FSO操作类asp创建文件夹
作者:wang 日期:2009-12-15
FSO操作类
cls_FileObj.asp
VBScript code
<%Class clsFileObj Private Fso '对象 Public FsoObj '公共接口对象 '初始化,构造函数 Private Sub Class_Initialize Set Fso = Server.CreateObject("Scripting.FileSystemObject") Set FsoObj = Fso End Sub '结束,释构函数 Private Sub Class_Terminate Set Fso = Nothing Set FsoObj = Nothing End Sub '------------------------------------------------判断文件夹是否存在-------- '存在则返回True,否则返回False '参数FolderDir为文件的绝对路径 Public Function IsFolderExists(FolderDir) If Fso.FolderExists(FolderDir) Then IsFolderExists = True Else IsFolderExists = False End If End Function '------------------------------------------------创建---------------------- '建一个新文件夹 '参数NewFolderDir为要创建的文件夹绝对路径 Public Sub CreateFolderB(NewFolderDir) If IsFolderExists(NewFolderDir) Then Exit Sub Fso.CreateFolder (NewFolderDir) End Sub '------------------------------------------------遍列文件------------------ Function FileItem(FolderDir) '文件夹里的文件集合 'FolderDir 为文件夹绝对路径 If IsFolderExists(FolderDir) = False Then FileItem = False Exit Function End If Dim FileObj, FileerList, f Set FileObj = Fso.GetFolder(FolderDir) Set FileList = FileObj.Files FileItem = FileObj.Files.Count '文件总数 For Each f In FileList FileItem = FileItem & "|" & f.name Next Set FileList = Nothing Set FileObj = Nothing End Function '保证路径完整性 '成功创建或目录完整返回True,否则返回False 'strPath:目录相对路径 Public Function CheckPath(strPath) On Error Resume Next '创建FSO对象 Dim strCheckPath '去掉头尾“/” if strPath<>"" Then If Asc(Mid(strPath, Len(strPath), 1)) = 47 Then strPath = Left(strPath, Len(strPath) - 1) End If If Asc(Mid(strPath, 1, 1)) = 47 Then strPath = Right(strPath, Len(strPath) - 1) End If Else CheckPath=False Exit Function End if '利用数组保存文件夹 Arr_strPath = Split(strPath, "/") strCheckPath = Server.MapPath("/") For i = 0 To UBound(Arr_strPath) If Arr_strPath(i) <> "" Then strCheckPath = strCheckPath & "\" & Arr_strPath(i) If Not IsFolderExists(strCheckPath) Then '如果不存在就创建 CreateFolderB(strCheckPath) End If End If Next If Not IsFolderExists(Server.Mappath("/" & strPath)) Then CheckPath = False Else CheckPath = True End if End FunctionEnd Class%>
调用测试:
VBScript code
<% '文件生成操作开始 strFilePath = "/HTML/CCTV/" Set FileObj = New clsFileObj With FileObj '验证目录完整性 blnTemp = .CheckPath(strFilePath) if blnTemp Then Response.write "创建文件夹“" & strFilePath &"“成功!
" Else Response.write "创建路径出错,没有权限" Response.End End if End With Set FileObj=Nothing%>
cls_FileObj.asp
VBScript code
<%Class clsFileObj Private Fso '对象 Public FsoObj '公共接口对象 '初始化,构造函数 Private Sub Class_Initialize Set Fso = Server.CreateObject("Scripting.FileSystemObject") Set FsoObj = Fso End Sub '结束,释构函数 Private Sub Class_Terminate Set Fso = Nothing Set FsoObj = Nothing End Sub '------------------------------------------------判断文件夹是否存在-------- '存在则返回True,否则返回False '参数FolderDir为文件的绝对路径 Public Function IsFolderExists(FolderDir) If Fso.FolderExists(FolderDir) Then IsFolderExists = True Else IsFolderExists = False End If End Function '------------------------------------------------创建---------------------- '建一个新文件夹 '参数NewFolderDir为要创建的文件夹绝对路径 Public Sub CreateFolderB(NewFolderDir) If IsFolderExists(NewFolderDir) Then Exit Sub Fso.CreateFolder (NewFolderDir) End Sub '------------------------------------------------遍列文件------------------ Function FileItem(FolderDir) '文件夹里的文件集合 'FolderDir 为文件夹绝对路径 If IsFolderExists(FolderDir) = False Then FileItem = False Exit Function End If Dim FileObj, FileerList, f Set FileObj = Fso.GetFolder(FolderDir) Set FileList = FileObj.Files FileItem = FileObj.Files.Count '文件总数 For Each f In FileList FileItem = FileItem & "|" & f.name Next Set FileList = Nothing Set FileObj = Nothing End Function '保证路径完整性 '成功创建或目录完整返回True,否则返回False 'strPath:目录相对路径 Public Function CheckPath(strPath) On Error Resume Next '创建FSO对象 Dim strCheckPath '去掉头尾“/” if strPath<>"" Then If Asc(Mid(strPath, Len(strPath), 1)) = 47 Then strPath = Left(strPath, Len(strPath) - 1) End If If Asc(Mid(strPath, 1, 1)) = 47 Then strPath = Right(strPath, Len(strPath) - 1) End If Else CheckPath=False Exit Function End if '利用数组保存文件夹 Arr_strPath = Split(strPath, "/") strCheckPath = Server.MapPath("/") For i = 0 To UBound(Arr_strPath) If Arr_strPath(i) <> "" Then strCheckPath = strCheckPath & "\" & Arr_strPath(i) If Not IsFolderExists(strCheckPath) Then '如果不存在就创建 CreateFolderB(strCheckPath) End If End If Next If Not IsFolderExists(Server.Mappath("/" & strPath)) Then CheckPath = False Else CheckPath = True End if End FunctionEnd Class%>
调用测试:
VBScript code
<% '文件生成操作开始 strFilePath = "/HTML/CCTV/" Set FileObj = New clsFileObj With FileObj '验证目录完整性 blnTemp = .CheckPath(strFilePath) if blnTemp Then Response.write "创建文件夹“" & strFilePath &"“成功!
" Else Response.write "创建路径出错,没有权限" Response.End End if End With Set FileObj=Nothing%>
评论: 0 | 引用: 0 | 查看次数: 3380
发表评论