How to find some directory and print all files, which are in it

Feb 2, 2013 at 3:01pm
Hi everyone.

I've start to learn C++ and now have one trouble.
Could someone say HOW I CAN WORK WITH DIRECTORIES FROM HARDDRIVE and, if needed, delete some choosed file or files?

I have no idei how I should start and in which way move to do this.
Feb 2, 2013 at 3:22pm
You can use Windows command line;
1
2
3
4
int main()
{
   system("command");
}
Last edited on Feb 2, 2013 at 3:23pm
Feb 2, 2013 at 3:34pm
And what I'll get?
How it works?
Can you little bit more said about this or give some useful link?
Feb 2, 2013 at 3:38pm
You can use a function llike this, search MSDN to know exactly "how it works":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bool ListDirectoryContents(const wchar_t *sDir)
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = NULL; 

    wchar_t sPath[2048]; 

    //Specify a file mask. *.* = We want everything! 
    wsprintf(sPath, L"%s\\*.*", sDir); 

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
        wprintf(L"Path not found: [%s]\n", sDir); 
        return false; 
    } 

    do
    { 
        //Find first file will always return "."
        //    and ".." as the first two directories. 
        if(wcscmp(fdFile.cFileName, L".") != 0
                && wcscmp(fdFile.cFileName, L"..") != 0) 
        { 
            //Build up our file path using the passed in 
            //  [sDir] and the file/foldername we just found: 
            wsprintf(sPath, L"%s\\%s", sDir, fdFile.cFileName); 

            //Is the entity a File or Folder? 
            if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) 
            { 
                wprintf(L"Directory: %s\n", sPath); 
                ListDirectoryContents(sPath); //Recursion, I love it! 
            } 
            else{ 
                wprintf(L"File: %s\n", sPath); 
            } 
        }
    } 
    while(FindNextFile(hFind, &fdFile)); //Find the next file. 

    FindClose(hFind); //Always, Always, clean things up! 

    return true; 
} 

ListDirectoryContents(L"C:\\Windows\\");
Feb 2, 2013 at 5:05pm
run cmd on your computer and enter command "help" as result you will have list command. what you can use in system(). for get sintaksis you must write "command /?"
Feb 2, 2013 at 8:58pm
thanks............It's very interesting method, but I want to do it in other way.......not using system("command");
I want to use library windows.h but don't know how I'll do this. Maybe someone can share any idea about it?
Feb 3, 2013 at 1:29am
Hi everyone

I've tried to wrote some code, but get one problem

I don't know how to initialize the value of szDir, which will be used in FindFirstFile function.

Can someone help me with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<Windows.h>
#include<iostream>

using namespace std;

int main(int argc, char *argv)
{
	WIN32_FIND_DATA ffd;		// structure with information about the found object
	TCHAR szDir[MAX_PATH];		//
	HANDLE hFind=INVALID_HANDLE_VALUE;	//
	
	
	// set szDir
	

	hFind=FindFirstFile(szDir,&ffd);

   if (hFind==INVALID_HANDLE_VALUE)  
   { 
      cout<<"Why this condition is TRY?\n";
	  return -1;
   } 

   // if we are here - it means that we have some object, which may be or directory or file

   return 0;
}
Feb 3, 2013 at 2:17am
I want to use library windows.h but don't know how I'll do this


You've already been supplied code for such a method.

Using the following just seems so much simpler though..
(One can use boost if your compiler doesn't support this yet.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <filesystem>
#include <iostream>
#include <string>

namespace fs = std::tr2::sys ;

int main()
{
    typedef fs::wrecursive_directory_iterator iter_type ;

    iter_type it(fs::wpath(L"C:/Projects/TestDir")), end ;

    while ( it != end )
    {
        if ( it->status().type() != fs::directory_file ) 
            std::wcout << it->path().string() << '\n' ;
        ++it ;
    }
}
Topic archived. No new replies allowed.