Ver Mensaje Individual
  #1  
Antiguo 14-07-2015
noobdelphi5 noobdelphi5 is offline
Miembro
NULL
 
Registrado: ene 2015
Posts: 22
Reputación: 0
noobdelphi5 Va por buen camino
Smile Copiar Archivos de carpetas compartidas

Buen día

Intento hacer un service que copie archivos de carpetas compartidas en cierto tiempo, no me funciona con carpetas compartidas.

Utilizo Delphi 7, el servicio sería para Win XP y 7 y tomaré el numero de archivos para hacer unas validaciones. También quisiera poder tomar las fechas pero no se de que manera hacerlo, he buscado cómo hacerle pero no me resulta.

Aquí pegó el código de lo que llevo:

Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  ExtCtrls, ShellAPI, Variants, Forms;

type
  TPasarFacturas = class(TService)
    temporizador: TTimer;
    procedure ServiceExecute(Sender: TService);
    procedure temporizadorTimer(Sender: TObject);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  PasarFacturas: TPasarFacturas;

implementation

{$R *.DFM}

//Funcion Contar Archivos -SysUtils
function CuentaArchivos(const Ruta: string): Integer;
var
  SRec: TSearchRec;
  Attr, re: Integer;
begin
  Result:= 0;
  Attr:= faSysFile + faAnyFile + faArchive + faHidden; // No me funciona faReadOnly
  re:= FindFirst(Ruta , Attr, SRec);
  while re = 0 do
  begin
    Inc(Result);
    re:= FindNext(Srec);
  end;
end;

//Funcion Copiar -ShellApi -Forms -> Wnd
function CopiaTodo(Origen,Destino : String) :
  LongInt;
  var
    F : TShFileOpStruct;
    sOrigen, sDestino : String;
  begin
    Result := 0;
    sOrigen := Origen + #0;
    sDestino := Destino + #0;

    with F do
    begin
      Wnd   := Application.Handle;
      wFunc := FO_COPY;
      pFrom := @sOrigen[1];
      pTo   := @sDestino[1];
      fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT
    end;
    Result := ShFileOperation(F);
end;

//Procedimientos default service

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  PasarFacturas.Controller(CtrlCode);
end;

function TPasarFacturas.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TPasarFacturas.ServiceExecute(Sender: TService);
begin
  //Se ejecuta el codigo de manera indefinida
  temporizador.Enabled := True;
  while not Terminated do
    ServiceThread.ProcessRequests(True);
    temporizador.Enabled := False;
end;

//Proceso que se ejecutará
procedure TPasarFacturas.temporizadorTimer(Sender: TObject);
  var
  fichero : TStringList;
  filesOrigen,filesDestino: Integer;
const
  rutaFichero = 'C:\statusFacturas.txt';
  rutaOrigen =  '\\Facturas\2015\*.*';
  rutaDestino = 'C:\Documents and Settings\Propietario\Escritorio\Copia';
begin
  filesOrigen:=cuentaArchivos(rutaOrigen);
  filesDestino:=cuentaArchivos(rutaDestino+'\*.*');
  {
  Aqui estará un codigo para validar que archivos se copiarán
  }
  try
  //Copiar Archivos
  CopiaTodo(rutaOrigen, rutaDestino);
  fichero := TStringList.Create;
  if FileExists(rutaFichero) then
    fichero.LoadFromFile(rutaFichero);
    fichero.Add(DateTimeToStr(Now) + ' Se han copiado los archivos. No. de Archivos en origen: '+IntToStr(filesOrigen)+ ' No. de Archivos en destino: '+IntToStr(filesDestino));
    fichero.SaveToFile(rutaFichero);
  Except
  fichero := TStringList.Create;
  if FileExists(rutaFichero) then
    fichero.LoadFromFile(rutaFichero);
    fichero.Add(DateTimeToStr(Now) + ' Ha ocurrido un error al copiar.');
    fichero.SaveToFile(rutaFichero);
  end;
end;

end.

¿Cómo puedo copiar archivos de carpetas compartidas?, En el caso de que tuviera que ingresar con una credencial a la carpeta ¿Cómo lo consigo?

Les agradecería mucho su ayuda.

Última edición por Casimiro Notevi fecha: 14-07-2015 a las 23:55:48.
Responder Con Cita