Manipulating Files and Directories

In addition to uploading and downloading files, you can use ActiveFile for general purpose file and directory manipulation using a variety of methods provided by the File and Directory object.

Manipulation Topics

Manipulating files
Manipulating directories
Securing files and directories

 

  Manipulating files

When you upload files, a FormInput object is added to the Post.FormInputs collection for each file. Referencing these files is as simple as referencing the File property of the FormInput object. The following script demonstrates how to display information about an uploaded file:

<%
    ' Upload a file
    Set Post = Server.CreateObject("ActiveFile.Post")
    Post.Upload "C:\TEMP"

    ' Display information about the file
    Response.Write "Path = " & Post.FormInputs("UpFile").File & "<BR>"
    Response.Write "Size = " & Post.FormInputs("UpFile").File.Size & "<BR>"
    Response.Write "Type = " & Post.FormInputs("UpFile").File.FileType & "<BR>"
%>

However, you may need to access files on the server that were not just uploaded. In these cases, you can create a File object and initialize it to point to the specific file you are interested. The following script demonstrates how to access an arbitrary file using only a File object:

<%
    ' Initialize a File object to point at a file
    Set File = Server.CreateObject("ActiveFile.File")
    File.Name = "C:\myfile.xls"

    ' Display information about that file
    Response.Write "Path = " & File & "<BR>"
    Response.Write "Size = " & File.Size & "<BR>"
    Response.Write "Type = " & File.FileType & "<BR>"
%>

By initializing the File.Name property to a path, you can access any arbitrary file as long as you have the necessary permissions. See Security Considerations for more details.

Once you have a File object that is referencing a file, you can manipulate it in a variety of ways including renaming, copying, and deleting. For more details and examples see the File object reference.

 

  Manipulating directories

Using ActiveFile, you can access any local or network drive that accessible from your server as long as you have the necessary permissions. The root directory of all drives that are associated with drive letters can be access using the Computer.Drives collection as in the following example:

<%
    ' Initialize a Computer object
    Set Computer = Server.CreateObject("ActiveFile.Computer")

    ' Display each drive path
    For Each Directory In Computer.Drives
        Response.Write Directory.Path & "<BR>"
    Next
%>

In some cases you may need to access a specific directory to locate files or other information. The following script demonstrates how to access an arbitrary directory using a Directory object:

<%
    ' Initialize a Directory object to point at a directory
    Set Directory = Server.CreateObject("ActiveFile.Directory")
    Directory.Path = "C:\My Documents"

    ' Display contents of the directory
    For Each File In Directory.Files
        Response.Write File & "<BR>"
    Next
%>

By initializing the Directory.Path property to a path, you can access any arbitrary directory as long as you have the necessary permissions. See Security Considerations for more details.

Once you have a Directory object that is referencing a directory, you can manipulate it in a variety of ways including renaming and deleting. For more details and examples see the Directory object reference.

 

  Securing files and directories

Both the File and Directory object have an ACEs collection. The ACEs collection represents the list of users and groups that can access the file or directory and what priviledges they have been granted. By default, the IIS anonymous account does not have permission to access security information. In the example below, the Computer.Impersonate method is used to login under a different NT account that has the necessary permissions.

<%
    ' Log in under administrator account
    Set Computer = Server.CreateObject("ActiveFile.Computer")
    Computer.Impersonate "Administrator", "yourpassword"

    ' Initialize a Directory object to point at a directory
    Set Directory = Server.CreateObject("ActiveFile.Directory")
    Directory.Path = "C:\"

    ' Display contents of the directory
    For Each File In Directory.Files
        Response.Write File & "<BR>"
    Next

    Computer.EndImpersonate
%>

Note that it is possible to use an account other than the administrator account to access security information. Even the IIS anonymous account can be configured to be used for this purpose. See Security Considerations for more information.

In addition to accessing security information, is it also possible to add and update information in the ACEs collection. The following example demonstrates how to give a particular user read access to a file.

<%
    ' Log in under administrator account
    Set Computer = Server.CreateObject("ActiveFile.Computer")
    Computer.Impersonate "Administrator", "yourpassword"

    ' Initialize a File object to point to the file
    Set File = Server.CreateObject("ActiveFile.File")
    File.Name = "C:\myfile.dat"

    ' Give user FOO read access
    If File.ACEs.Exists("FOO") Then
        File.ACEs("FOO").Permissions = File.ACEs("FOO").Permissions Or AF_PERMIT_READ
    Else
        File.ACEs.Add "FOO", AF_PERMIT_READ
    End If

    Computer.EndImpersonate
%>
File Upload File Download File Upload File Download File Upload File Download File Upload File Download File Upload File Download File Upload File Download ASP .NET Drag n Drop Java Mac OS X