How to access properties of files and folders on the server?

Accessing the properties of the files and the folders on the server can be easily accomplished with the help of the FileSystemObject methods. You need to create an instance of the FileSystemObject and then use the methods of that object. There are several properties and methods for the FileSystemObject that allows you to work with the files on the server.



We shall see some code that works on the files on a server. For this we need to have some files on the server. Assuming that you have a file called Log.txt in the same directory as that of the .asp file that has the code to access the properties of the files; the code could be something like given below:

1. <%
2. Dim objFSO, objFile
3. Set objFSO =
4. Server.CreateObject(“scripting.FileSystemObject”)
5. Set objFile = objFSO.GetFile(Server.MapPath(“Log.txt”))
6. Response.Write “Log.txt was last modified on : ”
7. Response.Write objFile.DateLastModified
8. Set objFile = Nothing
9. Set objFSO = Nothing
10. %>

The second line of the code given above has the declarations for the objects that will be used to access the properties of the files. An instance of the FileSystemObject is created in the 3rd and the 4th line. The GetFile method of the FileSystemObject is used to access the file “Log.txt”. We are using the Server.MapPath method to access the file. You know that the MapPath is used to convert the relative path to the physical path which the gives the path exactly starting with the drive name.

We use the Response.Write methods to write some text to the output and to write the date of last modification done to the file. We are using the DateLastModified property of the FileObject to get the date on which the file was last modified. This simple code gives you an idea on how to access the properties of any file in the server.

You should check the code by replacing the Line 7 of the code with other properties and methods so that you know how those properties are also accessed.

For example the Line 7 could be modified as:

· Response.Write objFile.DateCreated
This gives the date on which the file was created. The time of creation is also displayed.

· Response.Write objFile.DateLastAccessed
The gives the date on which the file was last accessed. As in the earlier case it displays the time of last access also.

· Response.Write objFile.Path
This returns the path of the file along with the filename.

· Response.Write objFile.Drive
This returns the Drive in which the file exists.

· Response.Write objFile.Size
This returns the size of the file that is accessed in bytes.

These are some of the properties that can be accessed. There are also other properties like, Name, ShortPath, ParentFolder, and Type. For more details and code samples on these you can always refer the documentation provided for these in the Microsoft website.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource



| How to Parse an XML Document using DOM | Understanding Code Management in .NET | Understanding Enterprise Transaction Services in .NET | Understanding .NET Interface-Based Programming | Understanding Security Management in Web Services | Understanding SOAP Message and Delivery Structure | Understanding the Basics of Language Integrated Query (LINQ) | Understanding the Basics of Web Services using .NET | Understanding the Basics of XPath |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.