procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
LocalList, SharpList: TStrings; //就要是这两个释放掉
ldir, sdir, filename, filename1, cmd: string;
ListBAK: TStrings;
success: Boolean;
begin
FileListBox1.Create(nil);
FileListBox2.Create(nil);
FileListBox3.Create(nil);
try
ldir := Localdir; // + '\';
sdir := SharpDir; // + '\'; // E:\FIRST\
fileName := '';
//
FileListBox1.Mask := '*.rar';
FileListBox2.Mask := '*.rar';
FileListBox3.Mask := '*.bak'; FileListBox1.Directory := Ldir; //本地
FileListBox2.Directory := sdir; //服务器的共享rar
FileListBox3.Directory := ldir; LocalList := FileListBox1.Items; //本地 RAR
SharpList := FileListBox2.Items; //本机共享rar
ListBAK := filelistbox3.items; //本地 bak for i := 0 to LocalList.Count - 1 do //删除Rar
begin
filename := LocalList[i]; //本地RAR
fileName1 := Copy(LocalList[i], 0, Length(LocalList[i]) - 4) + '.bak'; // RAR->BAK
if ListBAK.IndexOf(fileName1) = -1 then
begin
success := DeleteFile(pchar(Ldir + fileName));
if success then
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Sucess!')
else
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Fail!');
end;
end;
for i := 0 to ListBAK.Count - 1 do //压缩 加复制
begin
filename := ListBAK[i];
fileName1 := Copy(ListBAK[i], 0, Length(ListBAK[i]) - 4) + '.rar'; // fileName:=list1[i];
if IsFileInUse(Ldir+filename) =True then
Exit;
if LocalList.IndexOf(fileName1) = -1 then
begin
cmd := 'a -o+ -m5 -ibck -ep ' + Ldir + FileName1 + ' ' + Ldir + filename;
WriteLogToFile(#13);
WriteLogToFile('开始压缩:' + FileName1);
ExecuteFileWait(Application.Handle, 'winrar.exe', cmd, '', esMaximized);
WriteLogToFile('压缩完成:' + FileName1);
success := copyfile(pchar(Ldir + FileName1), pchar(Sdir + FileName1), false);
if success then
begin
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Sucess!');
Timer1.Enabled := True;
end
else
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Fail!');
end;
end;
FileListBox1.Refresh;
FileListBox2.Refresh;
FileListBox3.Refresh;
finally end;
end;第四行的.
LocalList, SharpList这两个stringlist你都指向了这三个FileListBox的Items,所以只要这三个FileListBox释放了,这三个stringlist也就自动释放。在finally后面加上这三个FileListBox.Free;
你需要什么演示?
i: Integer;
LocalList, SharpList: TStrings; //就要是这两个释放掉
用TStrings 我可以直接这样
LocalList := FileListBox1.Items; //本地 RAR
SharpList := FileListBox2.Items; //本机共享rar
如果用TStringList怎么办
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
LocalList, SharpList: TStringList; //就要是这两个释放掉
ldir, sdir, filename, filename1, cmd: string;
ListBAK: TStringList;
success: Boolean;
begin
FileListBox1.Create(nil);
FileListBox2.Create(nil);
FileListBox3.Create(nil);
try
ldir := Localdir; // + '\';
sdir := SharpDir; // + '\'; // E:\FIRST\
fileName := '';
//
FileListBox1.Mask := '*.rar';
FileListBox2.Mask := '*.rar';
FileListBox3.Mask := '*.bak'; FileListBox1.Directory := Ldir; //本地
FileListBox2.Directory := sdir; //服务器的共享rar
FileListBox3.Directory := ldir; //LocalList := FileListBox1.Items; //本地 RAR
//SharpList := FileListBox2.Items; //本机共享rar
//ListBAK := filelistbox3.items; //本地 bak
localList.assign(FileListBox1.items);
SharpList.assign(FileListBox2.items);
LisxBak.assigned(FileListbox3.items); for i := 0 to LocalList.Count - 1 do //删除Rar
begin
filename := LocalList[i]; //本地RAR
fileName1 := Copy(LocalList[i], 0, Length(LocalList[i]) - 4) + '.bak'; // RAR->BAK
if ListBAK.IndexOf(fileName1) = -1 then
begin
success := DeleteFile(pchar(Ldir + fileName));
if success then
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Sucess!')
else
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Fail!');
end;
end;
for i := 0 to ListBAK.Count - 1 do //压缩 加复制
begin
filename := ListBAK[i];
fileName1 := Copy(ListBAK[i], 0, Length(ListBAK[i]) - 4) + '.rar'; // fileName:=list1[i];
if IsFileInUse(Ldir+filename) =True then
Exit;
if LocalList.IndexOf(fileName1) = -1 then
begin
cmd := 'a -o+ -m5 -ibck -ep ' + Ldir + FileName1 + ' ' + Ldir + filename;
WriteLogToFile(#13);
WriteLogToFile('开始压缩:' + FileName1);
ExecuteFileWait(Application.Handle, 'winrar.exe', cmd, '', esMaximized);
WriteLogToFile('压缩完成:' + FileName1);
success := copyfile(pchar(Ldir + FileName1), pchar(Sdir + FileName1), false);
if success then
begin
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Sucess!');
Timer1.Enabled := True;
end
else
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Fail!');
end;
end;
FileListBox1.Refresh;
FileListBox2.Refresh;
FileListBox3.Refresh;
finally
FreeAndNil(LocalList);
FreeAndNil(SharpList);
FreeAndNil(ListBAK);
end;
end;
var
i: Integer;
LocalList, SharpList: TStrings; //就要是这两个释放掉
ldir, sdir, filename, filename1, cmd: string;
ListBAK: TStrings;
success: Boolean;
begin
FileListBox1.Create(nil);
FileListBox2.Create(nil);
FileListBox3.Create(nil);
LocalList := TStringList.create;
SharpList:= TStringList.create;
try
ldir := Localdir; // + '\';
sdir := SharpDir; // + '\'; // E:\FIRST\
fileName := '';
//
FileListBox1.Mask := '*.rar';
FileListBox2.Mask := '*.rar';
FileListBox3.Mask := '*.bak'; FileListBox1.Directory := Ldir; //本地
FileListBox2.Directory := sdir; //服务器的共享rar
FileListBox3.Directory := ldir; LocalList := FileListBox1.Items; //本地 RAR //这里是指针赋值,当FileListBox1.Items不存在的时候LocalList就会报错,建议使用LocaList.Assign(FileListBox1.Items);
SharpList := FileListBox2.Items; //本机共享rar //同理SharpList.Assign(FileListBox1.Items);
ListBAK := filelistbox3.items; //本地 bak for i := 0 to LocalList.Count - 1 do //删除Rar
begin
filename := LocalList[i]; //本地RAR
fileName1 := Copy(LocalList[i], 0, Length(LocalList[i]) - 4) + '.bak'; // RAR->BAK
if ListBAK.IndexOf(fileName1) = -1 then
begin
success := DeleteFile(pchar(Ldir + fileName));
if success then
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Sucess!')
else
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Fail!');
end;
end;
for i := 0 to ListBAK.Count - 1 do //压缩 加复制
begin
filename := ListBAK[i];
fileName1 := Copy(ListBAK[i], 0, Length(ListBAK[i]) - 4) + '.rar'; // fileName:=list1[i];
if IsFileInUse(Ldir+filename) =True then
Exit;
if LocalList.IndexOf(fileName1) = -1 then
begin
cmd := 'a -o+ -m5 -ibck -ep ' + Ldir + FileName1 + ' ' + Ldir + filename;
WriteLogToFile(#13);
WriteLogToFile('开始压缩:' + FileName1);
ExecuteFileWait(Application.Handle, 'winrar.exe', cmd, '', esMaximized);
WriteLogToFile('压缩完成:' + FileName1);
success := copyfile(pchar(Ldir + FileName1), pchar(Sdir + FileName1), false);
if success then
begin
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Sucess!');
Timer1.Enabled := True;
end
else
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Fail!');
end;
end;
FileListBox1.Refresh;
FileListBox2.Refresh;
FileListBox3.Refresh;
finally
LocalList.free;
SharpList.free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
LocalList, SharpList: TStringList; //就要是这两个释放掉
ldir, sdir, filename, filename1, cmd: string;
ListBAK: TStringList;
success: Boolean;
begin
FileListBox1.Create(nil);
FileListBox2.Create(nil);
FileListBox3.Create(nil); localList := TStringList.create;
SharpList := TStringList.create;
LisxBak := TStringList.create;
try
ldir := Localdir; // + '\';
sdir := SharpDir; // + '\'; // E:\FIRST\
fileName := '';
//
FileListBox1.Mask := '*.rar';
FileListBox2.Mask := '*.rar';
FileListBox3.Mask := '*.bak'; FileListBox1.Directory := Ldir; //本地
FileListBox2.Directory := sdir; //服务器的共享rar
FileListBox3.Directory := ldir; //LocalList := FileListBox1.Items; //本地 RAR
//SharpList := FileListBox2.Items; //本机共享rar
//ListBAK := filelistbox3.items; //本地 bak
localList.assign(FileListBox1.items);
SharpList.assign(FileListBox2.items);
LisxBak.assign(FileListbox3.items); for i := 0 to LocalList.Count - 1 do //删除Rar
begin
filename := LocalList[i]; //本地RAR
fileName1 := Copy(LocalList[i], 0, Length(LocalList[i]) - 4) + '.bak'; // RAR->BAK
if ListBAK.IndexOf(fileName1) = -1 then
begin
success := DeleteFile(pchar(Ldir + fileName));
if success then
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Sucess!')
else
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Fail!');
end;
end;
for i := 0 to ListBAK.Count - 1 do //压缩 加复制
begin
filename := ListBAK[i];
fileName1 := Copy(ListBAK[i], 0, Length(ListBAK[i]) - 4) + '.rar'; // fileName:=list1[i];
if IsFileInUse(Ldir+filename) =True then
Exit;
if LocalList.IndexOf(fileName1) = -1 then
begin
cmd := 'a -o+ -m5 -ibck -ep ' + Ldir + FileName1 + ' ' + Ldir + filename;
WriteLogToFile(#13);
WriteLogToFile('开始压缩:' + FileName1);
ExecuteFileWait(Application.Handle, 'winrar.exe', cmd, '', esMaximized);
WriteLogToFile('压缩完成:' + FileName1);
success := copyfile(pchar(Ldir + FileName1), pchar(Sdir + FileName1), false);
if success then
begin
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Sucess!');
Timer1.Enabled := True;
end
else
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Fail!');
end;
end;
FileListBox1.Refresh;
FileListBox2.Refresh;
FileListBox3.Refresh;
finally
FreeAndNil(LocalList);
FreeAndNil(SharpList);
FreeAndNil(ListBAK);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
LocalList, SharpList: TStringList; //就要是这两个释放掉
ldir, sdir, filename, filename1, cmd: string;
ListBAK: TStrings;
success: Boolean;
begin
FileListBox1.Create(nil);
FileListBox2.Create(nil);
FileListBox3.Create(nil);
LocalList.Create;
SharpList.Create;
try
ldir := Localdir; // + '\';
sdir := SharpDir; // + '\'; // E:\FIRST\
fileName := '';
//
FileListBox1.Mask := '*.rar';
FileListBox2.Mask := '*.rar';
FileListBox3.Mask := '*.bak'; FileListBox1.Directory := Ldir; //本地
FileListBox2.Directory := sdir; //服务器的共享rar
FileListBox3.Directory := ldir; LocalList := FileListBox1.Items; //本地 RAR
SharpList := FileListBox2.Items; //本机共享rar
ListBAK := filelistbox3.items; //本地 bak for i := 0 to LocalList.Count - 1 do //删除Rar
begin
filename := LocalList[i]; //本地RAR
fileName1 := Copy(LocalList[i], 0, Length(LocalList[i]) - 4) + '.bak'; // RAR->BAK
if ListBAK.IndexOf(fileName1) = -1 then
begin
success := DeleteFile(pchar(Ldir + fileName));
if success then
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Sucess!')
else
WriteLogToFile(Format('%-20s', [fileName]) + ' Delete Fail!');
end;
end;
for i := 0 to ListBAK.Count - 1 do //压缩 加复制
begin
filename := ListBAK[i];
fileName1 := Copy(ListBAK[i], 0, Length(ListBAK[i]) - 4) + '.rar'; // fileName:=list1[i];
if IsFileInUse(Ldir+filename) =True then
Exit;
if LocalList.IndexOf(fileName1) = -1 then
begin
cmd := 'a -o+ -m5 -ibck -ep ' + Ldir + FileName1 + ' ' + Ldir + filename;
WriteLogToFile(#13);
WriteLogToFile('开始压缩:' + FileName1);
ExecuteFileWait(Application.Handle, 'winrar.exe', cmd, '', esMaximized);
WriteLogToFile('压缩完成:' + FileName1);
success := copyfile(pchar(Ldir + FileName1), pchar(Sdir + FileName1), false);
if success then
begin
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Sucess!');
Timer1.Enabled := True;
end
else
WriteLogToFile(Format('%-40s', [FileName1]) + ' Copy Fail!');
end;
end;
FileListBox1.Refresh;
FileListBox2.Refresh;
FileListBox3.Refresh;
finally
LocalList.Free;
SharpList.Free; //TSringList释放 end;
end;
另,增加一点点内存是正常的,反复测试下,如果不存在溢出,ok
procedure TForm1.Button1Click(Sender: TObject);
var
d:TStrings;
begin
//t.Create();
d:=TStringList.Create;
FileListBox1.Create(nil);
FileListBox1.Mask := '*.*';
FileListBox1.Directory :='E:\'; //本地
d.Assign(FileListBox1.Items);
FreeAndNil(d);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
begin
for i:=0 to 10000 do
Button1Click(Self);
end;