Brinkster Knowledge Base

KB Home   /  Support  /  Code Snippets  /   ASP File System Object (FSO) example
ASP File System Object (FSO) example Articles
Following is an ASP File System Object demonstration.
Copy and paste this code snippet into your .asp page, then view the page in your web browser.
http://yoursite.com/page.asp

*Note that no edit to the code needs to be made at all to run this demonstration.*
*Note if you have a file/directory named test in your desired directory, it may delete your existing test file/direcotry.*

<%
''Current Directory must have writeable permissions for this sample to work.
''It is suggested to NOT run this in the root directory 
'' and NOT set writeable permissions to the root directory of your website.

vqs = request.querystring("Action")
response.write "" &_
"<a href='?action=cf'>Create File</a>&nbsp;&nbsp;&nbsp;" &_
"<a href='?action=cd'>Create Directory</a>&nbsp;&nbsp;&nbsp;" &_
"<a href='?action=df'>Delete File</a>&nbsp;&nbsp;&nbsp;" &_
"<a href='?action=dd'>Delete Directory</a>&nbsp;&nbsp;&nbsp;" &_
"<br><br>"

select case vqs
  Case "cf"
    call createfile
  Case "cd"
    call createdir
  Case "df"
    call delfile
  Case "dd"
    call deldir
End Select

sub createfile()
  set fs=Server.CreateObject("Scripting.FileSystemObject")
  if not fs.FileExists(server.mappath("test.txt")) then
    set fname=fs.CreateTextFile(server.mappath("test.txt"),true)
    fname.WriteLine("Hello World!")
    fname.Close
    response.write "File Created. " & server.mappath("test.txt")&"<br>"
  else
    response.write "File Exists Already. " & server.mappath("test.txt")
  end if
  set fs=nothing
  set fname=nothing
end sub


sub createdir()
  set fs=Server.CreateObject("Scripting.FileSystemObject")
  if fs.FolderExists(server.mappath("test")) then
    response.write "Directory Already Exists - must remove before creation. " & server.mappath("test")&"<br>"
  else
    set f=fs.CreateFolder (server.mappath("test")) 
    response.write "Directory Created. " & server.mappath("test")&"<br>"
  end if  


  set f=nothing
  set fs=nothing
end sub

sub delfile()
  Set fs=Server.CreateObject("Scripting.FileSystemObject")
  if fs.FileExists(server.mappath("test.txt")) then
    fs.DeleteFile(server.mappath("test.txt"))
    response.write "File Delete. " & server.mappath("test.txt")
  else
    response.write "File Does Not Exist. " & server.mappath("test.txt")
  end if
  set fs=nothing
end sub

sub deldir()
  set fs=Server.CreateObject("Scripting.FileSystemObject")
  if fs.FolderExists(server.mappath("test")) then
    fs.DeleteFolder(server.mappath("test"))
    response.write "Directory Delete. " & server.mappath("test")
  end if
  set fs=nothing
end sub
%>