FTP operation code example

Source: Internet
Author: User

In Windows programming, FTP operations are often required.

The following is a summary of some sample code:

1 //
2 //************************************* ********************
3 // FTP basic operation
4 //************************************* ********************
5 //
6 //
7 // 1. Connect to FTP
8 //
9 bool flag;
10 cstring cstrftpserver = text ("10.142.252.155"); // FTP server address
11 cstring cstrftpusername = text ("pdmug"); // User Name
12 cstring cstrftppassword = text ("pdmuguser"); // Password
13 cinternetsession * m_pinternetsession = NULL;
14 cftpconnection * m_pftpconnection = NULL;
15
16 try
17 {
18 m_pinternetsession = new cinternetsession ();
19 m_pftpconnection = m_pinternetsession-> getftpconnection (cstrftpserver,
20 cstrftpusername, cstrftppassword, 21); // 21 --- FTP port
21}
22 catch (cinternetexception * PEX) // error: Can not connect to specific FTP
23 {
24 if (m_pinternetsession! = NULL)
25 {
26 Delete m_pinternetsession;
27}
28 If (m_pftpconnection! = NULL)
29 {
30 Delete m_pftpconnection;
31}
32
33 return;
34}
35
36 //
37 // 2. Get Current Directory
38 //
39 cstring cstrcurrdir;
40 flag = m_pftpconnection-> getcurrentdirectory (cstrcurrdir );
41 if (! Flag) // get current directory Error
42 {
43}
44
45 //
46 // 3. Set Current Directory
47 //
48 cstring cstrnewcurrdir = text ("// pdmpv/gox/back_cover /");
49 flag = m_pftpconnection-> setcurrentdirectory (cstrnewcurrdir );
50 if (! Flag) // set current directory Error
51 {
52}
53
54 //
55 // 4. Download file from ftp
56 //
57 flag = m_pftpconnection-> GetFile (text ("ca110900_2nd_md.ol "),
58 text ("d :\\ 123.ol "),
59 true );
60 if (! Flag) // download file fail
61 {
62}
63
64 //
65 // 5. Upload File to FTP
66 //
67 flag = m_pftpconnection-> putfile (text ("D: \ 123.txt"), text (" 456.txt "));
68 if (! Flag) // Upload File fail
69 {
70}
71
72 //
73 // 6. Rename file on FTP
74 //
75 flag = m_pftpconnection-> Rename (text ("456.txt"), text (" 456_wy.txt "));
76 if (! Flag) // rename file fail
77 {
78}
79
80 //
81 // 7. Remove file on FTP
82 //
83 flag = m_pftpconnection-> remove (text ("456.txt "));
84 If (! Flag) // Remove File fail
85 {
86}
87
88 //
89 // 8. create directory on FTP
90 //
91 flag = m_pftpconnection-> createdirectory (text ("wangyao "));
92 If (! Flag) // create directory on FTP fail
93 {
94}
95
96 //
97 // 9. Remove directory on FTP
98 // Note: directory must be empty or will cause Error
99 //
100 flag = m_pftpconnection-> removedirectory (text ("wangyao "));
101 if (! Flag) // remove directory on FTP fail
102 {
103}
104
105 //
106 // 10. Do not forget to free resource
107 //
108 Delete m_pinternetsession;
109 Delete m_pftpconnection;
110
111
112 //
113 //************************************* ********************
114 // FTP file Finder
115 //************************************* ********************
116 //
117 //
118 // 1. above: connect to FTP
119 //
120
121 //
122 // 2. above: Set Current Directory
123 //
124
125 //
126 // 3. Find file (refer to cfilefind)
127 //
128 cftpfilefind ffinder (m_pftpconnection );
129 bool bfind = ffinder. findfile (text ("*.*"));
130 while (bfind)
131 {
132 bfind = ffinder. findnextfile ();
133
134 // The current folder and the upper-layer folder (names are. And ..)-----------------
135 if (ffinder. isdots ())
136 {
137 continue;
138}
139
140 // subfolders ---------------------------------------------
141 If (ffinder. isdirectory ())
142 {
143 cstring cstrdirname = ffinder. getfilename (); // directory name
144 cstring cstrdirpath = ffinder. getfilepath (); // directory path
145 continue;
146}
147
148 // file -------------------------------------------------
149 cstring cstrfilename = ffinder. getfilename (); // file name
150 cstring cstrfilepath = ffinder. getfilepath (); // file path
151}
152
153 ffinder. Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.