| View previous topic :: View next topic |
| Author |
Message |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Apr 08, 2008 10:03 am Post subject: [Delphi]Searching through LocalDisk for a File ? |
|
|
my stupid code is:
| Code: | function ScanFolder(filename:string;dir:string):boolean;
var Data:TWin32FindDataA;
fsearch:thandle;
t:BOOL;
lol:boolean;
begin
fsearch:=FindFirstFile(pchar(filename) and pchar(dir),Data);
if fsearch <> INVALID_HANDLE_VALUE then lol := false;
while lol = False do
begin
t:=FindNextFile(fsearch,Data);
Sleep(10);
if t = true then lol:=true;
end;
Result:=False;
end; |
useage:
| Code: | if ScanFolder('Somefile.exe,'C:\') = True then ShowMessage('yes')
else showmessage('no'); |
help would be appreaciated
|
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Tue Apr 08, 2008 11:16 am Post subject: |
|
|
Try to use FileExists, else use this: | Code: | function ScanFolder(filename:string;dir:string):boolean;
var
Data:TWin32FindDataA;
fsearch:thandle;
begin
result:=false;
fsearch:=FindFirstFile(pchar(dir + filename),Data);
if fsearch <> INVALID_HANDLE_VALUE then
repeat
Sleep(10);
result:=true;
until FindNextFile(fsearch,Data) = 0;
FindClose(fsearch);
end; |
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Apr 08, 2008 12:23 pm Post subject: |
|
|
| HolyBlah wrote: | Try to use FileExists, else use this: | Code: | function ScanFolder(filename:string;dir:string):boolean;
var
Data:TWin32FindDataA;
fsearch:thandle;
begin
result:=false;
fsearch:=FindFirstFile(pchar(dir + filename),Data);
if fsearch <> INVALID_HANDLE_VALUE then
repeat
Sleep(10);
result:=true;
until FindNextFile(fsearch,Data) = 0;
FindClose(fsearch);
end; |
|
HolyBlah, FileExists needs full path, also your code has error
until FindNextFile(fsearch,Data) = 0; should be:
until FindNextFile(fsearch,Data) = True;
and findclose is targeted to Sysutils, so do:
Windows.FindClose();
also, your code doesn't work well S:, no return value AT ALL, nothing happens
|
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Wed Apr 09, 2008 7:19 am Post subject: |
|
|
So here it is again: | Code: | function ScanFolder(filename:string;dir:string):boolean;
var
Data:TWin32FindDataA;
fsearch:THandle;
begin
result:=false;
fsearch:=(FindFirstFile(pchar(dir + filename),Data));
if fsearch <> -1 then
result:=true;
Windows.FindClose(fsearch);
end;
|
Btw: You don't have to compare boolean to true/false.
|
|
| Back to top |
|
 |
Hixk is my hero Grandmaster Cheater
Reputation: 0
Joined: 22 Sep 2007 Posts: 611
|
Posted: Wed Apr 09, 2008 4:57 pm Post subject: |
|
|
Is this the sort of thing you wanted?
| Code: | function ScanFolder(FileName:String; Directory:String):Boolean;
var
FileFound: TSearchRec;
begin
if FindFirst(Directory + FileName, faAnyFile, FileFound) = 0 then begin
Result := True;
FindClose(FileFound);
end else begin
Result := False;
FindClose(FileFound);
end;
end; |
And as HolyBlah said, you don't have to compare to True / False, so usage would be:
| Code: | procedure TForm1.Button1Click(Sender: TObject);
begin
if ScanFolder('WINDOWS','C:\') then begin // Note the lack of = True
ShowMessage('WINDOWS was found in C:\');
end else begin
ShowMessage('WINDOWS was not found in C:\');
end;
end; |
_________________
I'm LolSalad. |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Fri Apr 11, 2008 9:49 am Post subject: |
|
|
| Hixk is my hero wrote: | Is this the sort of thing you wanted?
| Code: | function ScanFolder(FileName:String; Directory:String):Boolean;
var
FileFound: TSearchRec;
begin
if FindFirst(Directory + FileName, faAnyFile, FileFound) = 0 then begin
Result := True;
FindClose(FileFound);
end else begin
Result := False;
FindClose(FileFound);
end;
end; |
And as HolyBlah said, you don't have to compare to True / False, so usage would be:
| Code: | procedure TForm1.Button1Click(Sender: TObject);
begin
if ScanFolder('WINDOWS','C:\') then begin // Note the lack of = True
ShowMessage('WINDOWS was found in C:\');
end else begin
ShowMessage('WINDOWS was not found in C:\');
end;
end; |
|
FFS i know you don't need to use the = True/False, but boolean is for True/False only (the function must return a value), unless HolyBlah will use BOOL
|
|
| Back to top |
|
 |
|