全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2326
推到 Plurk!
推到 Facebook!

FileExists 次數精簡

答題得分者是:GrandRURU
mjihkimo
一般會員


發表:2
回覆:5
積分:1
註冊:2007-12-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2014-10-17 11:13:25 IP:123.51.xxx.xxx 訂閱
各位大大們好 

想請問一下,小弟我有很多檔案要偵測是否存在
scan1:='C:1.exe';
scan2:='C:2.exe';
下面就會變的很多
if FileExists(c:\1.exe) then
Showmessage('錯誤碼01');
end;
if FileExists(c:\2.exe) then
Showmessage('錯誤碼02');
end;

有沒有辦法把它精簡呢?!
老大仔
尊榮會員


發表:78
回覆:837
積分:1088
註冊:2006-07-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2014-10-17 12:22:02 IP:210.61.xxx.xxx 未訂閱
用個迴圈+變數去判斷不就可以了...

===================引 用 mjihkimo 文 章===================
各位大大們好

想請問一下,小弟我有很多檔案要偵測是否存在
scan1:='C:1.exe';
scan2:='C:2.exe';
下面就會變的很多
if FileExists(c:\1.exe) then
Showmessage('錯誤碼01');
end;
if FileExists(c:\2.exe) then
Showmessage('錯誤碼02');
end;

有沒有辦法把它精簡呢?!
GrandRURU
站務副站長


發表:240
回覆:1680
積分:1874
註冊:2005-06-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2014-10-17 13:48:20 IP:59.120.xxx.xxx 未訂閱
同感

如果使用 TStringList,就能做更多事了

以這個例子來看
我們可以:
StringList.Add('1.exe');
StringList.Add('2.exe');

for i:=0 to StringList.Count-1 do
begin
if FileExists(varDir StringList[i]) then
ShowMessage(ErrorMsg IntToStr(I));
end;

===================引 用 老大仔 文 章===================
用個迴圈 變數去判斷不就可以了...

===================引 用 mjihkimo 文 章===================
各位大大們好

想請問一下,小弟我有很多檔案要偵測是否存在
scan1:='C:1.exe';
scan2:='C:2.exe';
下面就會變的很多
if FileExists(c:\1.exe) then
‚ ‚ ‚ Showmessage('錯誤碼01');
end;
‚ if FileExists(c:\2.exe) then
‚ ‚ ‚ Showmessage('錯誤碼02');
end;

有沒有辦法把它精簡呢?!
mjihkimo
一般會員


發表:2
回覆:5
積分:1
註冊:2007-12-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2014-10-17 15:02:49 IP:123.51.xxx.xxx 訂閱
感謝副站長您的回覆 小弟我目前正在試試您貼的方法(卡關中)
等用成功我在把我用好的分享上來 ^^"


===================引 用 GrandRURU 文 章===================
同感

如果使用 TStringList,就能做更多事了

以這個例子來看
我們可以:
StringList.Add('1.exe');
StringList.Add('2.exe');

for i:=0 to StringList.Count-1 do
begin
if FileExists(varDir StringList[i]) then
ShowMessage(ErrorMsg IntToStr(I));
end;

===================引 用 老大仔 文 章===================
用個迴圈 變數去判斷不就可以了...

===================引 用 mjihkimo 文 章===================
各位大大們好

想請問一下,小弟我有很多檔案要偵測是否存在
scan1:='C:1.exe';
scan2:='C:2.exe';
下面就會變的很多
if FileExists(c:\1.exe) then
ƒ‚‚ ƒ‚‚ ƒ‚‚ Showmessage('錯誤碼01');
end;
ƒ‚‚ if FileExists(c:\2.exe) then
ƒ‚‚ ƒ‚‚ ƒ‚‚ Showmessage('錯誤碼02');
end;

有沒有辦法把它精簡呢?!
mjihkimo
一般會員


發表:2
回覆:5
積分:1
註冊:2007-12-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2014-10-17 17:49:03 IP:123.51.xxx.xxx 訂閱
解答:

procedure TForm1.Button1Click(Sender: TObject);
var
Str: TStringList;
i: Integer;
begin
Str:=TStringList.Create;
Str.Add('C:\1.exe');
Str.Add('C:\2.exe');
Str.Add('C:\3.exe');
Str.Add('C:\4.exe');
Str.Add('C:\5.exe');
Str.Add('C:\6.exe');
Str.Sort;
for i := 1 to Str.Count - 1 do
if FileExists( Str[i]) then
Showmessage('錯誤碼0x000001');
Str.Free;
end;

系統時間:2024-04-26 7:59:22
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!