Drives - formatando via API

Top  Previous  Next

There is an API hidden away in Shell32.dll called SHFormatDrive, this brings up the standard format removable drive dialog. I stumbled across this in the borland.public.delphi.winapi newsgroup.

--------------------------------------------------------------------------------

const

        SHFMT_ID_DEFAULT        = $FFFF;

        // Formating options

        SHFMT_OPT_QUICKFORMAT   = $0000;

        SHFMT_OPT_FULL          = $0001;

        SHFMT_OPT_SYSONLY       = $0002;

        // Error codes

        SHFMT_ERROR             = $FFFFFFFF

        SHFMT_CANCEL            = $FFFFFFFE

        SHFMT_NOFORMAT          = $FFFFFFFD

 

function SHFormatDrive(Handle: HWND; Drive, ID, Options: Word): LongInt;

                       stdcall; external 'shell32.dll' name 'SHFormatDrive'

 

procedure TForm1.btnFormatDiskClick(Sender: TObject);

var

  retCode: LongInt;

begin

  retCode:= SHFormatDrive(Handle, 0, SHFMT_ID_DEFAULT, SHFMT_OPT_QUICKFORMAT);

  if retCode < 0 then ShowMessage('Could not format drive');

end;

--------------------------------------------------------------------------------

How to del ALL files within directory

From: TM 

 

Try this: 

--------------------------------------------------------------------------------

 

procedure TfrmMain.DelDir(DirName: string);

var

        SearchRec: TSearchRec;

        GotOne: integer;

begin

        GotOne:= FindFirst(DirName + '\*.*', faAnyFile, SearchRec);

        while GotOne = 0 do

        begin

                if ((SearchRec.Attr and faDirectory) = 0) then

                        DeleteFile(DirName + '\' + SearchRec.Name)

                        else if (SearchRec.Name <> '.'and (SearchRec.Name <> '..') then

                                DelDir(DirName + '\' + SearchRec.Name);

                GotOne:= FindNext(SearchRec);

        end;

        FindClose(SearchRec);

end;

 

 

--------------------------------------------------------------------------------

If you want to delete the directory afterwards, you could do something like this: 

--------------------------------------------------------------------------------

 

//--------

        DelDir('C:\WASTE');

        {-I}

        RmDir('C:\WASTE');

        {+I}

        if IOResult <> 0 then

                raise Exception.Create('Error removing directory');

//-------