dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Opening and closing directories

  1. #1
    Luis E. Cuadrado Guest

    Opening and closing directories


    Hello:
    I was wondering if there is a way in C++ to open and close directories.
    I'm thinking to make a program that looks for files in the hard drive. Any
    answers is welcome.

    Have a nice day,

    Luis E.

  2. #2
    Pete Guest

    Re: Opening and closing directories


    Hi Luis,

    Here's a little hack I wrote to read through directories on your hard drive.
    I call it a "hack" because it uses printf() and its ilk for output rather
    than cout because CStrings don't display correctly with cout on the version
    of VC6 I'm running (haven't put in SP4 yet). Anyways, here it is.
    Be sure to tell the compiler to use MFC in a shared library or you'll get
    linker errors when you compile.

    Enjoy!
    *****************
    #include <afx.h>
    #include <string>
    #include <list>
    using namespace std;

    class Filenames {
    public:
    CString Filename;
    };

    void ShowDir(CString &Dir);

    void main()
    {
    // Be sure to use the double backslash between directories or
    // the compiler will misinterpret them. Remember \\ resolves
    // to a single \ when the string is compiled.
    CString Dir = "C:\\Temp"; // Set this to some small test folder of yours.

    ShowDir(Dir);
    }

    // How much should this directory be indented? Must be global.
    int indent = -2;

    void ShowDir(CString &Dir)
    {
    Filenames Work;
    list <Filenames> NameList;
    list <Filenames>::const_iterator it;
    int i;
    CString tmp;
    CFileFind finder;
    BOOL bWorking;

    indent += 2;
    bWorking = finder.FindFile(Dir + "\\*.*");
    while(bWorking)
    {
    bWorking = finder.FindNextFile();
    tmp = finder.GetFileName();
    if(tmp != "." && tmp != "..") { // Ignore "." and ".." files
    for(i = 0; i<indent; i++)
    putchar(' ');
    printf("%s",tmp);
    if(finder.IsDirectory()) { // Lots of other methods available too.
    Work.Filename = tmp;
    NameList.push_back(Work);
    printf(" <dir>\n");
    }
    else
    printf(" (%d)\n",finder.GetLength());
    }
    }

    // Only now can we call ourselves to look at subdirectories.
    // If we did this above, the current finder class would loose its place.
    for(it = NameList.begin(); it != NameList.end(); it++) {
    CString SubDir = Dir + "\\" + it->Filename;
    ShowDir(SubDir);
    }
    indent -= 2;
    }


  3. #3
    Luis E. Cuadrado luiscuadrado Guest

    Re: Opening and closing directories


    "Pete" <grandpete@hotmail.com> wrote:
    >
    >Hi Luis,
    >
    >Here's a little hack I wrote to read through directories on your hard drive.
    > I call it a "hack" because it uses printf() and its ilk for output rather
    >than cout because CStrings don't display correctly with cout on the version
    >of VC6 I'm running (haven't put in SP4 yet). Anyways, here it is.
    >Be sure to tell the compiler to use MFC in a shared library or you'll get
    >linker errors when you compile.
    >
    >Enjoy!
    >*****************
    >#include <afx.h>
    >#include <string>
    >#include <list>
    >using namespace std;
    >
    >class Filenames {
    >public:
    > CString Filename;
    >};
    >
    >void ShowDir(CString &Dir);
    >
    >void main()
    >{
    > // Be sure to use the double backslash between directories or
    > // the compiler will misinterpret them. Remember \\ resolves
    > // to a single \ when the string is compiled.
    > CString Dir = "C:\\Temp"; // Set this to some small test folder of yours.
    >
    > ShowDir(Dir);
    >}
    >
    >// How much should this directory be indented? Must be global.
    >int indent = -2;
    >
    >void ShowDir(CString &Dir)
    >{
    > Filenames Work;
    > list <Filenames> NameList;
    > list <Filenames>::const_iterator it;
    > int i;
    > CString tmp;
    > CFileFind finder;
    > BOOL bWorking;
    >
    > indent += 2;
    > bWorking = finder.FindFile(Dir + "\\*.*");
    > while(bWorking)
    > {
    > bWorking = finder.FindNextFile();
    > tmp = finder.GetFileName();
    > if(tmp != "." && tmp != "..") { // Ignore "." and ".." files
    > for(i = 0; i<indent; i++)
    > putchar(' ');
    > printf("%s",tmp);
    > if(finder.IsDirectory()) { // Lots of other methods available too.
    > Work.Filename = tmp;
    > NameList.push_back(Work);
    > printf(" <dir>\n");
    > }
    > else
    > printf(" (%d)\n",finder.GetLength());
    > }
    > }
    >
    > // Only now can we call ourselves to look at subdirectories.
    > // If we did this above, the current finder class would loose its place.
    > for(it = NameList.begin(); it != NameList.end(); it++) {
    > CString SubDir = Dir + "\\" + it->Filename;
    > ShowDir(SubDir);
    > }
    > indent -= 2;
    >}
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center