check existence of file or folder in asp

Fri Jul 13, 2012 10:48 pm

In this article we see how to check if a file or folder exists or not using ASP script. In your application you may need to check the existence of file before you start a process to avoid errors. In the example presented is this article we use object named “FileSystemObject”, this object uses the file path input in order to do checking on the existence. First we will need to create instance of FileSystemObject :
Code:
Dim myFileSystemObject
Set myFileSystemObject 
= Server.CreateObject("Scripting.FileSystemObject") 
Now we will use the created object to do checking for folder existence
: 
If myFileSystemObject
.FolderExists(Server.MapPath("db")) Then
Response
.Write "Folder is found"
else 
Response
.Write "Folder not found"
End if  

Based on the return of Folder Exists a message will be printed, the same case also for file existence:
Code:
Dim myFSOobj,myFilePath
myFilePath
=Server.MapPath("myfile.txt")    same directory
Set myFSOobj 
= Server.CreateObject("Scripting.FileSystemObject")
if myFSOobj.fileExists(myFilePath) Then 
Response
.Write "File is found "
Else
Response.Write "File is not found"
End if 
Set myFSOobj 
= Nothing

In the following snippet we will show you how to list the content of a folder:
Code:

Dim myobjFSO
, myobjFile, myobjFolder

Set myobjFSO 
= Server.CreateObject("Scripting.FileSystemObject")
Set myobjFolder = myobjFSO.GetFolder(Server.MapPath("/folderpath"))

For Each myFile in myobjFolder.Files
Response
.Write myFile.Name & "<br>"
Next
Set myobjFolder 
= Nothing
Set myobjFSO 
= Nothing


You can also get a file extension or file main part name as follows:
Code:
Dim myOpenFileobj, myFSOobj , myFilePath
myFilePath
=Server.MapPath("file.txt")
Set myFSOobj = Server.CreateObject("Scripting.FileSystemObject")
if myFSOobj.fileExists(myFilePath) Then 

Response
.Write "file is found"
Response.Write "<br> Extension: "& myFSOobj.GetExtensionName(FilePath)
Response.Write "<br> Name: "& myFSOobj.GetFileName(FilePath)
Response.Write "<br> Base Name: "& myFSOobj.GetBaseName(FilePath)
Response.Write "<br> Base Name: "& myFSOobj.GetBaseName("K:php_filesafile")
Else
Response.Write "File not found"
End if 
Set myFSOobj 
= Nothing



  Related Posts  to : check existence of file or folder in asp
 check folder content using asp     -  
 file exists in upper level folder link     -  
 check if file exists     -  
 java code for listing folder contents from remote folder     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 create folder in asp.net     -  
 Get all files in folder     -  
 copy folder     -  
 get Folder path and size     -  
 Zipping a Folder in java     -  

Topic Tags

ASP Files and I/O