dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Re: Opening and closing directories - sorry

  1. #1
    Luis E. Cuadrado Guest

    Re: Opening and closing directories - sorry


    Hi Pete:
    Thanks for the quick reply. I notice that you used MFC. Is that the only
    way to deal with searching the whole hard drive? This means there is no way
    to try to do it using STL? I really appreciate the code that you sent me.
    I'm going to take a closer look at it. Looks kind of interesting. Again,
    thank you and hope to hear from you soon.

    have a nice day,
    Luis E.



    "Luis E. Cuadrado" luiscuadrado wrote:
    >
    >"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;
    >>}
    >>

    >



  2. #2
    Danny Kalev Guest

    Re: Opening and closing directories - sorry



    "Luis E. Cuadrado" wrote:
    >
    > Hi Pete:
    > Thanks for the quick reply. I notice that you used MFC. Is that the only
    > way to deal with searching the whole hard drive? This means there is no way
    > to try to do it using STL?

    STL doesn't address this issue directly but there are certainly other
    ways to traverse a directory. Look at the <dos.h> header (if you intend
    to create a console app without fancy GUI) or the <dir.h> library which
    is not a part of the ANSI standard but it's a de factor standard in the
    POSIX world and has been adapted to Windows too so it's quite portable.

    Danny

  3. #3
    Pete Guest

    Re: Opening and closing directories - sorry


    You're welcome :)

    I wish I knew the other ways to traverse a directory, but I don't sorry.
    Maybe Danny's answer will help you out. I'm sure there must be a way to
    do it, as apps were written before MFC emerged.

    If you figure it out, I'd be interested in seeing a code snippet of how you
    do it without including afx.h

    -- Pete


  4. #4
    SteveJM Guest

    Re: Opening and closing directories - sorry


    "Pete" <grandpete@hotmail.com> wrote:
    >
    >You're welcome :)
    >
    >I wish I knew the other ways to traverse a directory, but I don't sorry.
    > Maybe Danny's answer will help you out. I'm sure there must be a way to
    >do it, as apps were written before MFC emerged.
    >
    >If you figure it out, I'd be interested in seeing a code snippet of how

    you
    >do it without including afx.h
    >
    >-- Pete
    >


    try the 32 bit _find functions: _findfirst, _findnext, _findclose. The following
    example is taken directly from the MSDN Library.

    /* FFIND.C: This program uses the 32-bit _find functions to print
    * a list of all files (and their attributes) with a .C extension
    * in the current directory.
    */

    #include <stdio.h>
    #include <io.h>
    #include <time.h>

    void main( void )
    {
    struct _finddata_t c_file;
    long hFile;

    /* Find first .c file in current directory */
    if( (hFile = _findfirst( "*.c", &c_file )) == -1L )
    printf( "No *.c files in current directory!\n" );
    else
    {
    printf( "Listing of .c files\n\n" );
    printf( "\nRDO HID SYS ARC FILE DATE %25c SIZE\n", '
    ' );
    printf( "--- --- --- --- ---- ---- %25c ----\n", ' '
    );
    printf( ( c_file.attrib & _A_RDONLY ) ? " Y " : " N " );
    printf( ( c_file.attrib & _A_SYSTEM ) ? " Y " : " N " );
    printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " );
    printf( ( c_file.attrib & _A_ARCH ) ? " Y " : " N " );
    printf( " %-12s %.24s %9ld\n",
    c_file.name, ctime( &( c_file.time_write ) ), c_file.size
    );

    /* Find the rest of the .c files */
    while( _findnext( hFile, &c_file ) == 0 )
    {
    printf( ( c_file.attrib & _A_RDONLY ) ? " Y " : " N " );
    printf( ( c_file.attrib & _A_SYSTEM ) ? " Y " : " N " );
    printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " );
    printf( ( c_file.attrib & _A_ARCH ) ? " Y " : " N " );
    printf( " %-12s %.24s %9ld\n",
    c_file.name, ctime( &( c_file.time_write ) ), c_file.size
    );
    }

    _findclose( hFile );
    }
    }


    Output

    Listing of .c files

    RDO HID SYS ARC FILE DATE SIZE
    --- --- --- --- ---- ---- ----
    N N N Y CWAIT.C Tue Jun 01 04:07:26 1993 1611
    N N N Y SPRINTF.C Thu May 27 04:59:18 1993 617
    N N N Y CABS.C Thu May 27 04:58:46 1993 359
    N N N Y BEGTHRD.C Tue Jun 01 04:00:48 1993 3726



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