Total members 11644 |It is currently Tue Jun 27, 2017 6:27 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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





Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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.     -  
 Get all files in folder     -  
 copy folder     -  
 create folder in asp.net     -  
 get Folder path and size     -  
 Zipping a Folder in java     -  



Topic Tags

ASP Files and I/O
cron






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team