Search for files and folders lazarus / 13-General ---------------------------------------------------------------------- mrkaban · 2016-12-11 10:42 Hello! I am sorry for my English! I am newbie. It seems a simple task, but he could not understand. The task of finding a file or folder with the name of part of the "nameProg", if the result of search for the truth, then print a message. Just the initial stage could not deal with the search. Help me please. That procedure code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfRuchPoisk.Button1Click(Sender: TObject);Var  Info : TSearchRec;  path : String;  nameProg : String;Begin  SetCurrentDir('C:\1\');  path:= GetCurrentDir;  nameProg := 'to-do-list.zip';  If FindFirst(UTF8ToSys(path + nameProg), faAnyFile and faDirectory, Info)=1 then    beginshowmessage('Search works');    end;  FindClose(Info);  end; ---------------------------------------------------------------------- Lulu · 2016-12-11 11:16 Hello, Do you want just to know if the file exists or do you want to find it in folder ? ---------------------------------------------------------------------- mrkaban · 2016-12-11 11:26 I explain that I was going to do in the future. I'm in the SQLite database has the names of the programs, and I want to search for files and folders with these names. We need to know if there is a match. Finding the file does not need to own. At this point, I need to deal with the search. Thank you! ---------------------------------------------------------------------- Bart · 2016-12-11 13:29 Quote from: mrkaban on December 11, 2016, 11:42:53 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfRuchPoisk.Button1Click(Sender: TObject);....  If FindFirst(UTF8ToSys(path + nameProg), faAnyFile and faDirectory, Info)=1 then...  You got your bitwise operators wrong: Code: [Select]faReadOnly               : 00000001 faHidden                 : 00000010 faSysFile                : 00000100 faVolumeId               : 00001000 faDirectory              : 00010000 faArchive                : 00100000 faSymlink                : 01000000 faAnyFile                : 00111111 faAnyFile and faDirectory: 00010000  // equals to faDirectory faAnyFile or faDirectory : 00111111  // equals to faAnyFile Bart ---------------------------------------------------------------------- Bart · 2016-12-11 13:32 Quote from: mrkaban on December 11, 2016, 12:26:05 pmFinding the file does not need to own. That makes no sense to me at all. Maybe it helps if you phrase that in your native language (and say what language that is), so someone else can translate it better? Bart ---------------------------------------------------------------------- mrkaban · 2016-12-11 13:38 Native language is Russian. I am sorry, because the English language I have bad. I do not need to know where the file is located and how many matches. I need to get the value of true or false. ---------------------------------------------------------------------- Bart · 2016-12-11 13:40 Also note that Set/GetCurrentDir is not threadsafe under Windows. A simple Code: [Select]  Path := 'C:\1\'; would suffice here. You may be interested to know that there is a procedure FindAllFiles() in LazFieUtils unit, that will find all files matching a certain mask in a certain directory, and you can specify wether it should search subdirectories as well. Just in case you did not want to re-invent the wheel. If that is not flexible enough for you, then you can try my enumdirsutf8 unit (which depends on  enumdirs unit). Bart ---------------------------------------------------------------------- Bart · 2016-12-11 13:51 Quote from: mrkaban on December 11, 2016, 02:38:56 pmI do not need to know where the file is located and how many matches. I need to get the value of true or false. OK: first of all given a certain folder, there can ever only be one (and exactly one) file with the specified name (e.g. 'to-do- list.zip'). If you search subfolders, then of course there can be more. You do not need True/False if the function will return the number of found matches: if there is none, the result will be 0. For your purpose the FindAllFiles procedure will do nicely. Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var   APath, AMask: String;   AList: TStringList;   i: Integer; begin   AList := TStringList.Create;   try     APath := 'C:\1';     AMask := 'to-do-list.zip';     FindAllFiles(AList, APath, AMask, True, faDirectory);     writeln('Files found = ',AList.Count);     for i := 0 to AList.Count - 1 do       writeln(AList.Strings[i]);   finally     AList.Free;   end; end; Outputs: Code: [Select]Files found = 2 C:\1\to-do-list.zip C:\1\a\to-do-list.zip Bart ---------------------------------------------------------------------- mrkaban · 2016-12-11 13:57 I want to implement a search for programs by the presence of files or folders with specific name. The names will be added to the database. Directory programs the user specifies. That is, if the truth is, there continue to deduce information about the program in the DBGrid. The problem seems very simple, but I can not deal with it. I tried the suggested code, it gives me an error "File not open". ---------------------------------------------------------------------- howardpc · 2016-12-11 14:55 Perhaps it would be easier for you if Bart's suggestion were written as a function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i interface ...    function FilesExistInPath(aPath: string; aMask: string; out aFileList: TStringList): boolean; implementation function FilesExistInPath(aPath: string; aMask: string; out aFileList: TStringList): boolean;begin  aFileList:=TStringList.Create;  FindAllFiles(aFileList, aPath, aMask, True, faDirectory);  Result:=(aFileList.Count > 0);end; procedure TForm1.Button1Click(Sender: TObject);var  fileList: TStringList;  i: Integer;begin  try    if FilesExistInPath('c:\1\', 'to-do-list.zip', fileList) then      for i:=0 to fileList.Count-1 do        WriteLn(fileList[i])    else WriteLn('no files match');  finally    fileList.Free;  end;end;              ... ---------------------------------------------------------------------- mrkaban · 2016-12-11 15:03 Quote from: howardpc on December 11, 2016, 03:55:55 pmPerhaps it would be easier for you if Bart's suggestion were written as a function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i interface ...    function FilesExistInPath(aPath: string; aMask: string; out aFileList: TStringList): boolean; implementation function FilesExistInPath(aPath: string; aMask: string; out aFileList: TStringList): boolean;begin  aFileList:=TStringList.Create;  FindAllFiles(aFileList, aPath, aMask, True, faDirectory);  Result:=(aFileList.Count > 0);end; procedure TForm1.Button1Click(Sender: TObject);var  fileList: TStringList;  i: Integer;begin  try    if FilesExistInPath('c:\1\', 'to-do-list.zip', fileList) then      for i:=0 to fileList.Count-1 do        WriteLn(fileList[i])    else WriteLn('no files match');  finally    fileList.Free;  end;end;              ... To attach a screenshot of the error that occurs. File exists! ---------------------------------------------------------------------- Thaddy · 2016-12-11 15:07 @HowardPC I would not use an out, but a var parameter that passes in a valid TStrings instance. It is all about scoping and memory allocating functions that do not release that memory themselves. That should be avoided. Nay, even considered bad practice. Nay, even stronger, accident waiting to happen. It is hard to document and makes it hard to use. I suggest to do it like so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function FilesExistInPath(aPath: string; aMask: string; var aFileList: TStrings): boolean;begin  Assert(Assigned(aFileList),'Need to pass-in a valid Tstrings instance');  FindAllFiles(aFileList, aPath, aMask, True, faDirectory);  Result:=(aFileList.Count > 0);end;  You now directly pass in combobox.Items, Listbox.Items, memo.lines etc. ---------------------------------------------------------------------- Bart · 2016-12-11 15:17 Quote from: mrkaban on December 11, 2016, 02:57:06 pmI tried the suggested code, it gives me an error "File not open". If you refer to my code in the post above yours, this is probably because of the writeln(). Default GUI programs on Windows do not have a console. I built mine with "Win32 GUI apllication" checkbos disabled (Project Options->Compier Options->Config and Target). Bart ---------------------------------------------------------------------- ASerge · 2016-12-11 18:07 Quote from: mrkaban on December 11, 2016, 02:38:56 pmThe task of finding a file or folder ... I need to get the value of true or false. 1. If you know the directory to search for and do not need to search in subdirectories, the easiest way Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ProgramExists := FileExists(PathToSearch + ExactName) or DirectoryExists(PathToSearch + ExactName);2. If not specified the exact name, but mask Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ProgramExists := False;if FindFirst(PathToSearch + Mask, faAnyFile, SearchRec) <> 0 thenbegin  SysUtils.FindClose(SearchRec);  ProgramExists := True;end; 3. If you want to search the subdirectories, and in addition, subdirectories by mask, then it is not so trivial. Specify the task. ---------------------------------------------------------------------- molly · 2016-12-11 19:49 Quote from: ASerge on December 11, 2016, 07:07:50 pm2. If not specified the exact name, but mask Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ProgramExists := False;if FindFirst(PathToSearch + Mask, faAnyFile, SearchRec) <> 0 thenbegin  SysUtils.FindClose(SearchRec);  ProgramExists := True;end; Memory leak alert. Always call findclose, even if findfirst 'fails'. See also FindClose documentation. QuoteFindClose ends a series of FindFirst/FindNext calls, and frees any memory used by these calls. It is absolutely necessary to do this call, or huge memory losses may occur. ---------------------------------------------------------------------- ASerge · 2016-12-11 21:34 Quote from: molly on December 11, 2016, 08:49:37 pmMemory leak alert. Always call findclose, even if findfirst 'fails'. Sorry, force of habit. In earlier versions of Delphi it is even an error to call FindClose if FindFirst method failed.