AllExperts > Experts 
Search      

Active Server Pages Programming (ASP)

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Active Server Pages Programming (ASP) Answers
Question Library

Ask a question about Active Server Pages Programming (ASP)
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Srini Nagarajan
Expertise
I can answer any kind of questions in ASP.NET, C#, VB.NET, SharePoint 2007, ASP, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience
Contact me if you need any custom development on ASP.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder.
 
   

You are here:  Experts > Computing/Technology > Business Software > Active Server Pages Programming (ASP) > reading html source code

Topic: Active Server Pages Programming (ASP)



Expert: Srini Nagarajan
Date: 6/18/2005
Subject: reading html source code

Question
Hi!

I just want to ask you if you know how can I read the source code of a certain webpage using ASP vbscript and store it to a text file?

thanks for your time =)

Norman

Answer
HI

Here is the Code you can use this and read the file.

One of the most important tasks in any programming language is the ability to read and write files. The steps involved in ASP are no different than many other languages:

Specify the location of the file
Determine if the file exists
Get a handle to the file
Read the contents
Close the file and release any resources used
File I/O in ASP can be done using the FileSystemObject component. When opening a text file you simply open it as a text stream, and it is this text stream that you use to access the contents of the file.

The FileSystemObject allows you to perform all file and folder handling operations. It can either return a file which can then be opened as a text stream, or it can return a text stream object directly.

In the following I present two different methods. The first method gets a file object and uses that to open the text stream, and the second method opens the text stream directly from the FileSystemObject.

Method 1:
<% 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
%>

Method 2:
<% 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

  Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, _                                          TristateUseDefault)
  ' Read file in one hit
  
  Dim Contents
  Contents = TextStream.ReadAll
  Response.write "<pre>" & Contents & "</pre><hr>"
  TextStream.Close
  Set TextStream = nothing
  
Else

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

End If

Set FSO = nothing
%>


Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.