Einzelnen Beitrag anzeigen

irata

Registriert seit: 5. Okt 2006
4 Beiträge
 
#3

Re: "SHFileOperation" beschwert sich bei geöffnete

  Alt 5. Okt 2006, 10:38
Hallo,

hier die "Move" Routine:
Delphi-Quellcode:
function TFmMain.MoveFile(const AOldName, ANewName: string ): boolean;
var
  sh: TSHFileOpStruct;
begin
  try
    FillChar(sh, sizeOf(TSHFileOpStruct), 0);
    sh.Wnd := Application.Handle;
    sh.wFunc := fo_Move;
    sh.pFrom := PChar(AOldName + #0);
    sh.pTo := PChar(ANewName + #0);
    sh.fFlags := fof_Silent or FOF_NOCONFIRMATION or FOF_NOERRORUI;
    Result := ShFileOperation(sh) = 0;
  except
    Result := false;
  end;
end;
und hier die Timer Routine:
Delphi-Quellcode:
procedure TFmMain.ExportTimerTimer(Sender: TObject);
begin
  if ( not FmOption.checkOptions( CheckExport ) ) then
    LogExport( 'Options empty, please check!' )
  else if ( not FmOption.RegExpEnable ) then

  else
    DoExport();

end;

procedure TFmMain.doExport();
var
  Search: TSearchRec;
  FileList: TStringList;
  FilePath: String;
  i: integer;
begin
  if ( DoingExport ) then

  else
  begin
    DoingExport := true;
    ExportCounter := ExportCounter + 1;
    FileList := TStringList.Create();

    if ( FindFirst( FmOption.RegExpPath + '\*.*', faAnyFile, Search ) = 0 ) then
    begin
      try
        repeat
          if ( (Search.Name <> '.') and
               (Search.Name <> '..')) then begin
            FileList.Add( Search.Name );
          end;
        until FindNext( Search ) <> 0;
      finally
        FindClose( Search );
      end;
    end;

    FileList.Sort();

    for i := 0 to FileList.Count - 1 do
    begin
      FilePath := FmOption.RegExpPath + '\' + FileList[i];
      exportFile( FilePath, true );
    end;

    DoingExport := false;

    if (ExportCounter mod 10 = 0 ) then
      LogExport( '--- Mark ---' );
  end;
end;

procedure TFmMain.exportFile( const filepath: string;const remove: boolean );
var
  FileName,
  FileTmp,
  Path: string;
  Stream: TIdMultiPartFormDataStream;
  Resp: TMemoryStream;
  result: Boolean;
begin
  filename := ExtractFileName( filepath );
  Stream := TIdMultiPartFormDataStream.Create;
  Resp := TMemoryStream.Create();
  result := true;
  Resp.Clear();
  Resp.Seek( 0, soFromBeginning );

  try
    if ( remove ) then
    begin
      Path := FmOption.RegTmpPath;
      FileTmp := FmOption.RegTmpPath + '\' + FileName;
      if ( not MoveFile( FilePath, FileTmp ) ) then
      begin
        LogExport( '[Error] Can''t move file ' + FilePath + ' to ' + FileTmp );
        result := false;
      end;
    end
    else
    begin
      Path := ExtractFilePath( FilePath );
      FileTmp := FilePath;
    end;

    if ( result ) then
    begin
      chdir( Path );
      Stream.AddFile( 'file', FileName, 'text/plain' );

      HTTPClient.Request.Username := FmOption.RegUsername;
      HTTPClient.Request.Password := FmOption.RegPassword;
      HTTPClient.Request.BasicAuthentication := true;
      HTTPClient.Post( 'https://' + FmOption.RegServer + '/files/import',
                       Stream, Resp );

      lbExpLastFile.Caption := 'Last filename: ' + FileName;
      lbExpLastFile.Width := 220;

    end;
  except
    on e : exception do
    begin
      LogExport( '[Error] sending file ' + FileName );
      result := false;
    end;
  end;

  Stream.Destroy();
  Resp.Seek(0,soFromBeginning);
  Resp.Free;

  if ( result ) then
    if ( remove and not RemoveFile( FileTmp ) ) then
      LogExport( '[Error] Can''t delete file ' + FileTmp )
    else
      LogExport( 'Export: ' + FileName )
  else
    if ( remove and not MoveFile( FileTmp, FilePath ) ) then
      LogExport( '[Error] Can''t export file and move file ' + FileTmp + ' to ' + FilePath )
    else
      LogExport( '[Error] Can''t export file ' + FileName );
end;
Falls noch die anderen Proceduren benötigt werden nur fragen

Danke und Gruss

Tobias
  Mit Zitat antworten Zitat