File Management  
 

The FTP control includes a number of methods which can be used to manage files and directories on a server. In most cases, it is required that the client authenticate itself with a username and password because few servers support these functions when the client is logged in anonymously. It is also required that the user have the appropriate access rights on the server to perform the requested function. For example, the user must have the right to modify a directory in order to delete a file from that directory. If a method returns an error indicating that access was denied, verify with the server administrator that you have the correct access rights to that file or directory.

Renaming Files

The FTP control supports the ability to rename files using the RenameFile method. It take two arguments, the name of the file on the server and the new name that you want to change it to:

nError = FtpClient1.RenameFile(strOldName, strNewName)

If the file is renamed successfully, the method will return zero, otherwise it will return an error code. Note that you can also use this method to move files between directories and rename directories as well.

Deleting Files

Both the FTP and the HTTP control supports the ability to delete files using the DeleteFile method. The method accepts a single string argument, the name of the file to delete:

nError = FtpClient1.DeleteFile(strFileName)

If the file is deleted successfully, the method will return zero, otherwise it will return an error code. Deleting a file is a permanent action and there is no provision for restoring a previously deleted file. This method cannot be used to remove a directory. If this method is used with the HTTP control, it requires that the server be configured to allow file deletion. Most web servers do not permit this by default.

Creating Directories

The FTP control can be used to create new directories using the MakeDirectory method. This method accepts a single string argument which specifies the name of the directory to create:

nError = FtpClient1.MakeDirectory(strNewDirectory)

If the directory has been created successfully, the method will return zero, otherwise it will return an error code. Most servers will not create multiple subdirectories in a single operation. For example, if you specify the name "/office/projects/documents" an error will usually be returned if the directory "/office/projects" does not already exist.

It is also important to keep in mind that the file naming conventions depend on the server operating system. For example, a server running on a UNIX system uses case-sensitive file names, which means that the directory "/Office/Projects" is different than "/office/projects". On the other hand, those would refer to the same directory on a Windows server. If this distinction is important to your program, you can determine the type of server that you're connected to by checking the value of the System property.

Deleting Directories

The FTP control can be used to delete directories using the RemoveDirectory method. This method accepts a single string argument which specifies the name of the directory to delete:

nError = FtpClient1.RemoveDirectory(strDirectory)

If the directory has been deleted successfully, the method will return zero, otherwise it will return an error code. Deleting a directory is a permanent action and there is no provision for restoring a previously deleted directory. If the directory is not empty (in other words, contains one or more files or subdirectories) then the method will fail.

Checking Modification Times

If you need to determine the time that a file was last modified, both the FTP and HTTP controls provide a method called GetFileTime. This method has two arguments, the name of the file to check, and a string variable passed by reference which will contain the file date and time when the method returns. For example:

nError = FtpClient1.GetFileTime(strFileName, strFileDate)
If nError = 0 Then
    MsgBox "The file was modified on " & strFileDate
End If

The date string is formatted according to the system locale and the value of the Localize property determines if the date is adjusted for the local timezone. If the file does not exist, or the server does not support the command used to obtain the modification time for the file, an error will be returned.

Checking Permissions

The FTP control can be used to determine the access permissions that have been specified for a given file using the GetFilePermissions method. This method accepts two arguments, the name of the file to check and an integer passed by reference which will contain the file permissions when the method returns. For example:

nError = FtpClient1.GetFilePermissions(strFileName, nFilePerms)
If nError > 0 Then
    MsgBox FtpClient1.LastErrorString, vbExclamation
    Exit Sub
End If

If (nFilePerms And ftpPermOwnerRead) <> 0 Then
    MsgBox "The file " & strFileName & " can be read by the owner"
End If

The integer value returned is one or more bit flag values which contains information about the access rights for the file. For more information, refer to the table in the Technical Reference documentation. If the file does not exist, or the server does not support the command used to obtain the file permissions, an error will be returned.

Changing Permissions

In addition to checking the access rights for a file, you can also change those rights using the SetFilePermissions method in the FTP control. The following example demonstrates how to change the permissions so that only the owner can read and write to the file:

nFilePerms = ftpPermOwnerRead Or ftpPermOwnerWrite
nError = FtpClient1.SetFilePermissions(strFileName, nFilePerms)
If nError > 0 Then
    MsgBox FtpClient1.LastErrorString, vbExclamation
    Exit Sub
End If

For more information about the permission flags that can be used, refer to the Technical Reference documentation. If the file does not exist, or the server does not support the command used to change the file permissions, an error will be returned.