简体   繁体   中英

C++ iterating through files and directories

I'm working on a C++ program that will automatically backup my work to my FTP server. So far I am able to upload a single file, by specifying a file name using this

CString strFilePath = szFile ;
            int iPos = strFilePath.ReverseFind('\\');
            CString strFileName = strFilePath.Right((strFilePath.GetLength()- iPos-1) );

            CString strDirPath = m_szFolderDroppedIn ;
            strDirPath = strDirPath.Mid(0,strDirPath.GetLength() - 1);  
            int iPost = strDirPath.ReverseFind('\\');
            CString strDirName = strDirPath.Right((strDirPath.GetLength()- iPost -1) );

            bool curdir = ftpclient.SetServerDirectory((char*)strDirName.GetBuffer(strDirName.GetLength())); 

            //Upload to Server 
            int uploadret = ftpclient.PutFile(szFile,(char*)strFileName.GetBuffer(strFileName.GetLength()),0,true,dwLastError); 
            m_lsDroppedFiles.RemoveAll();
            break;
            }

Now I want to be able to iterate through a directory (Which contains subdirectories) and recursively call. I'm having a problem getting a hold of the files in the directory.

Any help or code snippet...

Since you are using MFC, you can use the CFileFind class. Example code is given in MSDN. Alternatively, you can use boost.filesystem for the same.

@Swapnil:如果使用boost::filesystem ,则有一个recursive_directory_iterator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM