📄 filetransferthreadunit.pas
字号:
unit FileTransferThreadUnit;
interface
uses
Classes, Windows, SysUtils;
type
TFileTransferThread = class(TThread)
private
{ Private declarations }
sDirName:String;
aDirName:String;
timeAfter:TDateTime;
fileNumber:integer;
haveSent:integer;
DirNumber:integer;
procedure DoCopyDirDone(sfn,afn:String; tat:TDateTime; var fileSum, haveTranSum, dirSum:integer);
procedure CopyDir;
protected
procedure Execute; override;
public
constructor Create(_sDirName,_aDirName:String; _timeAfter:TDateTime);
end;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TFileTransferThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TFileTransferThread }
uses
FileTestUnit, TestFunctionUnit;
constructor TFileTransferThread.Create(_sDirName,_aDirName:String; _timeAfter:TDateTime);
begin
sDirName:=_sDirName;
aDirName:=_aDirName;
timeAfter:=_timeAfter;
fileNumber:=0;
haveSent:=0;
DirNumber:=0;
inherited Create(True);
end;
procedure TFileTransferThread.DoCopyDirDone(sfn,afn:String; tat:TDateTime; var fileSum, haveTranSum, dirSum:integer);
var
hFindfile:Cardinal;
tfile:String;
t:String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
begin
//先保存当前目录
sCurDir:=GetCurrentDir;
ChDir(sfn);
hFindfile:=FindFirstFile('*.*',FindFileData);
if hFindFile<>INVALID_HANDLE_VALUE then
begin
if not DirectoryExists(afn) then
ForceDirectories(afn);
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
Continue;
//if (FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY) or (FindFileData.dwFileAttributes=48) then
if FindFileData.dwFileAttributes and faDirectory = faDirectory then
begin
dirSum:=dirSum+1;
FileTestForm.ListBox1.Items.Add(tfile+'-->'+IntToStr(FindFileData.dwFileAttributes));
t:=afn+'\'+tfile;
if not DirectoryExists(t) then
ForceDirectories(t);
if sfn[Length(sfn)]<>'\' then
DoCopyDirDone(sfn+'\'+tfile,t,tat,fileSum,haveTranSum,dirSum)
else
DoCopyDirDone(sfn+tfile,afn+tfile,tat,fileSum,haveTranSum,dirSum);
end
else
begin
t:=afn+'\'+tFile;
if GetFileLastModifyTime(sfn+'\'+tfile)>tat then
begin
try
CopyFile(PChar(sfn+'\'+tfile),PChar(t),False);
except
SendFile(sfn+'\'+tfile,t);
end;
haveTranSum:=haveTranSum+1;
fileSum:=fileSum+1;
FileTestForm.ListBox1.Items.Add(intToStr(FindFileData.dwFileAttributes)+','+'###'+DateTimeToStr(now)+tfile);
end
else
begin
fileSum:=fileSum+1;
FileTestForm.ListBox1.Items.Add(intToStr(FindFileData.dwFileAttributes)+','+'***'+DateTimeToStr(now)+tfile);
end;
end;
until FindNextFile(hFindFile,FindFileData)=false;
Windows.FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
exit;
end;
//回到原来的目录下
ChDir(sCurDir);
end;
procedure TFileTransferThread.CopyDir;
begin
DoCopyDirDone(sDirName,aDirName,timeAfter,fileNumber,haveSent,DirNumber);
FileTestForm.ListBox1.Items.Add('==========共有文件夹:'+IntToStr(DirNumber)+'个');
FileTestForm.ListBox1.Items.Add('==========共有文件:'+IntToStr(fileNumber)+'个');
FileTestForm.ListBox1.Items.Add('==========已传输文件:'+IntToStr(haveSent)+'个');
end;
procedure TFileTransferThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate:=True;
synchronize(CopyDir);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -