Showing posts with label Directory. Show all posts
Showing posts with label Directory. Show all posts

7/20/2018

get file list in the folder (example code)

example code


namespace fs = std::experimental::filesystem;

std::vector<std::string> list_files_in_dir(string dirPath)
{
 vector<string> r;
 
 for (auto & p : fs::directory_iterator(dirPath))
 {
  std::cout << p.path().string() << std::endl;
  r.push_back(p.path().string());
 }
 
 return r;
}

3/13/2014

C,C++, To get a list of files in a directory using CFileFind function, example source code


        CFileFind finder;
 CString strFileName;
 //CStringList list;
 strFiles.Format("C:\\Demo\\*.avi", pszPathname);
 BOOL bWorking = finder.FindFile(strFiles);
 while(bWorking)
 {
  bWorking = finder.FindNextFile();
  //list.AddTail(finder.GetFileName());
  ::AfxMessageBox(finder.GetFileName());
 }


C, C++, To select folder only and get path using SHBrowseForFolder function, example source code

//Get only folder name
 ITEMIDLIST *pidlBrowse;
 WCHAR pszPathname[MAX_PATH];
 
 BROWSEINFO BrInfo;
 BrInfo.hwndOwner = GetSafeHwnd();
 BrInfo.pidlRoot = NULL;
 
 CString str =_T("c:\\"); //initial path
 LPCTSTR lpszDefaultFolder = str;
 memset(&BrInfo, 0, sizeof(BrInfo));
 BrInfo.pszDisplayName=(LPSTR)pszPathname;
 BrInfo.lpszTitle=_T("Select directory");
 BrInfo.ulFlags=BIF_RETURNONLYFSDIRS; //select folder only
 //BrInfo.ulFlags=BIF_BROWSEINCLUDEFILES; //select folder and file
 BrInfo.lParam=(LPARAM)lpszDefaultFolder;
 //BrInfo.lpfn=BrowseCallback;
 
 pidlBrowse = ::SHBrowseForFolder(&BrInfo);

 
 if (pidlBrowse != NULL)
 {
  //Get Path
  SHGetPathFromIDList(pidlBrowse, (LPSTR)pszPathname);
  AfxMessageBox((LPCSTR)pszPathname);
 }


...



C,C++, Get current directory( path ), GetCurrentDirectory function

Get path
#Case 1
char reDir[500];
::GetCurrentDirectory(500,reDir);
::AfxMessageBox(reDir);

#Case 2
TCHAR buff[MAX_PATH];
memset(buff, 0, MAX_PATH);
::GetCurrentDirectory(MAX_PATH, buff);
CString strFolder = buff;





Get path & module name 
TCHAR buff[MAX_PATH]; 
 memset(buff, 0, MAX_PATH); 
 ::GetModuleFileName(NULL,buff,sizeof(buff)); 
 CString strFolder = buff; 
 strFolder = strFolder.Left(strFolder.ReverseFind(_T('\\'))+1);