|
|
| D: | Domains | Authors.aspalliance.com | Stevesmith | Articles | File Access Tutorial |
|
File Access Tutorial [Example]
The File Access Component of ASP allows you to manipulate files on your server using ASP code. The first thing you should understand is what objects are available, and what their properties are. They are Drive, File, FileSystem, and Folder. There is also a Drives collection, a Files collection, and a Folders collection. Finally, the Textstream object is useful for moving data to and from files. Current specs for these objects are available from Microsoft's web site; you can click here to see a pop-up window with a relatively complete listing of the objects and their properties and methods. The first thing that you need to do in order to access files is create the necessary objects. For the purpose of this tutorial, let's say we want to be able to generate a list of files in a particular directory, with some size and other information. The first thing we will need to do is create a filesystem object, then pull the particular folder out that we are interested in:
'Create our filesystem object
Set objFS = Server.CreateObject("Scripting.FileSystemObject")
'Get this folder
FolderPath=Request.ServerVariables("APPL_PHYSICAL_PATH") & "articles"
Set objFolder = objFS.GetFolder(FolderPath)
Set objFiles = objFolder.Files
As you can see, this code leaves us with two objects we can use to get all of the file system information about this directory. objFolder is a Folder object referencing the folder itself, and objFiles is a Files collection of all of the files in the directory. We will use a For Each ... Next loop to reference the elements of this collection later in the example. Next, let's display the folder info. We'll use a small HTML table for this: 1 <TABLE BORDER=1 BGCOLOR="#CCCCCC">2 <CAPTION><B>Directory:</B></CAPTION> 3 <TR BGCOLOR="lightblue"> 4 <TH>Property</TH> 5 <TH>Value</TH> 6 </TR> 7 <TR> 8 <TD>IsRootFolder</TD> 9 <TD><%=objFolder.IsRootFolder%></TD> 10 </TR> 11 <TR> 12 <TD>Size</TD> 13 <TD><%'=objFolder.Size%></TD> 14 </TR> 15 <TR> 16 <TD>Name</TD> 17 <TD><%=objFolder.Name%></TD> 18 </TR> 19 <TR> 20 <TD>Path</TD> 21 <TD><%=FolderPath%></TD> 22 </TR> 23 </TABLE> Then, for the files, the only real change is the addition of the For Each ... Next loop. 1 <TABLE BORDER=1 BGCOLOR="#CCCCCC">2 <CAPTION><B>Files:</B></CAPTION> 3 <TR BGCOLOR="lightblue"> 4 <TH>Name</TH> 5 <TH>Path</TH> 6 <TH>Drive</TH> 7 <TH>Size</TH> 8 <TH>Attributes</TH> 9 <TH>Created</TH> 10 <TH>Last Accessed</TH> 11 <TH>Last Modified</TH> 12 <TH>Parent Folder</TH> 13 <TH>Short Name</TH> 14 <TH>Short Path</TH> 15 <TH> 16 </TR> 17 <% For Each objFile In objFiles %> 18 <TR> 19 <TD><%=objFile.Name%></TD> 20 <TD><%=objFile.Path%></TD> 21 <TD><%=objFile.Drive%></TD> 22 <TD><%=objFile.Size%></TD> 23 <TD><%=objFile.Attributes%></TD> 24 <TD><%=objFile.DateCreated%></TD> 25 <TD><%=objFile.DateLastAccessed%></TD> 26 <TD><%=objFile.DateLastModified%></TD> 27 <TD><%=objFile.ParentFolder%></TD> 28 <TD><%=objFile.ShortName%></TD> 29 <TD><%=objFile.ShortPath%></TD> 30 </TR> 31 <% Next %> 32 </TABLE> |
|
|
|
|
|
|
|