Recent

Author Topic: Directory of TFileNameEdit  (Read 11969 times)

asdf

  • Sr. Member
  • ****
  • Posts: 310
Directory of TFileNameEdit
« on: December 15, 2010, 05:49:23 pm »
if fileexists(FileNameEdit1.Text) then
if fileexists(FileNameEdit2.Text) then
if directory of FileNameEdit1.Text and FileNameEdit2.Text is the same then

In order to  get all filenames greater than or equal to FileNameEdit1.Text and less than or equal to FileNameEdit2.Text in 'only' the same directory. Please help writing code for the above red-colored line with great thanks.
Lazarus 1.2.4 / Win 32 / THAILAND

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Directory of TFileNameEdit
« Reply #1 on: December 15, 2010, 05:52:26 pm »
Code: [Select]
if ExtractFilePath(FileNameEdit1.Text) = ExtractFilePath(FileNameEdit2.Text) then

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Directory of TFileNameEdit
« Reply #2 on: December 15, 2010, 06:53:07 pm »
One more question please...

if fileexists(FileNameEdit1.Text) then
if fileexists(FileNameEdit2.Text) then
if ExtractFilePath(FileNameEdit1.Text) = ExtractFilePath(FileNameEdit2.Text) then
for i:= 0 to number of files in directory of FileNmaeEdit1 do begin
if file_in_directory_of_FileNmaeEdit1(i) = FileNameEdit1.Text then

Please help writing code for the above red-colored line.
Thank you again in advance.
Lazarus 1.2.4 / Win 32 / THAILAND

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Directory of TFileNameEdit
« Reply #3 on: December 15, 2010, 07:00:48 pm »
What about using this ready-to-use function:

Code: [Select]
function GetFileSomewhere(FileName : String; Path : String) : string;
var
  SearchRec: TSearchRec;
  I : Integer;
  NextPath : String;
begin
  // assume failure
  Result := '';
  // adds '\' to Path
  Path := Trim(Path);
  if Path[Length(Path)] <> '\' then
    Path := Path + '\';
  // tries to find file on current directory
  if FindFirst(Path + Filename, faAnyFile, SearchRec) = 0 then
    // gets out if OK
    Result := Path + Filename
  else
  begin
    // searches all subdirectories
    sysutils.FindClose(SearchRec);
    I := FindFirst(Path + '*.*', faDirectory, SearchRec);
    // if Result <> '' then stops, otherwise searches in all subdirectories
    while (I = 0) and (Result = '') do
    begin
      // on each subdirectory searches for files which are not '.' or '..'
      if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      begin
        NextPath := Path + SearchRec.Name + '\';
        Result := GetFileSomewhere(Filename, NextPath);
        // if file found, set Path
        if Result <> '' then
          Path := NextPath;
      end;
      I := FindNext(SearchRec);
      // processes the messages
      Application.ProcessMessages;
    end;
    sysutils.FindClose(SearchRec);
  end;
  // if file not found, clears Path
  if Result = '' then
    Path := '';
end; 

Imants

  • Full Member
  • ***
  • Posts: 198
Re: Directory of TFileNameEdit
« Reply #4 on: December 15, 2010, 07:02:23 pm »
Or you can use something like this

Code: [Select]
//FFiles: TSearchRec;

if fileexists(FileNameEdit1.Text) then
if fileexists(FileNameEdit2.Text) then
if ExtractFilePath(FileNameEdit1.Text) = ExtractFilePath(FileNameEdit2.Text) then
Begin
  if FindFirstUTF8(ExtractFilePath(FileNameEdit1.Text)+ '*.*', faAnyFile - faDirectory, FFiles) <> 0 then
    Exit;

  try
   repeat
     if FFiles.Name = ExtractFileName(FileNameEdit1.Text) then
     Begin
       ...
     end;
   until FindNext(FFiles) <> 0
  finally
    FindClose(FFiles)
  end;
end;
« Last Edit: December 15, 2010, 07:04:00 pm by Imants »

Bart

  • Hero Member
  • *****
  • Posts: 5744
    • Bart en Mariska's Webstek
Re: Directory of TFileNameEdit
« Reply #5 on: December 15, 2010, 10:39:51 pm »
if fileexists(FileNameEdit1.Text) then
if fileexists(FileNameEdit2.Text) then
if ExtractFilePath(FileNameEdit1.Text) = ExtractFilePath(FileNameEdit2.Text) then
for i:= 0 to number of files in directory of FileNmaeEdit1 do begin
if file_in_directory_of_FileNmaeEdit1(i) = FileNameEdit1.Text then

Why do you want to iterate all files and then only (?) do something with the file specified in FilenameEdit1? You allready know it exists, so no need to iterate the entire directory to find it...

Code: [Select]
  if ExtractFilePath(FileNameEdit1.Text) = ExtractFilePath(FileNameEdit2.Text) then

Be aware that this may fail if the filesystem isn't case sensitive, for instance if FilenameEdit.Text = 'c:\foo\bar.txt' and FilenameEdit2.Text = 'C:\FOO\SOAP.TXT' then the result will be False, when in fact the paths are the same.

Bart

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Directory of TFileNameEdit
« Reply #6 on: December 16, 2010, 03:05:30 am »
Sorry,
the last statement is
if file_in_directory_of_FileNmaeEdit1(i) >= FileNameEdit1.Text then

For example, in the directory there will be only files with filename beginning with the same characters.
tfa_001.txt, tfa_002.txt, tfa_003.txt   ....  tfa_999.txt
All begin with 'tfa_'.
Sometimes user may need only from tfa_056.txt to tfa_071.txt by starting to select file in FileNameEdit1 and ending at in FileNameEdit2.
Lazarus 1.2.4 / Win 32 / THAILAND

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Directory of TFileNameEdit
« Reply #7 on: December 16, 2010, 04:22:00 am »
Dear Imants

I've tested only ...

if FFiles.Name >= ExtractFileName(FileNameEdit1.Text) then
     Begin
     CheckListBox1.Items.Add(FFiles.Name);

It added, I thought, the hidden file.
How can I filter such hidden file ?
Lazarus 1.2.4 / Win 32 / THAILAND

Imants

  • Full Member
  • ***
  • Posts: 198
Re: Directory of TFileNameEdit
« Reply #8 on: December 16, 2010, 08:13:42 am »
Dear Imants

I've tested only ...

if FFiles.Name >= ExtractFileName(FileNameEdit1.Text) then
     Begin
     CheckListBox1.Items.Add(FFiles.Name);

It added, I thought, the hidden file.
How can I filter such hidden file ?

try to use:

Code: [Select]
if FindFirstUTF8(ExtractFilePath(FileNameEdit1.Text)+ '*.*', faAnyFile - faDirectory - faHidden, FFiles) <> 0 then

Bart

  • Hero Member
  • *****
  • Posts: 5744
    • Bart en Mariska's Webstek
Re: Directory of TFileNameEdit
« Reply #9 on: December 16, 2010, 04:23:44 pm »
Sorry,
the last statement is
Code: [Select]
if file_in_directory_of_FileNmaeEdit1(i) >= FileNameEdit1.Text then

Don't do that (using >= on strings).
The expression
Code: [Select]
if 'C:\FOO\TFA_002.TXT' >= 'c:\foo\tfa_001.txt'
will evaluate to FALSE (when in your logic it should be TRUE).

Also only scan for files with the following mask: 'tfa_???.txt' and not '*.*' to ensure only processing the files that match the format you described.

Bart

 

TinyPortal © 2005-2018