File System Function Library
Working with files & the FileSystem object is actually fairly easy. James Lind?n presents a full
featured function library to make it even easier.
Click Here to download the function library include file.
The purpose of this script is to make working with the file system easier. It includes functions
for manipulating directories and files. Whether you want to use this script for a web based file
manager, or simply want to make working with text files easier, the script has functions which
will streamline your code.
To use these functions, save the file above (or copy the code here to an include file), upload
the include file to your server, and include it in your scripts like this:
<!--#include file="filesys.txt"-->
For more information on include files Click Here.
For more information on the FileSystem object Click Here.
<%
' Project: InteliGenie
' Creator: James Lind?n
' Date: 1/31/01 5:14PM
'********************************************************************
' root Returns the application root root()
' url Returns the true url url()
' mkdir Creates directory on server mkdir( DIrName )
' rmdir Delete directory rmdir( DirName )
' isdir Returns boolean on folder existance isdir( DirName )
' cpdir Copy directory cpdir( DirName, Destination, OverWrite )
' mvdir Move directory mvdir( DirName, Destination )
' isfile Returns boolean on file existance isfile( FileName )
' wfile Creates file and writes string to file wfile( FileName, OverWrite, String )
' rfile Read file to string rfile( FileName )
' afile Append string to file afile( FileName, String )
' cpfile Copy file cpfile( FileName, Destination, OverWrite )
' mvfile Move file mvfile( FileName, Destination )
' rmfile Delete file rmfile( FileName )
'********************************************************************
'The root() function will return a string variable of the drive path
'sample string = c:\inetpub\wwwroot
Public Function root()
root = Request.ServerVariables( "Appl_Physical_Path" )
End Function
'********************************************************************
'The url() function will return a string variable of the web url
'sample string =
http://www.domain.com/filesys.aspPublic Function url()
url = "http://" & Request.ServerVariables( "Server_Name" )
& Request.ServerVariables( "Script_Name" )
End Function
'********************************************************************
'The mkdir() function creates a directory and returns a string variable with a pass/fail message
Public Function mkdir( xVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FolderExists( xVar ) Then
msg = "FAIL: Directory already exists."
Else
Sys.CreateFolder( xVar )
msg = "PASS: Directory created."
End If
Set Sys = Nothing
mkdir = msg
End Function
'********************************************************************
'The rmdir() function removes a directory and returns a string variable with a pass/fail message
Public Function rmdir( xVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FolderExists( xVar ) Then
Sys.DeleteFolder( xVar )
msg = "PASS: Directory removed."
Else
msg = "FAIL: Directory does not exist."
End If
Set Sys = Nothing
rmdir = msg
End Function
'********************************************************************
'The isdir() function checks to see if a directory exists and returns a boolean variable
Public Function isdir( xVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FolderExists( xVar ) Then
msg = True
Else
msg = False
End If
Set Sys = Nothing
isdir = msg
End Function
'********************************************************************
'The cpdir() function copies a folder and returns a string variable with a pass/fail message
Public Function cpdir( xVar, yVar, zVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FolderExists( xVar ) Then
Sys.CopyFolder xVar, root & yVar, zVar
msg = "PASS: Directory copied."
Else
msg = "FAIL: Directory not found."
End If
Set Sys = Nothing
cpdir = msg
End Function
'********************************************************************
'The mvdir() function moves a folder and returns a string variable with a pass/fail message
Public Function mvdir( xVar, yVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FolderExists( xVar ) Then
Sys.MoveFolder xVar, root & yVar
msg = "PASS: Directory moved."
Else
msg = "FAIL: Directory not found."
End If
Set Sys = Nothing
mvdir = msg
End Function
'********************************************************************
'The isfile() function checks to see if a file exists and return a boolean variable
Public Function isfile( xVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FileExists( xVar ) Then
msg = True
Else
msg = False
End If
Set Sys = Nothing
isfile = msg
End Function
'********************************************************************
'The wfile() write a string to a file and returns a stirng variable with a pass/fail message
Public Function wfile( xVar, yVar, zVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If yVar Then
Set Txt = Sys.OpenTextFile( xVar, 2 )
Txt.Write( zVar )
Txt.Close
msg = "PASS: File created, data saved."
Else
If Sys.FileExists( xVar ) Then
msg = "FAIL: File already exists."
Else
Set Txt = Sys.OpenTextFile( xVar, 2 )
Txt.Write( zVar )
Txt.Close
msg = "PASS: File created, data saved."
End If
End If
Set Sys = Nothing
wfile = msg
End Function
'********************************************************************
'The rfile() function reads a file and returns a string variable with the contents of the file
Public Function rfile( xVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FileExists( xVar ) Then
Set Txt = Sys.OpenTextFile( xVar, 1 )
msg = Txt.ReadAll
Txt.Close
Else
msg = "FAIL: File does not exist."
End If
Set Sys = Nothing
rfile = msg
End Function
'********************************************************************
'The afile() function appends a string to a file and returns a string variable with a pass/fail
message
Public Function afile( xVar, zVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FileExists( xVar ) Then
Set Txt = Sys.OpenTextFile( xVar, 8 )
Txt.Write( zVar )
Txt.Close
msg = "PASS: File appended, data saved."
Else
msg = "FAIL: File does not exist."
End If
Set Sys = Nothing
afile = msg
End Function
'********************************************************************
'The cpfile() function copies a files and returns a string variable with a pass/fail message
Public Function cpfile( xVar, yVar, zVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FileExists( xVar ) Then
Sys.CopyFile xVar, root & yVar, zVar
msg = "PASS: File copied."
Else
msg = "FAIL: File not found."
End If
Set Sys = Nothing
cpfile = msg
End Function
'********************************************************************
'The mvfile() function moves a file and returns a string variable with a pass/fail message
Public Function mvfile( xVar, yVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FileExists( xVar ) Then
Sys.MoveFile xVar, root & yVar
msg = "PASS: File moved."
Else
msg = "FAIL: File not found."
End If
Set Sys = Nothing
mvfile = msg
End Function
'********************************************************************
'The rmfile() function deletes a file and returns a string variable with a pass/fail message
Public Function rmfile( xVar )
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
If Sys.FileExists( xVar ) Then
Sys.DeleteFile( xVar )
msg = "PASS: File deleted."
Else
msg = "FAIL: File not found."
End If
Set Sys = Nothing
rmfile = msg
End Function
%>
3/18/01 by James Lind?n