0

SHFileOperation の使用に問題があります *.mb および *.db ファイルをコピーして削除します

CopyFiles コードはうまく機能し、必要に応じてすべてのファイルをコピーしてフォルダーを作成しますが、DeleteFiles コードを呼び出すと、「bkp」フォルダー内のすべてのファイルが削除されますが、フォルダーは削除されません。

「アクセスが拒否されました」と表示されているフォルダにアクセスしようとすると、アプリケーションを閉じた後、フォルダが削除されます。

ここで私の手順:

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;
4

1 に答える 1