sas

Topic: ASP


Let's see an example to read a text file

<%
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 & "'"
%>

To read a specified number of characters from a file, use Read method. To read an entire line (all except the newline character), use ReadLine method. To read the entire contents of a text file, use ReadAll method.

Let's have a quick look at how to move, copy and delete files:

  1. To move a file, use File.Move or FileSystemObject.MoveFile .
  2. To copy a file, use File.Copy or FileSystemObject.CopyFile .
  3. To delete a file, use File.Delete or FileSystemObject.DeleteFile


Example Program

<%
Option Explicit

Const Filename = "/readme.txt" ' file to read

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' Create a filesystem object
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")

' Map the logical path to the physical system path
Dim Filepath
Filepath = Server.MapPath(Filename)

if FSO.FileExists(Filepath) Then

' Get a handle to the file
Dim file
set file = FSO.GetFile(Filepath)

' Get some info about the file
Dim FileSize
FileSize = file.Size

Response.Write "<p><b>File: " & Filename & " (size " & FileSize &_
" bytes)</b></p><hr>"
Response.Write "<pre>"

' Open the file
Dim TextStream
Set TextStream = file.OpenAsTextStream(ForReading, _
TristateUseDefault)

' Read the file line by line
Do While Not TextStream.AtEndOfStream
Dim Line
Line = TextStream.readline

' Do something with "Line"
Line = Line & vbCRLF

Response.write Line
Loop

Response.Write "</pre><hr>"

Set TextStream = nothing

Else

Response.Write "<h3><i><font color=red> File " & Filename &_
" does not exist</font></i></h3>"

End If

Set FSO = nothing
%>

 

Prev