program bashbash;
{$mode objfpc}{$H+}
{.$define mysetup} // for my personal setup not matching yours.
// https://forum.lazarus.freepascal.org/index.php/topic,64932.0.html
// https://wiki.freepascal.org/FindAllFiles
uses
classes, process, fileutil, sysutils;
const
{$ifdef mysetup}
source = './flacs';
{$else}
source = '/mnt/music/bashmusic/source1';
{$endif}
FindRecursive = false; // search source directory recursively for flac files (true (=yes)/false (=no)). Your batch file doesn't.
procedure Print(s: string); overload;
begin
{$ifdef mysetup}
writeln(s);
{$else}
// in case you wish to use lazarus and display the output to a memo
// then your code should be looking something like:
Form1.InfoMemo.Append(s);
{$endif}
end;
procedure Print(s: string; const args: array of const); overload;
begin
print(Format(s, args));
end;
procedure test;
var
FileNames: TStringList;
Filename : string;
command : string;
parameter: string;
rc_result: string;
Artist: string;
Title : string;
Album : string;
begin
// create a stringlist that will contain the found filenames
FileNames := TStringList.Create;
try
// find all the files in directory source that matches the mask *.flac
FindAllFiles(Filenames, source, '*.flac', FindRecursive);
print('found %d files.', [FileNames.Count]);
// iterate through the FileNames list
// equivalent to your bash line: for filename in $source/*.flac ; do
for FileName in FileNames do
begin
print('');
print('handling filename %s', [Filename]);
// equivalent of your bash line: metaflac --show-tag=artist $filename
command := 'metaflac';
parameter := '--show-tag=artist';
if RunCommand(command, [parameter, FileName], rc_result) then
begin
Artist := rc_result.TrimRight;
end
else print('error running command %s %s', [command, parameter]);
// equivalent of your bash line: metaflac --show-tag=title $filename
command := 'metaflac';
parameter := '--show-tag=title';
if RunCommand(command, [parameter, FileName], rc_result) then
begin
Title := rc_result.TrimRight;
end
else print('error running command %s %s', [command, parameter]);
// equivalent of your bash line: metaflac --show-tag=album $filename
command := 'metaflac';
parameter := '--show-tag=album';
if RunCommand(command, [parameter, FileName], rc_result) then
begin
Album := rc_result.TrimRight;
end
else print('error running command %s %s', [command, parameter]);
// show the resulting answers:
print('Filename = %s', [Filename]);
print('Artist = %s', [Artist]);
print('Title = %s', [Title]);
print('Album = %s', [Album]);
end;
finally;
FileNames.Free;
end;
end;
begin
test;
end.