INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

ASP 101

Reading/Writing/Creating Files by vasah20
Posted: 23 Feb 01

In order to do anything with filesystem on the webserver, we need to create an instance of the Scripting.FileSystemObject (FSO). What this will do is allow us access to the files and folders in our site, and will also allow us to read and write to them as well. I'm only going to cover the reading and writing to files, I'll try and write another FAQ for accessing the directory structure of your website.

Reading a file
(this outputs the entire file)

1  dim fsoObj, sFileName
2  sFileName = Server.MapPath("readThisFile.txt")
3  set fsoObj = Server.CreateObject("Scripting.FileSystemObject")
4  if fsoObj.fileExists(sFileName) then
5     dim objTextFile
6     set objTextFile = fsoObj.OpenTextFile(sFileName)
7     while not objTextFile.AtEndOfStream
8        Response.write objTextFile.ReadLine
9     wend
10    objTextFile.close
11    set objTextFile = nothing
12 else
13    Response.write "File Not Found"
14 end if
15 set fsoObj = nothing


allow me to explain what's going on, line by line.
1 - declare variables (good programming practice )
2 - get absolute path to the file. The FSO needs to have an absolute path to the file, it can't be root or doc relative.
3 - create the FSO object
4 - always check to see if the file exists. It'll save you some pain, and allow you to close gracefully. If you don't, you'll get a VBError.
5 - new variable, this one to hold the contents of your textfile
6 - Keep the while loop going as long as the end of the file isn't reached.
7 - Displays to the browser the line that was read
10 through 15 - clean up. Notice how you only close objTextFile, not the fsoObj itself. Again, always close and clean up. again, Good programming practice

Writing to a file / Creating a file

1  dim fsoObj, sFileName, objtextFile
2  sFileName = Server.MapPath("writeToThisFile.txt")
3  set fsoObj = Server.CreateObject("Scripting.FileSystemObject")
4  set objtextFile = fsoObj.OpenTextFile(sFileName, 8)
5  objTextFile.WriteLine ("Hello world")
6  objTextFile.Close
7  set objTextFile = nothing
8  set fsoObj = nothing


I'll explain what's new here.
3 - this opens up a text file, the 8 means it's for appending. If you want to change this, the values are 1 for reading, 2 for writing, 8 for appending. (1 is default)
4 - this appends the line "Hello World" to the end of the file

Now -- if you want to create a file, all you need to do is change one line from above.

3 set objTextFile = fsoObj.OpenTextFile("C:\inetpub\thisFile.txt", 8, TRUE)

the last parameter is for creation, and it takes a boolean value (False by default). What this does is open up thisFile.txt for appending, but if it doesn't find the file, it creates it. If you want, you can also use this:

  set objTextFile = fsoObj.CreateTextFile("C:\inetpub\thisFile.txt")


VERY IMPORTANT
Before you begin, you need to make sure that you have the right permissions on your webserver. Everyone has read permission, which obviously you need to read files. If you plan on writing to files or creating files, though, then you are going to need "write" permissions set on your server. If you control your server, then what you need to do is set the IUSR_machinename security level to allow write permissions, since IUSR_machinename is what anonymous web users use to get into your account. Right-click the folder or file that needs write permissions, then select the security tab. Click the button marked Permissions, then select the IUSR_machinename profile. Make sure that it has write access.

If you do not have control over your webserver, then you need to have your ISP set the write permissions for you.

I've found that the easiest thing to do this is to have your ISP grant write priviledges to a folder on your site, then just write everything to that folder. This way, you won't have to grant permissions on for every file on your site.

hope this helps someone out.
leo

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close