Manipulating Directories on an FTP Server
A version of this page is also available for
4/8/2010
The Windows Internet Services (WinInet) functions provide the capability to create and remove directories on an FTP server to which the application has the necessary privileges. If the application must log on to a server with a specific user name and password, the values can be used in InternetConnect when creating the FTP session handle.
The FtpCreateDirectory function takes a valid FTP session handle and a NULL-terminated string that contains either a fully qualified path or a name relative to the current directory and creates a directory on the FTP server.
The following example shows two separate calls to FtpCreateDirectory. In both examples, hFtpSession is the session handle created by the InternetConnect function, and the root directory is the current directory.
FtpCreateDirectory(hFtpSession, "test");
/* This will create the directory "test" in the current directory,
which is the root directory. */
FtpCreateDirectory(hFtpSession, "\test\example");
/* This will create the directory "example" in the test directory. */
The FtpRemoveDirectory function takes a valid FTP session handle and a NULL-terminated string that contains either a fully qualified path or a name relative to the current directory and removes that directory from the FTP server.
The following example shows two sample calls to FtpRemoveDirectory. In both calls, hFtpSession is the session handle created by the InternetConnect function, and the root directory is the current directory. There is a directory called "test" in the root directory and a directory called "example" in the "test" directory.
FtpRemoveDirectory(hFtpSession,"\test\example");
/* Removes the "example" directory and any files or directories contained
in it from the "test" directory. */
FtpRemoveDirectory(hFtpSession, "test");
/* Removes the "test" directory and any files or directories contained
in it from the root directory. */
The following example shows how to create the directory indicated by the string stored in the IDC_FTPEdit2 edit box. The HINTERNET handle hSecondary was created by InternetConnect after establishing an FTP session. DisplayDir is another function that is designed to enumerate the directory.
int WINAPI CreateDir(HWND hX)
{
char strInFile[80];
strInFile[0] = 0;
GetDlgItemText(hX,IDC_FTPEdit2,strInFile,80);
if (strlen(strInFile)==0)
{
MessageBox(hX,"Directory Name Must Be Specified","Create Dir",MB_OK);
return 0;
}
else
{
if(!FtpCreateDirectory(hSecondary,strInFile))
{
ErrorOut(hX,GetLastError(),"Create Dir");
return 0;
}
else
{
return DisplayDir(hX,INTERNET_FLAG_RELOAD);
}
}
}
The following example shows how to delete the directory indicated by the IDC_FTPEdit2 edit box. The HINTERNET handle hSecondary was created by InternetConnect after establishing an FTP session. DisplayDir is another function that is designed to enumerate the directory.
int WINAPI RemoveDir(HWND hX)
{
char strInFile[80];
GetDlgItemText(hX,IDC_FTPEdit2,strInFile,80);
if (strlen(strInFile)==0)
{
MessageBox(hX,"Directory Name Must Be Specified!","Remove Dir",MB_OK);
return 0;
}
else
{
if(!FtpRemoveDirectory(hSecondary,strInFile))
{
ErrorOut(hX,GetLastError(),"Remove Dir");
return 0;
}
else
{
MessageBox(hX,"Directory Deleted","Remove Dir",MB_OK);
return DisplayDir(hX,INTERNET_FLAG_RELOAD);
}
}
}