<%@ LANGUAGE="VBScript" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<% '***************************************************************************
   '* ASP File Operations                                                     *
   '*                                                                         *
   '* Do not remove this notice.                                              *
   '*                                                                         *
   '* Copyright 1999, 2000 by Mike Hall.                                      *
   '* Please see http://www.brainjar.com for documentation and terms of use.  *
   '***************************************************************************
  
   'Set file I/O constants.

   Const ForReading   = 1
   Const ForWriting   = 2
   Const ForAppending = 8

   'Map the file name to the physical path on the server.

   filename = "temp.txt"
   path = Server.MapPath(".") & "\data\" & filename

   'Get requested operation, if any.

   operation = Request.Form("operation") %>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>
ASP File Operations Demo</title>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<link rel="stylesheet" type="text/css" href="/common/default.css" />

</head>

<body>


<div id="demoBox">


<h3>
ASP File Operations Demo</h3>


<p>
Select a file operation below, the action will be performed against a text
file named '<a href="data/<% = filename %>
">
<% = filename %>
</a>
' and the
contents will be displayed.</p>


<p>
Note that the file may be suddenly changed or deleted if any one else
happens to be performing operations against it.</p>


</div>


<form action="<% = Request.ServerVariables("SCRIPT_NAME") %>
" method="post">

<p>

  <input name="operation" type="radio" value="create" />
 Create
  <input name="operation" type="radio" value="delete" />
 Delete
  <input name="operation" type="radio" value="read" checked="checked" />
 Read
  <input name="operation" type="radio" value="write" />
 Write
  <input name="operation" type="radio" value="append" />
 Append
</p>

<p>

  <input type="submit" value="Submit" />

  <input type="reset" value="Reset" />

</p>

</form>


<p>

<% 'Perform the request operation.

   if operation = "create" then
     CreateFile(path)
     ReadFile(path)
   elseif operation = "delete" then
     call DeleteFile(path)
   elseif operation = "read" then
     call ReadFile(path)
   elseif operation = "write" then
     call WriteFile(path)
     ReadFile(path)
   elseif operation = "append" then
     call AppendFile(path)
     ReadFile(path)
   end if %>

</p>


</body>

</html>


<% sub CreateFile(path)

     dim fs, file

     Response.Write("<b>CreateFile:</b><br>")

     set fs = CreateObject("Scripting.FileSystemObject")

     'If the file already exists give an error message.

     if fs.FileExists(path) then
       Response.Write("File " & filename & " already exists, create aborted.<br>" & vbCrLf)

     'Otherwise create it and write some data to it.

     else
       Response.Write("Creating " & filename & ".<br>")
       set file = fs.CreateTextFile(path)
       Response.Write("Writing data to " & filename & ".<br>")
       file.WriteLine("Line 1 added " & Now() & ".")
       file.WriteLine("Line 2 added " & Now() & ".")
       file.WriteLine("Line 3 added " & Now() & ".")
       file.WriteLine("Line 4 added " & Now() & ".")
       file.WriteLine("Line 5 added " & Now() & ".")
       file.Close()
     end if

   end sub

   sub DeleteFile(path)

     dim fs, file

     Response.Write("<b>DeleteFile:</b><br>")

     set fs = CreateObject("Scripting.FileSystemObject")

     'If the file doesn't exist give an error message.

     if not fs.FileExists(path) then
       Response.Write("File " & filename & " does not exist.<br>")

     'Otherwise delete it.

     else
       Response.Write("Deleting " & filename & ".<br>")
       fs.DeleteFile(path)
     end if

   end sub

   sub ReadFile(path)

     dim fs, file

     Response.Write("<b>ReadFile:</b><br>")

     set fs = CreateObject("Scripting.FileSystemObject")

     'If the file doesn't exist give an error message.

     if not fs.FileExists(path) then
       Response.Write("File " & filename & " does not exist, read aborted.<br>")


     'Otherwise, open it and display the contents.

     else
       set file = fs.OpenTextFile(path, ForReading)
       Response.Write("Reading " & filename & ".<br>")
       Response.Write("<pre>")
       do while not file.AtEndOfStream
         Response.Write(file.ReadLine & vbCrLf)
       loop
       Response.Write("</pre>")
       file.Close()
     end if

   end sub

   sub WriteFile(path)

     dim fs, file

     Response.Write("<b>WriteFile:</b><br>")

     set fs = CreateObject("Scripting.FileSystemObject")

     'If the file doesn't exist give an error message.

     if not fs.FileExists(path) then
       Response.Write("File " & filename & " does not exist, write aborted.<br>")

     'Otherwise, overwrite the contents.

     else
       set file = fs.OpenTextFile(path, ForWriting)
       Response.Write("Rewritng file " & filename & ".<br>")
       file.WriteLine("Rewriting file with line 1 added " & Now() & ".")
       file.WriteLine("Rewriting file with line 2 added " & Now() & ".")
       file.WriteLine("Rewriting file with line 3 added " & Now() & ".")
       file.Close()
     end if

   end sub

   sub AppendFile(path)

     dim fs, file

     Response.Write("<b>AppendFile:</b><br>")

     set fs = CreateObject("Scripting.FileSystemObject")

     'If the file exists, check its size and exit if it exceeds 500K.

     if fs.FileExists(path) then
       set file = fs.GetFile(path)
       if file.size > 300 then
         Response.Write("Size limit exceeded for file " & filename & ", append aborted.<br>")
         exit sub
       end if

     'If the file doesn't exists, give a informational message.

     else
       Response.Write("File " & filename & " does not exist, will create.<br>")
     end if

     'Open or create the file as needed and append a line to it.

     Response.Write("Appending line to " & filename & ".<br>")
     set file = fs.OpenTextFile(path, ForAppending, true)
     file.WriteLine("New line added " & Now() & ".")
     file.Close()

   end sub %>