ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

ASP Source Code:
' *** Inline Code Version ***
' If you need to do other things to the file once you verify its
' existance, then this is probably the way to go.  That way
' you'll be able to reuse the same FileSystemObject before
' you close it.
Dim objFSO             ' FileSystemObject variable.
Dim strFileThatExists  ' The name of the file that exists.
Dim strFileThatDoesnt  ' The name of the file that doesn't exist.
strFileThatExists = "file_exists.asp"
strFileThatDoesnt = "file_doesnt.asp"
' Create an instance of the FSO in order to access the file system.
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' The FileExists method expects a fully qualified path and
' filename.  I've specified relative file names above.  In order
' to get the full path we make a call to Server.MapPath to give us
' the corresponding physical path to the file on the server's file
' system.
<strong>Inline Code Version:</strong>
Checking to see if "<%= strFileThatExists %>" exists:
<b><%= objFSO.FileExists(Server.MapPath(strFileThatExists)) %></b>
Checking to see if "<%= strFileThatDoesnt %>" exists:
<b><%= objFSO.FileExists(Server.MapPath(strFileThatDoesnt)) %></b>
Set objFSO = Nothing
' *** Function Version ***
' As I mentioned above, I didn't wrap the above version in a
' function because after you check if a file exists, you'll
' probably want to do something with it.  If that's not the case,
' this function will just check if a file exists and return "True"
' if it does and "False" if it doesn't.
Function DoesFileExist(ByVal strFileName)
	' You'll notice I use ByVal when passing in strFileName.
	' That's because I play around with its value inside the
	' function and don't want any changes I make affecting
	' its value outside the function.
	Dim objFSO
	' If we don't have a full path starting with a drive letter
	' then we run the path through Server.MapPath().
	If InStr(strFileName, ":") = 0 Then
		strFileName = Server.MapPath(strFileName)
	End If
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	DoesFileExist = objFSO.FileExists(strFileName)
	Set objFSO = Nothing
End Function
<strong>Function Version:</strong>
Checking to see if "<%= strFileThatExists %>" exists:
<b><%= DoesFileExist(strFileThatExists) %></b>
Checking to see if "<%= strFileThatDoesnt %>" exists:
<b><%= DoesFileExist(strFileThatDoesnt) %></b>

Back the the Sample Output

Back to the Sample Index

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers