Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Management API

Status
Not open for further replies.

diskord

Programmer
Joined
Dec 13, 2002
Messages
1
Location
GB
Does anybody know if there is either a good way in Win32 to remove a directory on the hard drive in one go if it contains other directory's & files, or, if there is a nice API out there which has good file management.

Any help would be appreciated, thanks :)
 
Code:
#include <windows.h>
#include <stdio.h>

BOOL DelTree ( char *Dir )
{
  DWORD Attr = GetFileAttributes ( Dir );

  if ( Attr == 0xffffffff )
     { return FALSE; }

  if ( !(Attr & FILE_ATTRIBUTE_DIRECTORY )) // Not a directory
     { SetLastError ( 3 );
       return FALSE; }

  WIN32_FIND_DATA wfd;
  char DirContents[MAX_PATH];

  strcpy ( DirContents, Dir   );
  strcat ( DirContents, &quot;\\*&quot; );

  HANDLE FH = FindFirstFile ( DirContents, &wfd );

  if ( FH != INVALID_HANDLE_VALUE )
     {
       do {
            strcpy ( DirContents, Dir );
            strcat ( DirContents, &quot;\\&quot; );
            strcat ( DirContents, wfd.cFileName );

            if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
               {
                 if ( strcmp ( wfd.cFileName, &quot;.&quot;  ) != 0 &&
                      strcmp ( wfd.cFileName, &quot;..&quot; ) != 0    )
                    { if ( !DelTree ( DirContents ))
                         { FindClose ( FH );
                           return FALSE; } } } else
               { if ( !DeleteFile ( DirContents ))
                    { FindClose ( FH );
                      return FALSE; } }

          } while ( FindNextFile ( FH, &wfd ));
       FindClose ( FH ); }

  return RemoveDirectory ( Dir ); }

void main ( int argc, char *argv[] )
{
  if ( argc < 2 )
     { printf ( &quot;Usage: %s Dirname [Dirname ...]\n&quot;, argv[0] );
       exit ( 1 ); }
  for ( int i1 = 1; i1 < argc; i1++ )
     { printf ( &quot;Removing directory %s ...&quot;, argv[i1] );
       if ( DelTree ( argv[i1] ))
          { printf ( &quot;OK\n&quot; ); } else
          { printf ( &quot;Error %d\n&quot;, GetLastError ( )); } } }

Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top