在 ASP 中读取文件以及如何在特定目录中的所有文件中搜索特定文本。






3.68/5 (26投票s)
2004年6月4日
2分钟阅读

144452

1557
本文介绍了使用 ASP 的 FileSystemObject,关于如何创建/修改/删除文件,以及一个在特定文件夹中的所有文件中查找特定字符串的示例。
引言
FileSystemObject 提供对计算机文件系统的访问。 使用 FileSystemObject 可以执行的一些常见任务是检查特定文件夹、驱动器或文件,以及创建/修改/删除文件(如果您具有适当的文件夹/文件权限)。
开始吧
首先,以下是描述如何使用 FileSystemObject 创建文本文件并写入一些文本的代码
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set a = fso.CreateTextFile("c:\testfile.txt", True)
    a.WriteLine("This is a test.")
    a.Close
    set a = nothing
    set fso = nothing 
您创建一个 FileSystemObject 的实例,并创建一个文本文件c:\testfile.txt,然后向其中写入一些文本。 创建文本文件的第二种方法是使用 FileSystemObject 对象的 OpenTextFile 方法,并使用 ForWriting 标志。
    Const ForWriting = 2
    Set a = fsoOpenTextFile ("c:\testfile.txt", ForWriting ,True)
您可以使用 WriteLine 或 Write 方法来写入内容。 WriteLine 和 Write 之间的基本区别是,Write 会写入一个尾随换行符,而 WriteLine 则没有尾随换行符。 您可以使用 WriteBlankLines 方法写入空行。
例如,要写入三行空行,您可以使用以下内容
    a.WriteBlankLines(3)
以下是文件可以打开的模式
- ForReading(常量值 - 1) - 仅打开文件进行读取。
- ForWriting(常量值 - 2) - 打开文件进行写入。
- ForAppending(常量值 - 8) - 打开一个文件并写入文件的末尾。
例如,声明读取的常量
    Const ForReading = 1
让我们看一个读取文本文件的例子
    dim s
    Const ForReading = 1
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set a = fso.OpenTextFile("c:\testfile.txt", ForReading)
    s = ts.ReadLine
    a.Close
    set a = nothing
    set fso = nothing 
    Response.Write "File Content : '" & s & "'"
为了从文件中读取内容,将使用以下方法
要从文件中读取指定数量的字符,请使用 Read 方法。 要读取整行(除换行符外),请使用 ReadLine 方法。 要读取文本文件的全部内容,请使用 ReadAll 方法。
让我们快速了解一下如何移动、复制和删除文件
- 要移动文件,请使用 File.Move或FileSystemObject.MoveFile。
- 要复制文件,请使用 File.Copy或FileSystemObject.CopyFile。
- 要删除文件,请使用 File.Delete或FileSystemObject.DeleteFile。
以下示例将显示如何使用 File 方法操作文件
   Dim fso
   
   Set fso = CreateObject("Scripting.FileSystemObject")
      
   'Move a File
   Set f2 = fso.GetFile("c:\testfile.txt")
   f2.Move ("c:\temp\testfile.txt")    ' Move the file to \temp directory.
   
   'Copy file to \temp.
   f2.Copy ("c:\temp\testfile.txt")
   
   'Delete File 
   Set f2 = fso.GetFile("c:\tmp\testfile.txt")
   f2.Delete
以下示例将显示如何使用 FileSystemObject 方法操作文件
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
      
   'Move a File
   Set f2 = fso.GetFile("c:\testfile.txt")
   fso.MoveFile("source_file", "destination");
   
   'Copy file to c:\temp folder
   fso.CopyFile "c:\testfile.txt", "c:\temp\"
   
   'Delete File 
   fso.DeleteFile "c:\testfile.txt"
示例:如何在特定文件夹中的所有文件中搜索特定字符串
接受字符串的表单如下所示
    <FORM METHOD=POST id=form1 action="searchresult.asp" 
                           name=form1 onsubmit="return Check();">
      Enter text to search for:
      <INPUT TYPE=TEXT NAME=TextToSearch>
      <P>
      <INPUT TYPE=SUBMIT VALUE="Begin Search!" id=SUBMIT1 name=SUBMIT1>
    </FORM>
要在特定文件夹中搜索字符串,以下是代码:(searchresults.asp)
    'Search Text
    Dim strtextToSearch
    strtextToSearch = Request("TextToSearch")
    'Now, we want to search all of the files
    Dim fso
    'Constant to read
    Const ForReading = 1
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    'Specify the folder path to search.
    Dim FolderToSearch
    FolderToSearch = "D:\temp"
    
    'Proceed if folder exists
    if fso.FolderExists(FolderToSearch) then 
        
        Dim objFolder    
        Set objFolder = fso.GetFolder(FolderToSearch)
        
        Dim objFile, objTextStream, strFileContents, bolFileFound
        bolFileFound = False
        Dim FilesCounter
        FilesCounter = 0 'Total files found
        For Each objFile in objFolder.Files
            Set objTextStream = fso.OpenTextFile(objFile.Path,ForReading)
            'Read the content
            strFileContents = objTextStream.ReadAll
            If InStr(1,strFileContents,strtextToSearch,1) then
               Response.Write objFile.Name & "<br>"
               FilesCounter = FilesCounter + 1
            End If
            objTextStream.Close
        Next
        if FilesCounter = 0 then 
            Response.Write "Sorry, No matches found."
        else
            Response.Write "Total files found : " & FilesCounter
        end if    
        
        'Destroy the objects
        Set objTextStream = Nothing
        Set objFolder = Nothing
    else
        Response.Write "Sorry, invalid folder name"
    end if
    Set fso = Nothing
摘要
本文简要介绍了 FileSystemObject、如何创建/删除文本文件,以及如何使用 FileSystemObject 在特定文件夹中的所有文件中搜索特定字符串的示例。
参考文献
- MSDN.
