FileSystemObject组件实例教程

本例主要讲述如何应用FileSystemObject组件新建、复制和移动文件及文件夹、读写文本文件、创建文件和文件夹对象并返回它们的一些属性、显示驱动器列表等等,代码如下:
<%
Option Explicit
'On error resume next
Dim objFSO,drive
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Dim strPath,strFolder1,strFolder2,strFile1,strFile2,strFile3,strFile4
''''''''''''''''''''''''''''''''''''
'    设定路径
''''''''''''''''''''''''''''''''''''
strPath = Server.MapPath(".")
strFolder1 = strPath & "\01"
strFolder2 = strPath & "\02"
strFile1 = strFolder1 & "\a.txt"
strFile2 = strFolder1 & "\b.txt"
strFile3 = strFolder2 & "\a.txt"
strFile4 = strFolder2 & "\b.txt"
''''''''''''''''''''''''''''''''''''
'    创建文件文件夹
'     ├CreateFolder(folderpath)
'     ├CreateTextFile(filepath[,overwrite]
'             overwrite:true允许覆盖;@false不允许覆盖
''''''''''''''''''''''''''''''''''''
objFSO.CreateFolder(strFolder1)
objFSO.CreateFolder(strFolder2)
objFSO.CreateTextFile(strFile1)
objFSO.CreateTextFile(strFile2)
objFSO.CreateTextFile(strFile3)
objFSO.CreateTextFile(strFile4)
''''''''''''''''''''''''''''''''''''
'    对文本文件进行读写操作
'     ├OpenTextFile(filepath[,Iomode[,create]])
'             Iomode:@1只读;2可写覆盖所有内容;8只写
'             create:true自行建立;@false不自行建立
''''''''''''''''''''''''''''''''''''
Dim objTS
Set objTS = objFSO.OpenTextFile(strFile1,8)
objTS.WriteLine("你好!")
objTS.WriteLine("最后更新" & now())
objTS.close
Set objTS = nothing
''''''''''''''''''''''''''''''''''''
'    进行一些复制、移动、删除操作
'     ├CopyFolder oldfolderpath,newfolderpath[,overwrite]
'     ├CopyFile oldfilepath,newfilepath[,overwrite]
'     ├MoveFolder oldfolderpath,newfolderpath
'     ├MoveFile oldfilepath,newfilepath
'     ├DeleteFolder folderpath
'     ├DeleteFile filepath
''''''''''''''''''''''''''''''''''''
objFSO.CopyFolder strFolder2,strPath & "\03"
objFSO.CopyFile strFile3,strPath & "\03" & "\c.txt"
objFSO.MoveFolder strPath & "\03",strFolder1 & "\03"
objFSO.MoveFile strFile4,strFolder1 & "\03" & "\d.txt"
objFSO.DeleteFile strFile3
objFSO.DeleteFolder strFolder2
''''''''''''''''''''''''''''''''''''
'    判断文件文件夹是否存在并返回信息
'     ├FileExists(filepath)
'     ├FolderExists(folderpath)
''''''''''''''''''''''''''''''''''''
Dim objFolder,objFile,strFolder,strFile
If objFSO.FileExists(strFile1) then
    Set objFile = objFSO.GetFile(strFile1)
    Response.write objFile.Name & "<br>"
    Response.write "属性:" & objFile.Attributes & "<br>" '0普通;1只读;2隐藏;4系统;
    Response.write "创建日期:" & objFile.DateCreated & "<br>"
    Response.write "访问日期:" & objFile.DateLastAccessed & "<br>"
    Response.write "最后修改:" & objFile.DateLastModified & "<br>"
    Response.write "文件路径:" & objFile.Path & "<br>"
    Response.write "文件大小:" & objFile.Size & "<br>"
    Response.write "父文件夹:" & objFile.ParentFolder & "<br>"  '可返回一个Folder对象
    Set objFile = nothing
End if
If objFSO.FolderExists(strFolder1) then
    Set objFolder = objFSO.GetFolder(strFolder1)
    Response.write objFolder.Name & "<br>"
    Response.write "属性:" & objFolder.Attributes & "<br>" '0普通;1只读;2隐藏;4系统;
    Response.write "创建日期:" & objFolder.DateCreated & "<br>"
    Response.write "访问日期:" & objFolder.DateLastAccessed & "<br>"
    Response.write "最后修改:" & objFolder.DateLastModified & "<br>"
    Response.write "路径:" & objFolder.Path & "<br>"
    Response.write "大小:" & objFolder.Size & "<br>"
    Response.write "父文件夹:" & objFolder.ParentFolder & "<br>"  '可返回一个Folder对象
    Response.write "所在分区:" & objFolder.Drive & "<br>"
    Response.write "根文件夹:" & objFolder.IsRootFolder & "<br>"
    'Response.write "子 文 件:" & objFolder.Files & "<br>"  '可返回一个File对象
    'Response.write "子 文 件:" & objFolder.SubFolders & "<br>"  '可返回一个Folder对象
    Response.write "子文件夹:" & "<br>"
    For each strFolder in objFolder.SubFolders
        Response.write strFolder.Name & "<br>"
    Next
    Response.write "子文件:" & "<br>"
    For each strFile in objFolder.Files
        Response.write strFile.Name & "<br>"
    Next
    Set objFolder = nothing
End if
''''''''''''''''''''''''''''''''''''
'    驱动器列表
''''''''''''''''''''''''''''''''''''
'For each drive in objFSO.drives
'    Response.write drive.driveLetter & "<br>"
'    Response.write drive.drivetype & "<br>"
'    Response.write drive.VolumeName & "<br>"
'    Response.write drive.TotalSize & "<br>"
'    Response.write drive.Availablespace & "<br>"
'    Response.write drive.FileSystem & "<br>"
'Next
''''''''''''''''''''''''''''''''''''
'    举例:返回根目录文件夹
''''''''''''''''''''''''''''''''''''
'Response.write "E盘根目录文件夹:" & "<br>"
'Set objFolder = objFSO.GetFolder("E:\")
'For each strFolder in objFolder.SubFolders
'    Response.write strFolder.Name & "<br>"
'Next
'Set objFolder = nothing

Set objFSO = nothing
%>


评论: 1 | 引用: 0 | 查看次数: 4151
发表评论
登录后再发表评论!