Recent

Author Topic: Accessing File Properties  (Read 2926 times)

stephanos

  • New Member
  • *
  • Posts: 17
Accessing File Properties
« on: October 24, 2021, 07:30:46 pm »
Dear All

I am developing further features to my MP3 playlist writing programme using Lazarus 2.0.8.  The programme lives on the player.  So far I can select a number of files (that are on the player) and write them into a Memo, do a little bit of editing, and save the Memo content to the M3U file that is also on the player.

I will also write a feature to perform validation on the path/filenames as the Play List feature of the MP3 player only reads file names with characters of 1 Byte and between ALT+32-255, 256-275, 278-287.  An interesting limitation of the Player.

I am thinking ahead and will need to add another feature.  I want to add up the play time of each MP3 file that is in the Memo.  That way I will know if I have enough music to last the journey.

I have done some searches but cannot find anything that I understand.  What do I enter in Uses?  If someone can give the names of functions I can go looking.  So far, and with the help of this forum, I have been capturing multiple names of files using FileNameEdit1Change, and writing all the selected paths/file names to a string:
     
Code: Pascal  [Select][+][-]
  1. TheFileName := FileNameEdit1.DialogFiles.Text;

But I am sure that is not the way to go as I now want system information in the form of file properties, namely “Length”.

So here I am again, any help welcomed, but remember I do not know much

Thanks

Stephanos
Using Windows 10

jamie

  • Hero Member
  • *****
  • Posts: 5013
Re: Accessing File Properties
« Reply #1 on: October 24, 2021, 07:49:33 pm »
use "FileSize" or "FindFirst" for each name string in the list.

if using findfirst make sure you close the search record before starting the next file name to search..

The FindFirst has information about the file, size, date etc.
The only true wisdom is knowing you know nothing

stephanos

  • New Member
  • *
  • Posts: 17
Re: Accessing File Properties
« Reply #2 on: October 24, 2021, 11:47:37 pm »
Dear Jamie

Thanks.  I went here:
https://www.freepascal.org/docs-html/rtl/sysutils/findfirst.html
to research FindFirst.  The properties this function can detect are read-only, a system file, a directory, and hidden.  Moreover, I will not need to search a TsearchRec as I want to selected a file using FileNameEdit1Change.  Having selected it the path/filename is written to a string. At that stage I want to extract the data for the files property: Length, add that to a total etc.

If I can do this for one file I am sure I can then teach myself how to do it for multiple files selected at the same time.

Any further help appreciated

Stephanos

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1679
  • Former Delphi 1-7, 10.2 user
Re: Accessing File Properties
« Reply #3 on: October 25, 2021, 12:15:22 am »
See FpFstat for file properties.

Lazarus 2.3, FPC 3.3.1 macOS 12.0.1 x86_64 Xcode 13.1
Lazarus 2.3, FPC 3.3.1 macOS 12.0.1 aarch64 Xcode 13.1
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 VM

Zvoni

  • Hero Member
  • *****
  • Posts: 803
Re: Accessing File Properties
« Reply #4 on: October 25, 2021, 08:54:46 am »
Since he wants the playing time:
https://stackoverflow.com/questions/1065086/duration-of-an-mp3-wav-audio-file

The "hard" way would be to use the ffmpeg-libs and read the duration directly
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

MarkMLl

  • Hero Member
  • *****
  • Posts: 3463
Re: Accessing File Properties
« Reply #5 on: October 25, 2021, 09:00:12 am »
See FpFstat for file properties.

Or FileSize(), or FpStat(). Note the distinction between functions which require an opened file, and those which operate on the name.

This should really be a question about how to use the "See also" link near the bottom of most documentation pages, or the indexing links presented at the top.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BobDog

  • Full Member
  • ***
  • Posts: 168
Re: Accessing File Properties
« Reply #6 on: October 25, 2021, 06:22:20 pm »

Regarding:
"I am thinking ahead and will need to add another feature.  I want to add up the play time of each MP3 file that is in the Memo"
...
Windows using mcisendstring-- duration
and using filesize -- size in bytes (same as file -properties)
Code: Pascal  [Select][+][-]
  1.  function mciSendString(s:ansistring;p1:pointer;p2:longword;p3:longword):integer;cdecl external 'Winmm.dll' name 'mciSendStringA' ;
  2.    
  3.     function _filesize(_file:string):int64; //standard filelength in bytes
  4.      var
  5.         f:file of byte;
  6.         begin
  7.         AssignFile(f,_file);
  8.         Reset(f);
  9.         _filesize:=FileSize(f);
  10.         close(f);
  11.         end;
  12.    
  13.    
  14.     function mp3length(_file:string):int64;
  15.       type
  16.     chars = array[0..50] of char;
  17.     var
  18.     open,lngth,finish:ansistring;
  19.     length:chars;
  20.     tmp:integer;
  21.     l:int64;
  22.     begin
  23.      open:= 'open  '+_file+' type mpegvideo alias file1' ;
  24.       finish:= 'close file1';
  25.      mciSendString(pchar(open), nil, 0, 0);
  26.      lngth:= 'status file1 length';
  27.      mciSendString(pchar(lngth),@length, 50,0) ;
  28.      val(length,l,tmp);
  29.        mciSendString(pchar(finish),nil, 0, 0);
  30.      exit(l);
  31.     end;
  32.    
  33.     var
  34.     myfile:ansistring='C:\Users\Computer\Desktop\fb\pascal\mystuff\country.mp3'; //<<----- put in your path here.
  35.  
  36.  
  37.   begin
  38.  
  39.   writeln('mcisendstring method  = ', mp3length(myfile));
  40.   writeln('filesize method       = ' ,_filesize(myfile));
  41.   writeln('Press enter to end . . .');
  42.   readln;
  43.   end.
  44.  
Win 10 64 bits.
fpc 3.2.2
geany ide

 

TinyPortal © 2005-2018