uses crt,dos;
var
  F   : SearchRec;
  path:pathstr;

procedure dosrun(command:string);
{$M 8192,0,0}
begin
  if Command <> '' then
    begin
      SwapVectors;
      Exec(GetEnv('COMSPEC'), '/C ' + Command);
      SwapVectors;
      if DosError <> 0 then
        WriteLn('Could not execute COMMAND.COM');
    end;
end;

procedure renamefile(filename:string);
type
 b=char;
var
 fi:file of b;
 data:b;
 si,i:longint;
 tag,st,song,sing:string;

begin
 assign(fi,filename);
 reset(fi);
  si:=filesize(fi);
  si:=si-128;
  st:='';song:='';sing:='';
  seek(fi,si);
  tag:='';
  for i:=1 to 3 do
  begin
    read(fi,data);
    tag:=tag+data;
  end;
 if tag='TAG' then
  begin
   for i:=1 to 30 do
    begin
     read(fi,data);
     if (data<>#0) and (data>#47) and (data<>',') and (data<>'-') then song:=song+data;
    end;
   for i:=1 to 30 do
    begin
     read(fi,data);
     if (data<>#0) and (data>#32) and (data<>',')and (data<>'-')then sing:=sing+data;
    end;
   close(fi);
   if (sing<>'') and (song<>'') then sing:=sing+'-';
   st:=sing+song+'.mp3';
   if st<>'.mp3' then
    begin
     writeln(st);
     dosrun('ren '+filename+' '+st);
    end;
  end
  else
   begin
  seek(fi,0);
  tag:='';
  for i:=1 to 3 do
  begin
    read(fi,data);
    tag:=tag+data;
  end;
  if tag='ID3' then
  begin
   writeln('tag2:',filename);
 { for i:=1 to 203 do
  begin
    read(fi,data);
    sing:=sing+data;
  end;
   writeln(sing);
  }
  end;
   close(fi);
   end;

end;

begin
  clrscr;
  path:='*.MP3';
  FindFirst(Path, ReadOnly + Directory + Archive, F);
  while (DosError = 0)  do
  begin
    renamefile(f.name);
    FindNext(F);
  end;
end.
