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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I delete a directory with all its files ????

Status
Not open for further replies.

colosoderada

Programmer
Sep 14, 2000
26
0
0
ES
I am trying to delete a directory with all its files but somethings this error appears.

The file can´t be eliminated. System error (1026)
could anybody explain me that this error means???
How can I remove it????

Thi is my code, sometimes works wells, but other the program give me the error (1026).

If I make a directory for instance c:\kk and I use the program, the program works properly but if I make the directory C:\1234 the program can´t delete it and everytime that I have tried it the program give the error (1026) always with this directory.

Anyway, Thanks in advance.

Unit.h

//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TEdit *Nombre;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
AnsiString FicheroBorrar;
bool A;
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#include <Shellapi.h>
#include <Filectrl.hpp>
#pragma hdrstop
#include &quot;Unit1.h&quot;
#include &quot;Unit2.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
A = false;
try{
if(DirectoryExists(Nombre->Text)){ // EL directorio existe.
FicheroBorrar = Nombre->Text;
Form2->ShowModal();
if (A == true) // Se ha pulsado Aceptar
{
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = Application->Handle;
FileOp.wFunc = FO_DELETE;
FileOp.pFrom = Nombre->Text.c_str();
FileOp.pTo = NULL;
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = 0;
FileOp.lpszProgressTitle = NULL;
bool a = SHFileOperation(&FileOp); //Mando a la papelera los datos
A = false;
}
}
}
catch(...){
}
}
//--------------------------------------------

Unit2.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//#include &quot;Unit1.h&quot;
#include &quot;Unit2.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm2 *Form2;
extern bool A;
extern AnsiString FicheroBorrar;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
A = true;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormShow(TObject *Sender)
{
Label2->Caption = FicheroBorrar;
}
//---------------------------------------------------------------------------



--------------------------------------------------------------------------------
 
I don't know if this will help or not. Try using the following:
Code:
int OldErrMode; // use to reset error mode to original mode
OldErrMode = SetErrorMode(SEM_FAILCRITICALERRORS); // get original mode

SHFILEOPSTRUCT FileOp; 
FileOp.hwnd = Application->Handle; 
FileOp.wFunc = FO_DELETE; 
FileOp.pFrom = Nombre->Text.c_str(); 
FileOp.pTo = NULL; 
FileOp.fFlags = FOF_NOCONFIRMATION; 
FileOp.hNameMappings = 0; 
FileOp.lpszProgressTitle = NULL; 
bool a = SHFileOperation(&FileOp); //Mando a la papelera los datos 
A = false; 

int TheError = GetLastError();
// You will need to display the error somehow
SetErrorMode(OldErrMode); // reset error mode

[tab]The error returned by TheError will be a code in WINERROR.H. You may have to do a search for this. I found mine in the CBuilder directory. Hope this helps.

[tab]By the way this came from June 1998 C++ Builder Developer's Journal. James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top