Total members 11895 |It is currently Sat Dec 21, 2024 6:10 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



This is ASP (Active Server Pages) code that uses the FileSystemObject to check if a folder exists in the server. The first line of code creates an object "myFileSystemObject" using the "Server.CreateObject()" method and passing the argument "Scripting.FileSystemObject", this creates an instance of the FileSystemObject, which provides methods and properties for working with files and folders on the server. Then, the code uses the "FolderExists()" method of the FileSystemObject, this method checks if a folder with the specified path exists or not. The method takes the argument of "Server.MapPath("db")", this is a built-in ASP method that returns the physical path of a virtual path on the server. So in this case, it returns the physical path of the folder called "db". Then, the code uses an if-else statement to check the result of the folderExists method, if the folder exists, the script will write "Folder is found" in the response, otherwise, it will write "Folder not found". In summary, this ASP code creates an instance of the FileSystemObject, uses it to check if a folder called "db" exists in the server and writes the result in the response.

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


It worth noting that the FileSystemObject provides a lot of other methods and properties that can be used to work with files and folders on the server. Some of the commonly used methods are:

    CopyFile: copies a file from one location to another.
    DeleteFile: deletes a file from the specified location.
    CreateFolder: creates a new folder in the specified location.
    DeleteFolder: deletes a folder from the specified location.
    MoveFile: moves a file from one location to another.
    FileExists: checks if a file exists in the specified location.
It's also worth noting that the "Server" object is a built-in ASP object that provides various methods and properties for working with the server. Some of the commonly used properties of the "Server" object are:

    MapPath: returns the physical path of a virtual path on the server.
    ScriptTimeout: gets or sets the maximum time, in seconds, that a script can run before it is terminated by the server.
It's important to note that the FileSystemObject has been deprecated in newer versions of ASP and it is recommended to use other alternatives like the System.IO namespace in .NET to work with files and folders on the server.


Please note that the FileSystemObject is a COM (Component Object Model) object, which means it is not a native part of the .NET Framework. Because of this, it may have some limitations and performance issues when used in an ASP.NET application. Another alternative to the FileSystemObject is to use the System.IO namespace, which provides a wide range of classes that can be used to work with files and folders in a more efficient and secure way. For example, the Directory and File classes provide methods that can be used to create, delete, move and check the existence of files and folders, similar to the methods provided by the FileSystemObject.
It is also worth noting that the System.IO namespace also provides classes for working with file streams and directories, such as the FileStream and DirectoryInfo classes, which provide a more advanced set of features for working with files and directories. It's also important to keep in mind that the server-side code should have the necessary permissions to perform the operations on the file system, the user that is running the application pool should have the necessary permissions to access the folder. It's also important to validate the input to the functions that are working with the file system, to prevent any type of injection or unauthorized access to the file system.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
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.     -  
 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
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
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com