Monday 4 October 2010

ASP: Scripting.FileSystemObject... how to read file or folder information

   


As promised I would like to explain other useful methods related to the Scripting.FileSystemObject. Infact you can obtain information on a file by simply calling different methods. As I wrote a few days ago in the "ASP: Scripting.FileSystemObject... how to move, copy and delete a file or a folder" post, just start by setting the object:
<%
Dim FSO
   Set FSO = Server.CreateObject("Scripting.FileSystemObject")
and the target file:
Dim varfile
Dim filepath
Dim fileinfo
   filepath = yourfilepath
   varfile = Server.MapPath(filepath)
The yourfilepath variable could be something like "/doc/abc.txt". With the Server.MapPath we determine the complete path.
Now we use the GetFile method:
Set fileinfo = FSO.GetFile(varfile)
And there you go; all the information are available through the fileinfo object.
With fileinfo
   Response.write ("Name: " & .Name & "<br>" & _
   "Short name: " & .ShortName & "<br>" & _
   "Size: " & .Size & "<br>" & _
   "Type: " & .Type & "<br>" & _
   "Attributes: " & .Attributes & "<br>" & _
   "Drive: " & .Drive & "<br>" & _
   "Path: " & .Path & "<br>" & _
   "Short path: " & .ShortPath & "<br>" & _
   "Folder: " & .ParentFolder & "<br>" & _
   "Creation date: " & .DateCreated & "<br>" & _
   "Last modification date: " & .DateLastModified & "<br>" & _
   "Last access date: " & .DateLastAccessed)
End With
For the extension of the file, you can use:
Response.write FSO.GetExtensionName(varfile)
Or check if the file exists:
Response.write FSO.GetExtensionName(varfile)
All the given examples can be used to retrieve information of a folder as well, just remember that instead of "file" we need to use "folder"; for example we use GetFolder instead of GetFile, FolderExists instead of FileExists and so on.
Set Folder = FSO.GetFolder("C:\doc\")
  With Folder
   Response.write ("Name: " & .Name & "<br>" & _
   "Short name: " & .ShortName & "<br>" & _
   "Creation date: " & .DateCreated & "<br>" & _
   "Attributes: " & .Attributes & "<br>" & _
   "Last access: " & .DateLastAccessed & "<br>" & _
   "Last modification date: " & .DateLastModified & "<br>" & _
   "Drive: " & .Drive & "<br>" & _
   "Is root folder: " & .IsRootFolder & "<br>" & _
   "Parent folder: " & .ParentFolder & "<br>" & _
   "Short path: " & .ShortPath & "<br>" & _
   "Size: " & .Size & "<br>" & _
   "Type: " & .Type)
End With
Other interesting methods are GetFileName, GetParentFolderName, GetBaseName and BuildPath. I think the methods are quite self explanatory, except the BuildPath that is used to obtain the complete path of a file, starting from the root folder:
Response.write FSO.BuildPath(folder, filename)
where folder could be "C:/doc" and filename "abc.txt".
In the end you could find useful the GetDrive method, which gets the information on a specific drive:
Set Drive = FSO.GetDrive("C:")
  With Drive
   Response.write ("Available space in bytes: " & .AvailableSpace & "<br>" & _
   "Free space in bytes: " & .FreeSpace & "<br>" & _
   "Drive letter: " & .DriveLetter & "<br>" & _
   "Drive type: " & .DriveType & "<br>" & _
   "File system (FAT, NTFS, CDFS): " & .FileSystem & "<br>" & _
   "Ready: " & .IsReady & "<br>" & _
   "Root folder: " & .RootFolder & "<br>" & _
   "Serial number: " & .SerialNumber & "<br>" & _
   "Share name: " & .ShareName & "<br>" & _
   "Total size: " & .TotalSize & "<br>" & _
   "Volume name: " & .VolumeName)
  End With
%>
As you can see, it's really quite simple to create a file management system. Sometimes in your web application you can apply some of this methods according to your needs. Sometimes could be dangerous because of security issues, but knowing that you can do it, and how to do it, might be very handy.
IMPORTANT: remember that when working with the Scripting.FileSystemObject, you should consider the related security issues. If you have problems in using the code above, remember that read and/or write permissions of the working folder must be given to the "IUSR" account. Use it with great care and attention!
Related posts:
ASP: Scripting.FileSystemObject... how to move, copy and delete a file or a folder
JQuery: 5 useful file upload plugins
VBScript: How to check if a file exists on a remote server

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.