Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Open, Read and Create files on ASP

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

robinson
Newbie


Posts: 18
Group: Registered
Joined: Sep 2006
Status: Offline
Reputation: 0
Points: 0 (Donate)
Post: #1

Wink Open, Read and Create files on ASP


Open and Read content from a text file

Example 1:
This one will be the basic code we need to open a text file:

Code:
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")
filecontent = wfile.ReadAll

wfile.close
Set wfile=nothing
Set fs=nothing

response.write(filecontent)
%>

Line 2 will create the appropriate environment which allows to perform the operations involving files in the server. We have defined a variable named "fs" to do it (we may change the name of this variable).

In line 4 we have create a new variable named "wfile" and we have apply the method OpenTextFile to variable "fs". We have also define which is the exact location of the file we want to open in this line (the complete path is necessary).

In line 5 we have read all the content of the file to a variable named "filecontent" using the instruction "ReadAll".

Lines 7 to 9 are use to let the server know we have finished all operations involving files.

In line 11 we have response to the client with the content in the variable "filecontent".


Example 2:
Let's suppose we have a file with different kind of information in each line (a name in the first line, the last name in the second one, and the age in the third one), and we want to use them separately. This one will be the script we may use:

Code:
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")
firstname = wfile.ReadLine
lastname = wfile.ReadLine
theage = wfile.ReadLine

wfile.close
Set wfile=nothing
Set fs=nothing

%>

Your first name is <% =firstname %><BR>
Your last name is <% =firstname %><BR>
Your are <% =firstname %> years old<BR>

This example is very similar to the previous one, but in this case each line we have read from "myfile.txt" has been saved to a different variable (lines 5 to 7), and they have been used in lines 15 to 17 to respond to the client.

Example 3:
This example will read all lines in the file, and the response page will include the content of each line with its line number.

Code:
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")

counter=0
do while not wfile.AtEndOfStream
counter=counter+1
singleline=wfile.readline
response.write (counter & singleline & "<br>")
loop

wfile.close
Set wfile=nothing
Set fs=nothing

%>


In line 6 we will define the variable "counter", and in line 7 to 11 we will repeated instructions within the Do_While _Loop until the file does not reach the end of the file (the condition is "not wfile.AtEndOfStream").

Example 4:
Let's suppose we have a file with a number in line 1 and a second number in line 2.

Code:
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")

number1 = Clng(wfile.ReadLine)
number2= Clng(wfile.ReadLine)

number1and2 = number1 + number2
response.write (number1and2)

wfile.close
Set wfile=nothing
Set fs=nothing

%>


In the previous examples we were able to save the value in a line to a variable, but that variable was a string class variable. In this example we have saved the content of line 1 and line 2 to variables "number1" and number2" by using the function "Clng". This function has allow us to add both numbers in line 9 and send the result to the client (line 10).


Create and Write a text file

Example 1:
The basic code we need to create a file is very similar to that one we have used to open a file:

Code:
<%
thetext="Write this text in the file"

Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.CreateTextFile("c:\Mydir\myfile.txt", True)
wfile.Write (thetext)

wfile.close
Set wfile=nothing
Set fs=nothing

response.write("Text created")
%>


The differences to instructions when opening a file are line 6 and line 7:

The method used in line 6 is "CreateTextFile"; it is necessary to indicate the complete path to the file we want to create; in line 6 we may use the instruction True (to allow over-writing an existing file) or False (if the file exits, it is not over-written).

Line 7 will write in the file the string in variable "thetext".



We may also use this instruction to add content to the file

wfile.WriteLine (thetext1)
wfile.WriteLine (thetext2)
...

In this case we will write the content in variable "thetext1" in line 1, content in "thetext2" in line 2 etc.



Example 2:
Let suppose we want to record the IP address of all visitor to our page to a file named "mylog.txt".

Code:
<%
VisitorsIP=Request.ServerVariables ("REMOTE_ADDR")

Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\mylog.txt", 8,false,0)
wfile.WriteLine (VisitorsIP)

wfile.close
Set wfile=nothing
Set fs=nothing

response.write("IP registered")
%>

The IP address is requested in line 2 (check Functions and Procedures). In this case we have open the file "mylog.txt" in line 6 with the instruction "forappending". this instruction will allow us to open the file and add at the end of it the IP address of our last visitor.

23-09-2006 01:44 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 13-10-2008, 10:51 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB