最简单就用 COPY
我上次刚做过一个
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Filectrl,
Dialogs, StdCtrls, Buttons, ComCtrls,inifiles,shellapi,comobj,Registry,
ExtCtrls,dateutils, Menus, TeRollForm, WinSkinData, tray;
type
TfmMain = class(Ttrayfm)
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
GroupBox1: TGroupBox;
DateTimePicker1: TDateTimePicker;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
bitBack: TBitBtn;
CheckBox1: TCheckBox;
Timer1: TTimer;
SkinData1: TSkinData;
procedure BitBtn3Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure bitBackClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
linifile:string;
function jcSqlserver:boolean;
function setAutoStart(AutoStartflag: boolean): boolean;
function sqlstart(dataname:string):integer; //数据库名称 判断服务器是否启动
function copyfileJDT(s1,s2:string):boolean;
function copyfilePT(sf,s1,s2:string):boolean;
function MakeBat(s1,s2:string):boolean;
procedure MySearchFileList(const PathName, Ext:string; cFileList:TStrings);
public
{ Public declarations }
end;
var
fmMain: TfmMain;
implementation
{$R *.dfm}
const //uses ComObj,registry,shellapi;
SQLDMosVC_Unknown = 0; //未知状态
SQLDMosVC_Running = 1; //已经启动
SQLDMosVC_Paused = 2; //SQL Server为暂停状态
SQLDMosVC_Stopped = 3; //SQL Server为停止状态
SQLDMosVC_Starting = 4; //SQLSERVER正在启动(关闭-->启动)
SQLDMosVC_Stopping = 5; //SQLSERVER正在关闭
SQLDMosVC_Continuing= 6; //SQLSERVER正在启动(暂停-->启动)
SQLDMosVC_Pausing = 7; //SQLSERVER正在暂停(启动-->暂停)
procedure TfmMain.BitBtn2Click(Sender: TObject);
var
s: string;
begin
if SelectDirectory('浏览文件夹','',s) then //ExtractfilePath(application.exename)
Edit1.text:= S; //S等于目录的路径
end;
procedure TfmMain.BitBtn3Click(Sender: TObject);
var
s: string;
begin
if SelectDirectory('浏览文件夹','',s) then //ExtractfilePath(application.exename)
Edit2.text:= S; //S等于目录的路径
end;
procedure TfmMain.FormShow(Sender: TObject);
begin
inherited;
self.setAutoStart(True);
self.linifile:=Extractfilepath(application.ExeName)+'config.ini';
with Tinifile.Create(self.linifile) do
begin
self.Edit1.Text:=readstring('YWJ','URL','SQL 数据库存放目录');
self.Edit2.Text:=readstring('MDWJ','URL','//计算机名或IP地址/共享目录');
self.datetimepicker1.Time:=StrToDateTimeDef(readstring('JSQ','Sj',''),now);
self.CheckBox1.Checked:=readinteger('Note','YN',1)=1
end;
end;
procedure TfmMain.bitBackClick(Sender: TObject);
var
s:string;
st:Tstrings;
i:integer;
begin
st:=Tstringlist.Create;
try
s:='目录不存在,还要继续复制吗?';
if (not DirectoryExists(self.Edit1.Text)) or ( not DirectoryExists(self.Edit2.Text)) then
if not self.CheckBox1.Checked then
if application.MessageBox(pchar(s),'提示',mb_YesNo) =idNo then Exit;
{ if jcSqlserver
then s:= '是否要复制文件吗?若要复制文件,将会自动关闭SQL Server 服务器'
else s:= '是否要复制文件吗?'; }
s:= '是否要复制文件吗?若要复制文件,将会首先自动关闭 Ms SQL Server 服务器';
if not self.CheckBox1.Checked then
if application.MessageBox(pchar(s),'提示',mb_YesNo) =idNo then
Exit;
winexec(pchar('net stop mssqlserver'),0);
try
//是否打开服务器
//if (sqlstart('master')<>SQLDMosVC_Stopped) then exit;
s:=caption;
MySearchFileList(self.Edit1.Text,'',st);
for i:=0 to st.count-1 do
begin
self.copyfileJDT(st.Strings
,self.edit2.text+'/'+formatdatetime('YYYYMMDDHHMMNN',now)+extractfilename(st.Strings));
self.Caption:='正在备份文件'+st.strings;
end;
caption:=s;
except
on E: exception do showmessage(E.Message);
end;
if not self.CheckBox1.Checked then
if application.MessageBox('是否重启Ms SQL Server Service','提示',mb_YesNo) =idNo then
Exit;
winexec(pchar('net start mssqlserver'),sw_hide);
finally
st.Free;
end;
end;
function TfmMain.jcSqlserver: boolean;
var
SQLServer:Variant;
ServerList:Variant;
nServers:integer;
begin
SQLServer := CreateOleObject('SQLDMO.Application');
ServerList:= SQLServer.ListAvailableSQLServers;
result:=True;
if serverlist.count<=0 then
begin //如果服务器不存在,则退出系统
result:=False;
end;
{
else begin
for i:=1 to serverlist.count do
listbox1.Items.Add(serverlist.item(i)) ;
showmessage(serverlist.item(1)+'服务器已经打开,可以正常使用本系统');
adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security info=False;UserID=sa;InitialCatalog=ideal_db;DataSource='+ServerList.Item(1);
end;
}
SQLServer:=NULL;
serverList:=NULL;
end;
function TfmMain.setAutoStart(AutoStartflag: boolean): boolean;
var reg:TRegistry;
begin
result:=true;
reg:=TRegistry.Create;
reg.RootKey:=HKEY_LOCAL_MACHINE;
TRY
reg.OpenKey('SOFTWARE/Microsoft/Windows/CurrentVersion/Run',true);
if AutoStartflag then //自动启动
begin
if uppercase(reg.ReadString('BakDatabase'))<>uppercase(application.exename) then reg.WriteString('BakDatabase','"'+application.ExeName+'"')
end
else
Reg.DeleteValue('BakDatabase');
except
result:=false;
end;
reg.CloseKey;
reg.Free;
end;
procedure TfmMain.Timer1Timer(Sender: TObject);
begin
//caption:='定时复制文件'+' '+formatdatetime('HH-MM-SS',now)+' '+formatdatetime('HH-MM-SS',self.DateTimePicker1.Time);
if formatdatetime('HH-MM-SS',now)=formatdatetime('HH-MM-SS',self.DateTimePicker1.Time) then
begin
self.bitBack.Click;
end;
end;
procedure TfmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
inherited;
with Tinifile.Create(self.linifile) do
begin
self.bitBack.SetFocus;
writestring('YWJ','URL',self.Edit1.Text);
writestring('MDWJ','URL',self.Edit2.Text);
writestring('JSQ','Sj',formatdatetime('hh:mm:ss',self.datetimepicker1.Time)) ;
if self.CheckBox1.Checked
then writeinteger('Note','YN',1)
else writeinteger('Note','YN',0);
end;
end;
function TfmMain.sqlstart(dataname:string):integer; //数据库名称 判断服务器是否启动
{
const //uses ComObj,registry,shellapi;
SQLDMosVC_Unknown = 0; //未知状态
SQLDMosVC_Running = 1; //已经启动
SQLDMosVC_Paused = 2; //SQL Server为暂停状态
SQLDMosVC_Stopped = 3; //SQL Server为停止状态
SQLDMosVC_Starting = 4; //SQLSERVER正在启动(关闭-->启动)
SQLDMosVC_Stopping = 5; //SQLSERVER正在关闭
SQLDMosVC_Continuing= 6; //SQLSERVER正在启动(暂停-->启动)
SQLDMosVC_Pausing = 7; //SQLSERVER正在暂停(启动-->暂停)
}
var
reg:tregistry;
SQLServer : Variant;
ServerName,aa,PASSWORD,sd,sername : String;
LoginUserName,pp,SERVER : String;
LoginPassWord : String;
ReturnValue : Integer;
computernamechar;
size:dword;
begin
//获取本地计算机名称
getmem(computername,255);size:=255;
if getcomputername(computername,size)=true then
begin aa:=computername;freemem(computername);end;
if ((aa<>sername)and(sername<>'(local)')and(sername<>'(LOCAL)')and(sername<>'.')and(sername<>'(Local)'))then
begin
//result:=dataname;
result:=0;
exit;
end;//不是本机就不执行下边的
ServerName:='(local)'; //数据服务器名称
LoginUserName:='sa'; //登录ID
LoginPassWord:=sd; //密码
ReturnValue:=0;
SQLServer:=CreateOleObject('SQLDMO.SQLServer');
if VarIsNull(SQLServer) then
begin raise Exception.Create('没有安装SQL Server!');dataname:='';exit;end;
SQLServer.name:='(local)';
SQLServer.LoginSecure:=True;
SQLServer.logintimeout:=30;
ReturnValue:=SQLServer.Status;
case ReturnValue of
0: begin ShowMessage('数据服务管理器未知状态,请检查电脑系统是否正常!');dataname:='';exit;end;
1: sd:=sd;//begin ShowMessage('数据服务管理器已经启动');end;//SQLServer.Stop;
2: begin SQLServer.Continue;ShowMessage('数据服务管理器暂停状态,请关闭软件稍后再启动!');dataname:='';exit;end;
3: begin SQLServer.Start(False,ServerName,LoginUserName,LoginPassWord);
SQLServer.Connect(ServerName,LoginUserName,LoginPassWord);
ShowMessage('数据服务管理器停止状态,请关闭软件稍后再启动!');dataname:='';exit;
end;
4: begin ShowMessage('数据服务管理器正在启动,请关闭软件稍后再打开!');dataname:='';exit;end;
5: begin ShowMessage('数据服务管理器正在关闭,请关闭软件稍后再启动!');dataname:='';exit;end;
6: begin ShowMessage('数据服务管理器正在暂停,请关闭软件稍后再启动!');dataname:='';exit;end;
else
begin ShowMessage('数据服务管理器未知状态,请检查电脑系统是否正常!');dataname:='';end;
end;
SQLServer:=NULL;
// result:=dataname;
result:=ReturnValue;
end;
procedure TfmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
inherited;
canClose:=false;
if application.MessageBox('要退出吗?','提示',mb_YesNo) =idyes then canclose:=true;
end;
function TfmMain.copyfileJDT(s1,s2: string): boolean;
var
T: TShFileOpStruct;
begin
with T do
begin
T.Wnd := Handle;
T.wFunc := FO_Copy;
T.pFrom := PChar(s1);
T.pTo := PChar(s2);
T.fFlags :=FO_COPY;//FOF_NOCONFIRMATION;
end;
ShfileOperation(T);
end;
function TfmMain.MakeBat(s1,s2: string): boolean;
var
f:textfile;
begin
result:=true;
try
//assignfile(F,Extractfilepath(application.ExeName)+'Copyfile.bat');
assignfile(F,s1);
rewrite(f);
write(F,' copy '+ s1 +'/*.* ' + s2+'/*.*');
closefile(f);
//winexec(pchar(sf),sw_hide);
//deletefile(pchar(sf);
except
result:=false;
end;
end;
function TfmMain.copyfilePT(sf,s1,s2: string): boolean;
begin
copyfile(pchar(s1),pchar(s2),true);
end;
procedure TfmMain.MySearchFileList(const PathName, Ext:string; cFileList:TStrings);
var
F : TSearchRec;
Found : Boolean;
Pd :boolean;
begin
ChDir(PathName);
Found := (FindFirst('*.*', faAnyFile, F) = 0); //当FindFirst成功时返回0
while Found do
begin
if (F.Name = '.') or (F.Name = '..') then
begin
Found := (FindNext(F) = 0);
Continue;
end;
if (F.Attr and faDirectory)>0 then
MySearchFileList(F.Name,Ext,cFileList); //开始递归调用
//F.Name是文件名,GetCurrentDir可以得到当前目录
if Ext <>''
then Pd:= (F.Attr<>16) and (UpperCase(ExtractFileExt(F.Name))=Ext)
else Pd:= (F.Attr<>16);
if Pd then
cFileList.Add(GetCurrentDir+'/'+F.Name);
Found := (FindNext(F) = 0);
end;
FindClose(F);
ChDir('../');
end;
end.