Question

i'm having touble using SHFileOperation do Copy and delete *.mb and *.db files

the CopyFiles code works great, copy all files and create the folder if needed but when i call DeleteFiles code something strange happens, all files in 'bkp' folder are deleted, but not the folder.

when i try to access the folder it say's "Access denied", after i close my application, folder get deleted ok.

here my procedure :

procedure TForm1.Button1Click(Sender: TObject);
var
  shFOS : TShFileOpStruct;
  FileNameTemp: string;
  sr: TSearchRec;
begin
  try
    shFOS.Wnd := Application.MainForm.Handle;
    shFOS.wFunc := FO_COPY;
    shFOS.pFrom := PChar(DBEdit4.text+'\*.db' + #0);
    shFOS.pTo := PChar(ExtractFilePath(ParamStr(0))+'bkp'+ #0);
    shFOS.fFlags := FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
    SHFileOperation(shFOS);

    shFOS.Wnd := Application.MainForm.Handle;
    shFOS.wFunc := FO_COPY;
    shFOS.pFrom := PChar(DBEdit4.text+'\*.mb' + #0);
    shFOS.pTo := PChar(ExtractFilePath(ParamStr(0))+'bkp'+ #0);
    shFOS.fFlags := FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
    SHFileOperation(shFOS);
  finally
    application.ProcessMessages;
    //zip copied files
    FilenameTemp:=ExtractFilePath(ParamStr(0))+FormatDateTime('dd-mm-yyyy-hh-nn-zzz',now)+'.zip';
    ZipForge1.FileName := FilenameTemp;
    ZipForge1.OpenArchive(fmCreate);
    ZipForge1.BaseDir := ExtractFilePath(ParamStr(0))+'bkp';
    ZipForge1.AddFiles('*.*');
    ZipForge1.CloseArchive();
  end;

// check if any files were copied in order to create the zip file and upload it
// if i skip the FindFirst code works greate
if (FindFirst(ExtractFilePath(ParamStr(0))+'bkp\*.db',faAnyFile,sr)=0) or (FindFirst(ExtractFilePath(ParamStr(0))+'bkp\*.mb',faAnyFile,sr)=0) then
    begin

    idftp1.Username:=user.Text;
    idftp1.Password:=pw.Text;
    idftp1.Port:=21;
    idFTP1.Passive := false;

    try
      idftp1.Connect;
    except
      on E : Exception do
      begin
        Show;
        if (Pos(LowerCase('user cannot'), LowerCase(E.Message)) > 0) and (Pos(LowerCase('log in.'), LowerCase(E.Message)) > 0) then
          Application.MessageBox('USUÁRIO OU SENHA INVÁLIDO',Pchar(appCaption),mb_iconError+mb_ok)
        else if (Pos(LowerCase('socket error'), LowerCase(E.Message)) > 0) and (Pos(LowerCase('host not found.'), LowerCase(E.Message)) > 0) then
          Application.MessageBox('FALHA NA CONEXÃO, VERIFIQUE SUA INTERNET',Pchar(appCaption),mb_iconError+mb_ok)
        else if e.Message<>'' then
        begin
          Application.MessageBox(Pchar('ERRO DESCONHECIDO, FAVOR ENTRAR EM CONTATO COM NOSSO SUPORTE TÉCNICO'
                                      +#13+#10
                                      +#13+#10+'INFORME O SEGUINTE ERRO :'
                                      +#13+#10
                                      +#13+#10+e.Message),Pchar(appCaption),mb_iconError+mb_ok);
        end;

        exit;
      end;
    end;

    try
     idftp1.Put(FileNameTemp,ExtractFileName(FilenameTemp));
    finally
      //DeleteFiles
      idftp1.Disconnect;
      ZeroMemory(@shFOS, SizeOf(TShFileOpStruct));
      shFOS.Wnd := Application.MainForm.Handle;
      shFOS.wFunc := FO_DELETE;
      shFOS.pFrom := PChar(ExtractFilePath(ParamStr(0))+'bkp'+#0);
      shFOS.fFlags := FOF_NOCONFIRMATION;
      SHFileOperation(shFOS); // The error occurs here, files in bkp folder are deleted
    //but the folder still exists, and everytime i try to make another backup or remove the
//folder manually through windows the error os "Access denied"
    end;
  end;
end;
Was it helpful?

Solution

The obvious problem in the updated code is that you call FindFirst, but do not match those with calls to FindClose. Quite possibly the search handles that you fail to close are what blocks the delete operation from completing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top