1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| int CreateDir(char *rep)
{
HANDLE hdl ;
if ((hdl = CreateFile(rep, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL)) == INVALID_HANDLE_VALUE)
{
// le directory n'existe pas, on essaye de le créer
if (CreateDirectory(rep, NULL) == false)
{
// echec de la création, on teste pour savoir si le père existe
char tmp[MAX_PATH];
ZeroMemory(tmp, MAX_PATH);
char *p = strrchr(rep, '\\');
if (p != NULL)
strncpy(tmp, rep, p - rep);
else
{
sprintf(tmp, "Impossible de créer %s", rep);
Application->MessageBox(tmp, Application->Title.c_str(), MB_OK);
return 0;
}
if (CreateDir(tmp) != 0)
{
// la création du répertoire père a réussi, on réessaye
if (CreateDirectory(rep, NULL) == false)
{
sprintf(tmp, "Impossible de créer %s", rep);
Application->MessageBox(tmp, Application->Title.c_str(), MB_OK);
return 0;
}
//reussite
return 1;
}
// echec création
return 0;
}
// création réussie
return 1;
}
// le répertoire existe déjà, pas la peine de le créer
return 2;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int ret = CreateDir("E:\\rep0l\\rep1\\rep2\\rep3");
switch (ret)
{
case 0: Application->MessageBox("Echec de la création", Application->Title.c_str(), MB_OK); break;
case 1: Application->MessageBox("Réussite de la création", Application->Title.c_str(), MB_OK); break;
case 2: Application->MessageBox("Répertoire existant déjà", Application->Title.c_str(), MB_OK); break;
}
} |
Partager