Is there a way to edit or delete lines from the text file as well, or is it only a read/write/append affair?
The way the File System Object works, there's no easy way to edit text, delete a line,
or even insert a line in the middle of a file. To do anything like that you usually
need to read the contents of the file, edit the string that you read and write it back
out. There's no reason you can't write it back out to the same filename which would
basically achieve the desired result.
However, there is a downside. On a busy site (or even if you're just unlucky) there
is the chance that two users could be editing the same file at the same time.
With the right timing, this type of situation could result in the loss of some
or all of the data contained in the file. If your data is important or
you need to be able to handle many concurrent users, you're usually better off
using a database as your data store and utilizing transactions to deal with the
concurrency issues.
That being said, text file operations are generally very quick and we've
yet to have any issues with our samples which operate in this manner.
If you'd like to see some examples, take a look at the following samples:
Guestbook with New Entries on Top
and Web Log.
If the folder I reference doesn't exist, the sample returns an error. How can I check if the folder exists first?
The easiest way is to use the FileSystemObject.FolderExists method. It takes the path you want to check and will return a boolean (True/False) indicating if that path exists on the file system or not. Here's some sample code ilustrating it's usage.
<%
Dim objFSO
Dim strPath
strPath = "C:\Temp\asp101testfolder\test.txt"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' Check if specified folder exists. If not, create it.
If Not objFSO.FolderExists(strPath) Then
objFSO.CreateFolder(strPath)
End If
' Now we know the folder referenced in strPath exists.
' Do something with the folder.
' Get rid of FSO object
Set objFSO = Nothing
%>
You'll need to do this for each Folder. So the above code won't work unless
C:\Temp\ already exists. To be really safe you should check each folder
along the path.
I've used the code and it works great, but it appears to overwrite previous information each time. Is there any way of writing sequentially to give a permanent record of all the data input?
Sure... instead of using objFSO.CreateTextFile you simply use objFSO.OpenTextFile and pass it an existing file name. You should also specify that you're opening it for appending:
Set objFile = objFSO.OpenTextFile(Server.MapPath("textfile.txt"), 8) 'ForAppending
If you open it ForReading (1) then you can't write to it and ForWriting (2) overwrites the current contents, but ForAppending (8) lets you add additional text to the end.
I'm using Win NT or Win 2000 and keep getting a permission denied error message. How do I fix it?
In order for the anonymous internet user account to access the text file you need to be sure that the NTFS permissions on the file allow the type of access you need. The defaults on web directories normally don't allow the account write access so if you want the user to be able to write to the file you'll need to change them.
You can do this by using Windows Explorer and right-clicking on the file and selecting Properties. Next click on the Security tab. A list of accounts and their respective permissions should appear. The IUSR_machine-name account is the one that is normally used for anonymous web user access so this is the one whose security settings you should check. Once you've set the permissions to an appropriate state depending on your usage of the text file, save the changes and give your ASP script another try. It should work much better.
Please note: This form is only for submitting questions about the sample for us to consider including in the FAQ. If we feel the question merits inclusion, we will include it along with a reply. We will not respond to your email individually.