Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

ASP: using text files instead of databases

I'm making a very small application that will ask users for their names and e-mail addresses when they visit a site.

My question is:

How many records will ASP support if I save them in a plain text file?

I don't want to use a database connection because I only need two fields.

Thank you in advance!
0
NoviceASP
Asked:
NoviceASP
  • 2
1 Solution
 
whammyCommented:
Well, as a test I just wrote my email to a text file 1 million times in a loop... no problem with that.

It was definitely pretty darn slow populating the whole list into a textarea, though.... but it did it.

If you want here's a quick and easy example I wrote for someone else which posts to itself.

It doesn't have the name field, just the email address -but you could add that in easily:

addtolist.asp:

<% @Language="VBScript" %>
<% Option Explicit %>
<%
'*************************************** FUNCTIONS

Function IsEmail(str)
   Dim ieRegEx
   Set ieRegEx = New RegExp
   ieRegEx.Pattern = "^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z0-9]{2,}$"
   IsEmail = ieRegEx.Test(str)
End Function

'*********************************** DIM VARIABLES

Dim Email, SubmitNum ' Requested variables
Dim Found            ' Variable to check for duplicate emails
Dim fs, f, filepath  ' FileSystemObject variables

'******************** REQUEST/INITIALIZE VARIABLES

Email = Request.Form("Email")
SubmitNum = Request.Form("SubmitNum")
filepath = Mid(Server.MapPath("\"), 1, InStrRev(Server.MapPath("\"),"\")-1) & "\database\emaillist.txt"

'************************************ MAIN PROGRAM

SubmitNum = SubmitNum + 1

If IsEmail(Email) Then
   Call LookupEmail()
   If Found = False Then
      Call AppendEmail()
      Call DisplayThankYou()
   Else
      Call DisplayDupeMessage()
   End If
Else
   Call DisplayForm()
End If

'******************************** END MAIN PROGRAM

'************************************* SUBROUTINES

Sub LookupEmail()
   Found = False
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set f = fs.OpenTextFile(filepath,1,True)
   Do While NOT f.AtEndOfStream AND Found = False
      If f.ReadLine = Email Then Found = True
   Loop
   Set f = Nothing
   Set fs = Nothing
End Sub

Sub AppendEmail()
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set f = fs.OpenTextFile(filepath,8,True)
   f.WriteLine(Email)
   Set f = Nothing
   Set fs = Nothing
End Sub
%>

<% Sub DisplayThankYou() %><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
   <meta http-equiv="refresh" content="3;URL=addtolist.asp" />
   <title>Thank You!</title>
</head>
<body>
   <div><span style="font-weight:bold">Thank you for subscribing!</span></div>
</body>
</html><% End Sub %>


<% Sub DisplayDupeMessage() %><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
   <meta http-equiv="refresh" content="3;URL=addtolist.asp" />
   <title>Your email address already exists!</title>
</head>
<body>
   <div><span style="font-weight:bold">Your email address already exists in our database!</span></div>
</body>
</html><% End Sub %>


<% Sub DisplayForm() %><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
   <title>Please enter your email address to subscribe!</title>
</head>
<body>
   <form id="addtoemaillist" method="post" action="addtolist.asp">
      <input type="text" name="Email" value="<% = Email %>" />
      <% If SubmitNum > 1 Then %>
      <span style="color:#ff0000"> * Invalid Email Address</span>
      <% End If %>
      <br />
      <input type="hidden" name="SubmitNum" value="<% = SubmitNum %>" />
      <input type="submit" value="Submit" />
   </form>
</body>
</html><% End Sub %>





0
 
thelyristCommented:
perhaps you should take a look at XML

it is more flexible than using a text file, but yet pretty simple enough for simple task like that. a server that is capable of running ASP should be able to handle XML files just as well (with the MSXML parser)

with XSLT you can even transform the XML file into HTML for display purpose.

if you have not considered this option, i strongly recommend to look into it. it is well worth the time to know XML with ASP
0
 
NoviceASPAuthor Commented:
Thank you very much!
0
 
whammyCommented:
:)

Doesn't hurt to learn XML either, once you're familiar with FSO. I haven't had much need to use XML in classic ASP but I'm sure I will in .NET... however for something simple like this, text files should be fine.

If you need to add more fields, just use some delimiter, like | after making sure you strip those from any potential entries first.

Then when you use file.ReadLine, you can split up the lines as you read them using the Split() function.

P.S. If you go here you can learn just about everything about FileSystemObject, it's where I learned:

http://www.w3schools.com/asp/asp_ref_filesystem.asp

If you play around with the examples there you can do some pretty complex stuff just using text files.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now