COMO FAÇO PARA ENVIAR ARQUIVO PARA FTP
Olá pessoal,
Depois de levar uma corça boa resolvi postar aqui meu problema para assim com ajuda de todos ai tentar solucionar esta questão. Já agradeço de ante mãos a todos que se disporem a me auxiliar.
É o seguinte to criando um assistente de backup, para fazer cópia do banco de dados para uma pasta copia no próprio computador ou em outro. Até, ai tudo ok. Mas como todo mundo que se digna a subir o everest quero chegar ao topo. Dai então pensei em copiar o arquivo para um ftp. Conecto tudo bonitinho e tal mais não consigo copiar o arquivo no ftp. Aparece uma exception na execução, dezendo que não não foi informado o diretorio ou nome do arquivo. que faço??? Alguém ai pode me dar uma luz. Segue o código do botão e logo abaixo o da unit completa:
PS.: Por questões de segurança os dados de acesso ao são verdadeiros....
1 - botão usada para enviar:
procedure TfrmPrincipal.btnFtpClick(Sender: TObject);
var origem,destino:String;
begin
cxPageControl1.SendToBack;
pnAguarde.BringToFront;
origem:=edtNomeBackup.Text;
destino:=edtFtp.Text+origem;
LogarFtp;
IdFTP1.Put(origem,destino,False);
lblStatus.Visible:=True;
lblStatus.Caption:=''Cópia enviada ao FTP com sucesso'';
IdFTP1.Quit;
pnAguarde.SendToBack;
BtnSair.SetFocus;
end;
UNIT COMPLETA
unit UPrincipal;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, StdCtrls,
Buttons, CheckLst, ExtCtrls, cxPC, ACBrBase, ACBrDownload,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, Mask,
ToolEdit,idGlobal, Gauges, GIFImage, RXCtrls, IniFiles, Menus;
type
TfrmPrincipal = class(TForm)
pnTopo: TPanel;
lblNome: TLabel;
Panel1: TPanel;
cxPageControl1: TcxPageControl;
cxTabSheet1: TcxTabSheet;
Label2: TLabel;
Bevel1: TBevel;
Label3: TLabel;
Bevel2: TBevel;
CheckListBox1: TCheckListBox;
cxTabSheet2: TcxTabSheet;
BtnOK: TBitBtn;
BtnAjuda: TBitBtn;
BtnSair: TBitBtn;
BtnHistorico: TBitBtn;
BtnAgendar: TBitBtn;
lblDescricao: TLabel;
cbFtp: TCheckBox;
Label1: TLabel;
IdFTP1: TIdFTP;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
EdtDestino: TEdit;
SbBrowser: TSpeedButton;
pnAguarde: TPanel;
Image1: TImage;
edtNomeBackup: TEdit;
btnBackup: TButton;
lblStatus: TRxLabel;
edtFtp: TEdit;
btnFtp: TButton;
pnConfig: TPanel;
Label4: TLabel;
rbFetag: TRadioButton;
rbSindicato: TRadioButton;
Label5: TLabel;
cbSegunda: TCheckBox;
cbTerca: TCheckBox;
cbQuarta: TCheckBox;
cbQuinta: TCheckBox;
cbSexta: TCheckBox;
cbSabado: TCheckBox;
cbDomingo: TCheckBox;
edtHora: TMaskEdit;
Label6: TLabel;
Label7: TLabel;
edtPastaPadrao: TEdit;
Label8: TLabel;
edtDestinoPadrao: TEdit;
btnSalvar: TBitBtn;
btnCancelar: TBitBtn;
chkFtp: TCheckBox;
PopupMenu1: TPopupMenu;
Marcatodos1: TMenuItem;
procedure BtnSairClick(Sender: TObject);
procedure EdtDestinoClick(Sender: TObject);
procedure BtnOKClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SbBrowserClick(Sender: TObject);
procedure btnBackupClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnFtpClick(Sender: TObject);
procedure BtnAgendarClick(Sender: TObject);
procedure btnSalvarClick(Sender: TObject);
procedure btnCancelarClick(Sender: TObject);
procedure Marcatodos1Click(Sender: TObject);
private
{ Private declarations }
procedure LogarFtp;
Function CriticaDados: Boolean;
function Confirma(mensagem, titulo: string): integer;
procedure Aviso(mensagem: string);
procedure Alerta(mensagem: string);
Procedure GravarIni(IniNome, IniSessao, IniSubSessao, IniValor: String);
public
{ Public declarations }
My_Verifiq:Integer;
sTipo,sEnviaFtp,sSegunda,sTerca,sQuarta,sQuinta,sSexta,sSabado,sDomingo,
sHora,sPastaPadrao,sDestinoPadrao:String;
Function LerIni(IniNome, IniSessao, IniSubSessao: String): String;
procedure LeuIni;
end;
var
frmPrincipal: TfrmPrincipal;
implementation
uses Math;
const
sHost =''ftp.eloye.com.br'';
sUser =''joaname'';
sPass =''joaname2345'';
sPort =''88'';
sDB_sags =''C:\Sistemas\Sindicat\Database\Dados.gdb'';
sDB_sigs =''C:\Sistemas\Fetag\Database\Dados.gdb'';
sDiretBackup_sags =''ftp://ftp.elo.com.br/Backups%20Sags/STTRs%20MA/'';
sDiretBackup_sigs ='''';
{$R *.dfm}
{ TfrmPrincipal }
procedure TfrmPrincipal.LogarFtp;
begin
IdFTP1.Disconnect;
IdFTP1.Host:= sHost;
IdFTP1.Port:=21;//StrToInt(sPort);
IdFTP1.Username:= sUser;
IdFTP1.Password:= sPass;
IdFTP1.Passive:= False; {usa modo ativo}
IdFTP1.RecvBufferSize:= 8192;
try
{espera até 10 segundos pela conexão}
IdFTP1.Connect(True, 60000);
except
on E: Exception do
ShowMessage(E.Message);
end;
end;
procedure TfrmPrincipal.BtnSairClick(Sender: TObject);
begin
Close;
end;
procedure TfrmPrincipal.EdtDestinoClick(Sender: TObject);
begin
EdtDestino.Clear;
end;
procedure TfrmPrincipal.BtnOKClick(Sender: TObject);
begin
if not (cbFtp.Checked)then
begin
if not CriticaDados then exit;
cxPageControl1.SendToBack;
pnAguarde.BringToFront;
btnBackup.Click;
end
else
begin
btnFtp.Click;
end;
end;
procedure TfrmPrincipal.FormShow(Sender: TObject);
begin
lblStatus.Caption:='''';
lblStatus.Visible:=False;
if FileExists(ExtractFilePath(Application.ExeName)+''Abr.ini'')then
begin
LeuIni;
if(sTipo=''1'')then
My_Verifiq:=0
else
if(sTipo=''2'')then
My_Verifiq:=1;
if(My_Verifiq=0)then begin
lblNome.Caption:=''SIGS'';
lblDescricao.Caption:=''Sistema Integrado de Gestão Sindical'';
edtNomeBackup.Text:=Trim(sDB_sigs);
edtFtp.Text:='''';
CheckListBox1.Items.Add(sDB_sigs);
Marcatodos1Click(Sender);
end else
if(My_Verifiq=1)then begin
lblNome.Caption:=''SAGS'';
lblDescricao.Caption:=''Sistema de Automação e Gestão Sindical'';
edtNomeBackup.Text:=Trim(sDB_sags);
edtFtp.Text:=Trim(sDiretBackup_sags);
CheckListBox1.Items.Add(sDB_sags);
Marcatodos1Click(Sender);
end;
if(sEnviaFtp=''TRUE'')then
cbFtp.Checked:=TRUE
else
cbFtp.Checked:=FALSE;
end else
begin
Alerta(''O Assistente de Backup e Restauração ''+#13+
''ainda não foi configurado. Preencha os dados'');
BtnAgendar.Click;
end;
end;
procedure TfrmPrincipal.SbBrowserClick(Sender: TObject);
begin
If SaveDialog1.Execute then
Begin
EdtDestino.Text := SaveDialog1.FileName;
End
end;
function TfrmPrincipal.CriticaDados: Boolean;
begin
Result:= False;
if not(cbFtp.Checked)then
begin
if(EdtDestino.Text='''')then
begin
Alerta(''O destino do arquivo não foi informado'');
EdtDestino.SetFocus;
exit;
end;
end;
Result:=True;
end;
procedure TfrmPrincipal.Alerta(mensagem: string);
begin
Application.MessageBox(PAnsiChar(mensagem), ''Atenção'', MB_OK + MB_ICONWARNING + MB_DEFBUTTON1);
end;
procedure TfrmPrincipal.Aviso(mensagem: string);
begin
Application.MessageBox(PAnsiChar(mensagem), ''Informação'', MB_OK + MB_ICONINFORMATION + MB_DEFBUTTON1);
end;
function TfrmPrincipal.Confirma(mensagem, titulo: string): integer;
begin
if Application.MessageBox(PAnsiChar(mensagem), PAnsiChar(titulo), MB_YESNO + MB_ICONQUESTION) = IDYES then
result := 1
else
result := 0;
end;
procedure TfrmPrincipal.btnBackupClick(Sender: TObject);
var
origem, destino: string;
begin
If not CriticaDados then exit;
Begin
origem := edtNomeBackup.Text;
destino := EdtDestino.Text;
if FileExists(destino)then DeleteFile(destino);
if CopyFile(Pchar(origem),Pchar(destino),True) then
Begin
//Aviso(''Backupe Efetuado Com Sucesso'');
cxPageControl1.BringToFront;
pnAguarde.SendToBack;
lblStatus.Visible:=True;
Sleep(10000);
lblStatus.Caption:=''Cópia efetuada com sucesso!!'';
pnAguarde.SendToBack;
BtnSair.SetFocus;
End
Else
Alerta(''Não Foi Possível Realizar o Backup'')
End;
end;
procedure TfrmPrincipal.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
lblStatus.Caption:='''';
lblStatus.Visible:=False;
end;
procedure TfrmPrincipal.btnFtpClick(Sender: TObject);
var origem,destino:String;
begin
cxPageControl1.SendToBack;
pnAguarde.BringToFront;
origem:=edtNomeBackup.Text;
destino:=edtFtp.Text+origem;
LogarFtp;
IdFTP1.Put(origem,destino,False);
lblStatus.Visible:=True;
lblStatus.Caption:=''Cópia enviada ao FTP com sucesso'';
IdFTP1.Quit;
pnAguarde.SendToBack;
BtnSair.SetFocus;
end;
procedure TfrmPrincipal.GravarIni(IniNome, IniSessao, IniSubSessao,
IniValor: String);
var
ArqIni: TIniFile;
Begin
ArqIni := TIniFile.Create(IniNome);
try
ArqIni.WriteString(IniSessao, IniSubSessao, IniValor);
finally
ArqIni.Free;
end;
end;
function TfrmPrincipal.LerIni(IniNome, IniSessao,
IniSubSessao: String): String;
var
ArqIni: TIniFile;
Begin
ArqIni := TIniFile.Create(IniNome);
try
Result := ArqIni.ReadString(IniSessao, IniSubSessao, '''');
finally
ArqIni.Free;
end;
end;
procedure TfrmPrincipal.BtnAgendarClick(Sender: TObject);
begin
if FileExists(ExtractFilePath(Application.ExeName)+''Abr.ini'')then
begin
LeuIni;
if(sTipo=''1'')then
rbFetag.Checked:=True else
if(stipo=''2'')then
rbSindicato.Checked:=True;
if(sEnviaFtp=''TRUE'')then
chkFtp.Checked:=True else chkFtp.Checked:=False;
if(sSegunda=''TRUE'')then
cbSegunda.Checked:=True else cbSegunda.Checked:=False;
if(sTerca=''TRUE'')then
cbTerca.Checked:=True else cbTerca.Checked:=False;
if(sQuarta=''TRUE'')then
cbQuarta.Checked:=True else cbQuarta.Checked:=False;
if(sQuinta=''TRUE'')then
cbQuinta.Checked:=True else cbQuinta.Checked:=False;
if(sSexta=''TRUE'')then
cbSexta.Checked:=True else cbSexta.Checked:=False;
if(sSabado=''TRUE'')then
cbSabado.Checked:=True else cbSabado.Checked:=False;
if(sDomingo=''TRUE'')then
cbDomingo.Checked:=True else cbDomingo.Checked:=False;
if(sHora<>'''')then
edtHora.Text:=Trim(sHora);
if(sPastaPadrao<>'''')then
edtPastaPadrao.Text:=Trim(sPastaPadrao);
if(sDestinoPadrao<>'''')then
edtDestinoPadrao.Text:=Trim(sDestinoPadrao);
end;
DeleteFile(ExtractFilePath(Application.ExeName)+''Abr.ini'');
pnConfig.Top:=53;
pnConfig.Left:=8;
pnConfig.BringToFront;
pnConfig.SetFocus;
end;
procedure TfrmPrincipal.btnSalvarClick(Sender: TObject);
begin
try
if(rbFetag.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Tipo'',''1'')
else
if(rbSindicato.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Tipo'',''2'');
if(chkFtp.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Copiar no FTP'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Copiar no FTP'',''FALSE'');
if(cbSegunda.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Segunda-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Segunda-feira'',''FALSE'');
if(cbTerca.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Terça-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Terça-feira'',''FALSE'');
if(cbQuarta.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quarta-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quarta-feira'',''FALSE'');
if(cbQuinta.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quinta-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quinta-feira'',''FALSE'');
if(cbSexta.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sexta-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sexta-feira'',''FALSE'');
if(cbSabado.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sabado'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sabado'',''FALSE'');
if(cbDomingo.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Domingo'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Domingo'',''FALSE'');
if(edtHora.Text<>'''')or(edthora.Text<>'' : '')then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Hora'',Trim(edtHora.Text))
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Hora'',''08:00'');
if(edtPastaPadrao.Text<>'''')then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta Padrão'',Trim(edtPastaPadrao.Text))
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta Padrão'','''');
if(edtDestinoPadrao.Text<>'''')then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'',Trim(edtDestinoPadrao.Text))
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'','''');
Aviso(''O Assistente de Backup e Restauração será reiniciado''+#13+
''para que as alterações entrem em vigor'');
Application.Terminate;
WinExec(''C:\Sistemas\Abr\Abr.exe'',SW_NORMAL);
except
Alerta(''Não foi possível configurar o ''+#13+
''Assistente de Backup e Restauração. ''+#13+
''Contacte suporte técnico'');
end;
end;
procedure TfrmPrincipal.btnCancelarClick(Sender: TObject);
begin
pnConfig.SendToBack;
cxPageControl1.SetFocus;
end;
procedure TfrmPrincipal.LeuIni;
begin
sTipo:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Tipo'');
sEnviaFtp:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Copiar no FTP'');
sSegunda:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Segunda-feira'');
sTerca:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Terça-feira'');
sQuarta:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quarta-feira'');
sQuinta:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quinta-feira'');
sSexta:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sexta-feira'');
sSabado:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sabado-feira'');
sDomingo:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Domingo-feira'');
sHora:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Hora'');
sPastaPadrao:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'');
sDestinoPadrao:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'');
end;
procedure TfrmPrincipal.Marcatodos1Click(Sender: TObject);
var i:Integer;
marcar:Boolean;
begin
marcar:=True;
for i:= 0 to CheckListBox1.Items.Count-1 do
begin
CheckListBox1.Checked[i]:=marcar;
Next;
end;
BtnOK.SetFocus;
end;
end.
Depois de levar uma corça boa resolvi postar aqui meu problema para assim com ajuda de todos ai tentar solucionar esta questão. Já agradeço de ante mãos a todos que se disporem a me auxiliar.
É o seguinte to criando um assistente de backup, para fazer cópia do banco de dados para uma pasta copia no próprio computador ou em outro. Até, ai tudo ok. Mas como todo mundo que se digna a subir o everest quero chegar ao topo. Dai então pensei em copiar o arquivo para um ftp. Conecto tudo bonitinho e tal mais não consigo copiar o arquivo no ftp. Aparece uma exception na execução, dezendo que não não foi informado o diretorio ou nome do arquivo. que faço??? Alguém ai pode me dar uma luz. Segue o código do botão e logo abaixo o da unit completa:
PS.: Por questões de segurança os dados de acesso ao são verdadeiros....
1 - botão usada para enviar:
procedure TfrmPrincipal.btnFtpClick(Sender: TObject);
var origem,destino:String;
begin
cxPageControl1.SendToBack;
pnAguarde.BringToFront;
origem:=edtNomeBackup.Text;
destino:=edtFtp.Text+origem;
LogarFtp;
IdFTP1.Put(origem,destino,False);
lblStatus.Visible:=True;
lblStatus.Caption:=''Cópia enviada ao FTP com sucesso'';
IdFTP1.Quit;
pnAguarde.SendToBack;
BtnSair.SetFocus;
end;
UNIT COMPLETA
unit UPrincipal;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, StdCtrls,
Buttons, CheckLst, ExtCtrls, cxPC, ACBrBase, ACBrDownload,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, Mask,
ToolEdit,idGlobal, Gauges, GIFImage, RXCtrls, IniFiles, Menus;
type
TfrmPrincipal = class(TForm)
pnTopo: TPanel;
lblNome: TLabel;
Panel1: TPanel;
cxPageControl1: TcxPageControl;
cxTabSheet1: TcxTabSheet;
Label2: TLabel;
Bevel1: TBevel;
Label3: TLabel;
Bevel2: TBevel;
CheckListBox1: TCheckListBox;
cxTabSheet2: TcxTabSheet;
BtnOK: TBitBtn;
BtnAjuda: TBitBtn;
BtnSair: TBitBtn;
BtnHistorico: TBitBtn;
BtnAgendar: TBitBtn;
lblDescricao: TLabel;
cbFtp: TCheckBox;
Label1: TLabel;
IdFTP1: TIdFTP;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
EdtDestino: TEdit;
SbBrowser: TSpeedButton;
pnAguarde: TPanel;
Image1: TImage;
edtNomeBackup: TEdit;
btnBackup: TButton;
lblStatus: TRxLabel;
edtFtp: TEdit;
btnFtp: TButton;
pnConfig: TPanel;
Label4: TLabel;
rbFetag: TRadioButton;
rbSindicato: TRadioButton;
Label5: TLabel;
cbSegunda: TCheckBox;
cbTerca: TCheckBox;
cbQuarta: TCheckBox;
cbQuinta: TCheckBox;
cbSexta: TCheckBox;
cbSabado: TCheckBox;
cbDomingo: TCheckBox;
edtHora: TMaskEdit;
Label6: TLabel;
Label7: TLabel;
edtPastaPadrao: TEdit;
Label8: TLabel;
edtDestinoPadrao: TEdit;
btnSalvar: TBitBtn;
btnCancelar: TBitBtn;
chkFtp: TCheckBox;
PopupMenu1: TPopupMenu;
Marcatodos1: TMenuItem;
procedure BtnSairClick(Sender: TObject);
procedure EdtDestinoClick(Sender: TObject);
procedure BtnOKClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SbBrowserClick(Sender: TObject);
procedure btnBackupClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnFtpClick(Sender: TObject);
procedure BtnAgendarClick(Sender: TObject);
procedure btnSalvarClick(Sender: TObject);
procedure btnCancelarClick(Sender: TObject);
procedure Marcatodos1Click(Sender: TObject);
private
{ Private declarations }
procedure LogarFtp;
Function CriticaDados: Boolean;
function Confirma(mensagem, titulo: string): integer;
procedure Aviso(mensagem: string);
procedure Alerta(mensagem: string);
Procedure GravarIni(IniNome, IniSessao, IniSubSessao, IniValor: String);
public
{ Public declarations }
My_Verifiq:Integer;
sTipo,sEnviaFtp,sSegunda,sTerca,sQuarta,sQuinta,sSexta,sSabado,sDomingo,
sHora,sPastaPadrao,sDestinoPadrao:String;
Function LerIni(IniNome, IniSessao, IniSubSessao: String): String;
procedure LeuIni;
end;
var
frmPrincipal: TfrmPrincipal;
implementation
uses Math;
const
sHost =''ftp.eloye.com.br'';
sUser =''joaname'';
sPass =''joaname2345'';
sPort =''88'';
sDB_sags =''C:\Sistemas\Sindicat\Database\Dados.gdb'';
sDB_sigs =''C:\Sistemas\Fetag\Database\Dados.gdb'';
sDiretBackup_sags =''ftp://ftp.elo.com.br/Backups%20Sags/STTRs%20MA/'';
sDiretBackup_sigs ='''';
{$R *.dfm}
{ TfrmPrincipal }
procedure TfrmPrincipal.LogarFtp;
begin
IdFTP1.Disconnect;
IdFTP1.Host:= sHost;
IdFTP1.Port:=21;//StrToInt(sPort);
IdFTP1.Username:= sUser;
IdFTP1.Password:= sPass;
IdFTP1.Passive:= False; {usa modo ativo}
IdFTP1.RecvBufferSize:= 8192;
try
{espera até 10 segundos pela conexão}
IdFTP1.Connect(True, 60000);
except
on E: Exception do
ShowMessage(E.Message);
end;
end;
procedure TfrmPrincipal.BtnSairClick(Sender: TObject);
begin
Close;
end;
procedure TfrmPrincipal.EdtDestinoClick(Sender: TObject);
begin
EdtDestino.Clear;
end;
procedure TfrmPrincipal.BtnOKClick(Sender: TObject);
begin
if not (cbFtp.Checked)then
begin
if not CriticaDados then exit;
cxPageControl1.SendToBack;
pnAguarde.BringToFront;
btnBackup.Click;
end
else
begin
btnFtp.Click;
end;
end;
procedure TfrmPrincipal.FormShow(Sender: TObject);
begin
lblStatus.Caption:='''';
lblStatus.Visible:=False;
if FileExists(ExtractFilePath(Application.ExeName)+''Abr.ini'')then
begin
LeuIni;
if(sTipo=''1'')then
My_Verifiq:=0
else
if(sTipo=''2'')then
My_Verifiq:=1;
if(My_Verifiq=0)then begin
lblNome.Caption:=''SIGS'';
lblDescricao.Caption:=''Sistema Integrado de Gestão Sindical'';
edtNomeBackup.Text:=Trim(sDB_sigs);
edtFtp.Text:='''';
CheckListBox1.Items.Add(sDB_sigs);
Marcatodos1Click(Sender);
end else
if(My_Verifiq=1)then begin
lblNome.Caption:=''SAGS'';
lblDescricao.Caption:=''Sistema de Automação e Gestão Sindical'';
edtNomeBackup.Text:=Trim(sDB_sags);
edtFtp.Text:=Trim(sDiretBackup_sags);
CheckListBox1.Items.Add(sDB_sags);
Marcatodos1Click(Sender);
end;
if(sEnviaFtp=''TRUE'')then
cbFtp.Checked:=TRUE
else
cbFtp.Checked:=FALSE;
end else
begin
Alerta(''O Assistente de Backup e Restauração ''+#13+
''ainda não foi configurado. Preencha os dados'');
BtnAgendar.Click;
end;
end;
procedure TfrmPrincipal.SbBrowserClick(Sender: TObject);
begin
If SaveDialog1.Execute then
Begin
EdtDestino.Text := SaveDialog1.FileName;
End
end;
function TfrmPrincipal.CriticaDados: Boolean;
begin
Result:= False;
if not(cbFtp.Checked)then
begin
if(EdtDestino.Text='''')then
begin
Alerta(''O destino do arquivo não foi informado'');
EdtDestino.SetFocus;
exit;
end;
end;
Result:=True;
end;
procedure TfrmPrincipal.Alerta(mensagem: string);
begin
Application.MessageBox(PAnsiChar(mensagem), ''Atenção'', MB_OK + MB_ICONWARNING + MB_DEFBUTTON1);
end;
procedure TfrmPrincipal.Aviso(mensagem: string);
begin
Application.MessageBox(PAnsiChar(mensagem), ''Informação'', MB_OK + MB_ICONINFORMATION + MB_DEFBUTTON1);
end;
function TfrmPrincipal.Confirma(mensagem, titulo: string): integer;
begin
if Application.MessageBox(PAnsiChar(mensagem), PAnsiChar(titulo), MB_YESNO + MB_ICONQUESTION) = IDYES then
result := 1
else
result := 0;
end;
procedure TfrmPrincipal.btnBackupClick(Sender: TObject);
var
origem, destino: string;
begin
If not CriticaDados then exit;
Begin
origem := edtNomeBackup.Text;
destino := EdtDestino.Text;
if FileExists(destino)then DeleteFile(destino);
if CopyFile(Pchar(origem),Pchar(destino),True) then
Begin
//Aviso(''Backupe Efetuado Com Sucesso'');
cxPageControl1.BringToFront;
pnAguarde.SendToBack;
lblStatus.Visible:=True;
Sleep(10000);
lblStatus.Caption:=''Cópia efetuada com sucesso!!'';
pnAguarde.SendToBack;
BtnSair.SetFocus;
End
Else
Alerta(''Não Foi Possível Realizar o Backup'')
End;
end;
procedure TfrmPrincipal.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
lblStatus.Caption:='''';
lblStatus.Visible:=False;
end;
procedure TfrmPrincipal.btnFtpClick(Sender: TObject);
var origem,destino:String;
begin
cxPageControl1.SendToBack;
pnAguarde.BringToFront;
origem:=edtNomeBackup.Text;
destino:=edtFtp.Text+origem;
LogarFtp;
IdFTP1.Put(origem,destino,False);
lblStatus.Visible:=True;
lblStatus.Caption:=''Cópia enviada ao FTP com sucesso'';
IdFTP1.Quit;
pnAguarde.SendToBack;
BtnSair.SetFocus;
end;
procedure TfrmPrincipal.GravarIni(IniNome, IniSessao, IniSubSessao,
IniValor: String);
var
ArqIni: TIniFile;
Begin
ArqIni := TIniFile.Create(IniNome);
try
ArqIni.WriteString(IniSessao, IniSubSessao, IniValor);
finally
ArqIni.Free;
end;
end;
function TfrmPrincipal.LerIni(IniNome, IniSessao,
IniSubSessao: String): String;
var
ArqIni: TIniFile;
Begin
ArqIni := TIniFile.Create(IniNome);
try
Result := ArqIni.ReadString(IniSessao, IniSubSessao, '''');
finally
ArqIni.Free;
end;
end;
procedure TfrmPrincipal.BtnAgendarClick(Sender: TObject);
begin
if FileExists(ExtractFilePath(Application.ExeName)+''Abr.ini'')then
begin
LeuIni;
if(sTipo=''1'')then
rbFetag.Checked:=True else
if(stipo=''2'')then
rbSindicato.Checked:=True;
if(sEnviaFtp=''TRUE'')then
chkFtp.Checked:=True else chkFtp.Checked:=False;
if(sSegunda=''TRUE'')then
cbSegunda.Checked:=True else cbSegunda.Checked:=False;
if(sTerca=''TRUE'')then
cbTerca.Checked:=True else cbTerca.Checked:=False;
if(sQuarta=''TRUE'')then
cbQuarta.Checked:=True else cbQuarta.Checked:=False;
if(sQuinta=''TRUE'')then
cbQuinta.Checked:=True else cbQuinta.Checked:=False;
if(sSexta=''TRUE'')then
cbSexta.Checked:=True else cbSexta.Checked:=False;
if(sSabado=''TRUE'')then
cbSabado.Checked:=True else cbSabado.Checked:=False;
if(sDomingo=''TRUE'')then
cbDomingo.Checked:=True else cbDomingo.Checked:=False;
if(sHora<>'''')then
edtHora.Text:=Trim(sHora);
if(sPastaPadrao<>'''')then
edtPastaPadrao.Text:=Trim(sPastaPadrao);
if(sDestinoPadrao<>'''')then
edtDestinoPadrao.Text:=Trim(sDestinoPadrao);
end;
DeleteFile(ExtractFilePath(Application.ExeName)+''Abr.ini'');
pnConfig.Top:=53;
pnConfig.Left:=8;
pnConfig.BringToFront;
pnConfig.SetFocus;
end;
procedure TfrmPrincipal.btnSalvarClick(Sender: TObject);
begin
try
if(rbFetag.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Tipo'',''1'')
else
if(rbSindicato.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Tipo'',''2'');
if(chkFtp.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Copiar no FTP'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Copiar no FTP'',''FALSE'');
if(cbSegunda.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Segunda-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Segunda-feira'',''FALSE'');
if(cbTerca.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Terça-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Terça-feira'',''FALSE'');
if(cbQuarta.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quarta-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quarta-feira'',''FALSE'');
if(cbQuinta.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quinta-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quinta-feira'',''FALSE'');
if(cbSexta.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sexta-feira'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sexta-feira'',''FALSE'');
if(cbSabado.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sabado'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sabado'',''FALSE'');
if(cbDomingo.Checked)then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Domingo'',''TRUE'')
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Domingo'',''FALSE'');
if(edtHora.Text<>'''')or(edthora.Text<>'' : '')then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Hora'',Trim(edtHora.Text))
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Hora'',''08:00'');
if(edtPastaPadrao.Text<>'''')then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta Padrão'',Trim(edtPastaPadrao.Text))
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta Padrão'','''');
if(edtDestinoPadrao.Text<>'''')then
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'',Trim(edtDestinoPadrao.Text))
else
GravarIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'','''');
Aviso(''O Assistente de Backup e Restauração será reiniciado''+#13+
''para que as alterações entrem em vigor'');
Application.Terminate;
WinExec(''C:\Sistemas\Abr\Abr.exe'',SW_NORMAL);
except
Alerta(''Não foi possível configurar o ''+#13+
''Assistente de Backup e Restauração. ''+#13+
''Contacte suporte técnico'');
end;
end;
procedure TfrmPrincipal.btnCancelarClick(Sender: TObject);
begin
pnConfig.SendToBack;
cxPageControl1.SetFocus;
end;
procedure TfrmPrincipal.LeuIni;
begin
sTipo:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Tipo'');
sEnviaFtp:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Banco de Dados'', ''Copiar no FTP'');
sSegunda:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Segunda-feira'');
sTerca:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Terça-feira'');
sQuarta:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quarta-feira'');
sQuinta:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Quinta-feira'');
sSexta:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sexta-feira'');
sSabado:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Sabado-feira'');
sDomingo:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Domingo-feira'');
sHora:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Hora'');
sPastaPadrao:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'');
sDestinoPadrao:=LerIni(ExtractFilePath(Application.ExeName) + ''Abr.ini'', ''Backup'', ''Pasta destino Padrão'');
end;
procedure TfrmPrincipal.Marcatodos1Click(Sender: TObject);
var i:Integer;
marcar:Boolean;
begin
marcar:=True;
for i:= 0 to CheckListBox1.Items.Count-1 do
begin
CheckListBox1.Checked[i]:=marcar;
Next;
end;
BtnOK.SetFocus;
end;
end.
Marcos Morais
Curtidas 0