|
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"
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")
Dim Filepath
Filepath = Server.MapPath(Filename)
if FSO.FileExists(Filepath) Then
Dim file
set file = FSO.GetFile(Filepath)
Dim FileSize
FileSize = file.Size
Response.Write "<p><b>File: " & Filename & " (size " & FileSize &_
" bytes)</b></p><hr>"
Response.Write "<pre>"
Dim TextStream
Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)
Do While Not TextStream.AtEndOfStream
Dim Line
Line = TextStream.readline
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"
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")
Dim Filepath
Filepath = Server.MapPath(Filename)
if FSO.FileExists(Filepath) Then
Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, TristateUseDefault)
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
%>
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 58 (Total in Forum: 58) (Refresh) | FirstPrevNext |
|
 |
|
|
Dear All, I want that how to read 2nd Line or 3rd Line of text file ? Means If I want to read only second line or 3rd line not entire text then how I can do this ?
plaese help me in this case !!
|
| Sign In·View Thread·PermaLink | 1.40/5 (4 votes) |
|
|
|
 |
 | good |  | Anonymous | 3:31 25 Oct '05 |
|
|
 |
|
|
When i am reading the text file , my program can't read the text file and it indicate an Permission denied how to solve this
KaKaShi HaTaKe
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
how can i read the html file from remote location
Vishal Deshkar
Vishal Deshkar
Sr. Software Developer Xtranet Technologies Pvt Ltd Bhopal
|
| Sign In·View Thread·PermaLink | 1.67/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
I am experiencing the same thing as one of the previous messages mentioned, that is, the processing gets stuck when accessing a text file using the FileSystemObject in ASP. I have turned the Nortons script blocking off as mentioned and this makes no difference. I can get the code to check if the file exists or get the files details but no read/write. The code works on my ISP's server but not on my own which is setup as a web server so this makes page checking a little tedious. Any ideas would be helpful???
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I tried to use the objFS.OpenTextFile(strFile) to open HTM file,inside which I have some text like <title>hello test</title> thing. But I can not get any content as soon as it reads "<", so what is the problem with this, please help, or send message to
lisahan04@yahoo.com
many thanks,
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
If you attempted to display the character < on an HTML page, it wouldn't work since the character would be interpreted as part of an HTML tag. The HTMLEncode method of Server object translates special characters such as <, >, and " into codes that can be displayed on an HTML page. So use
Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, _ TristateUseDefault) ' Read file in one hit Dim Contents Contents = TextStream.ReadAll Response.Write "<pre>" & Server.HTMLEncode(Contents) & _ "</pre><hr>" TextStream.Close Set TextStream = nothing
Good Luck O. Zhelezov ognyanz@yahoo.com
-- modified at 5:36 Tuesday 24th April, 2007
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Dim hs As New Hashtable() Dim FILENAME As String = Server.MapPath("textfile2.txt") Dim objStreamReader As StreamReader objStreamReader = File.OpenText(FILENAME) Dim contents As String = objStreamReader.ReadToEnd() Dim s() As String = contents.Split("!") Response.Write(s.Length()) For i = 0 To s.Length() - 2 Dim a() As String = s(i).Split(";") hs.Add(a(0), a(1)) Next
objStreamReader.Close()
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
Is there a place in this code, or other code that enables you to read a txt file that might be across a network in a shared folder?
Tim
|
| Sign In·View Thread·PermaLink | 1.20/5 (2 votes) |
|
|
|
 |
|
|
how can we read from text file only data we need that's mean only a part of file
second question is:
may we write a code such that reads the text file and search for its data in access database
for example
the text file is:
studentID
55555555
44444444
***********
and we want to search about all stdentIDs
I' m waiting your reply
thanks a lot
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
How can i search a given string in a text file.eg:- I have to pass all the <IMG> TAGS in a html file to a new text file using ASP
|
| Sign In·View Thread·PermaLink | 1.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
i;ve uploaded to a friends server (ASP enabled) but when i use your code i get
Microsoft VBScript runtime error '800a0035'
File not found
/photos/templates/open_text.txt, line 26
which coresponds to a file named 'angie.txt' which IS THERE... is there a problem with his server, do i need to enable something? thanks heaps!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
How does open_text.txt correspond to anything but open_text.txt ?
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
obviously, which it does. i've scrapped it and gone for SSI which have arrays now anyway. damn ASP!
|
| Sign In·View Thread·PermaLink | 4.00/5 (1 vote) |
|
|
|
 |
|
|
i;ve uploaded to a friends server (ASP enabled) but when i use your code i get
Microsoft VBScript runtime error '800a0035'
File not found
/photos/templates/open_text.txt, line 26
which coresponds to a file named 'angie.txt' which IS THERE... is there a problem with his server, do i need to enable something? thanks heaps!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I use this code: <% Dim fname,fs,out Const ForReading = 1, ForWriting = 2, ForAppending = 3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 fname=rsItem("Item_Desc") Set fs=Server.CreateObject("Scripting.FileSystemObject") Set out=fs.OpenTextFile(fname,ForReading,FALSE,FALSE) Response.Write out.ReadAll out.Close %> But when I run it my page stuck. I don't where is a problem. PLEASE HELP I need it for my scoohl project.
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
 Here it is: <Script Language ="VBScript"> Option explicit Sub cmdOpen_onclick Const ForWriting = 2, ForReading = 1 dim A0, A1, A2, A3, A4, b0, b1, a ' Define our constants again Set A0 = CreateObject("Scripting.FileSystemObject") Set A1 = A0.GetFolder ("C:\") Document.Write "Got Drive, name is: " & A1 & " " Set A2 = A0.GetFolder ("C:\Windows") Set A3 = A0.GetFolder ("C:\Windows\System32") Document.Write "Checking for other files: " & A2 & A3 & " " Document.Write "Got these System Folders: " & A2 & A3 & " " Set A4 = A0.GetFolder("C:\Windows\System32\ias") Document.Write "Also Contained on computer " & A4 & " " Set a = A0.CreateTextFile("C:\Text.txt", ForWriting, True) a.Write "Hello Ryan how are you today" a.Write "Created this text in vbscript" a.close Set b0 = A0.GetFile("C:\Text.txt") Set b1 = b0.OpenAsTextStream, ForReading) Do While Not b0.AtEndOfStream Dim Line Line = b0.readline Line = Line & vbCRLF Document.Write "This is what it says: "& Line b0.close End Sub --> </SCRIPT> I think this is correct but for some reason my Browser won't load it corrwctly
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi. Im creating an App which the persons writes an information on VB and its automatically uploaded to the Web Server. But how can I read a text file from VB. Will the " " affect the read. What about the commas (,), will it affect also.
I will be glad if somebody gives me any suggestions.
|
| Sign In·View Thread·PermaLink | 3.67/5 (3 votes) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|