Use Ui

#REPLACE TAB_FOUND_TRUE  1
#REPLACE TAB_FOUND_FALSE 0

Procedure DoReadDir String sMask
   Local String sDirEntry sFileExt

   Pos "." In sMask
   If Strmark Gt 0 Begin
      Uppercase (Right (sMask, Strlen - 1)) To sFileExt
   End
   Else Begin
      Uppercase sMask To sFileExt
   End

   Direct_Input Channel 4 ("DIR:" - sMask)
   While [Not Seqeof]
      Readln Channel 4 sDirEntry
      [Not Seqeof] Begin
         Keycheck Begin
            Send DoCheckExit
         End
         If (Left (sDirEntry, 1)) Ne "[" Begin
            Send DoProcessFile sDirEntry sFileExt
            Indicate Seqeof False
         End
      End
   End
   Close_Input Channel 0
End_Procedure // DoReadDir

Procedure DoProcessFile String sFileName String sFileExt
   Local String sOutputFileName sChar sTabChar sLineOut sCrChar
   Local Integer bTabsFound iCharRead

   Show "Processing " sFileName

   Character 9 To sTabChar
   Character 13 To sCrChar

   Move (Replace (sFileExt, Uppercase (sFileName), "DFI")) To sOutputFileName

   Show " to " sOutputFileName

   Direct_Output Channel 5 sOutputFileName
   Move "" To sLineOut
   Move TAB_FOUND_FALSE To bTabsFound
   Direct_Input Channel 6 sFileName
   While [Not Seqeof]
      Keycheck Begin
         Send DoCheckExit
      End
      Read_Block Channel 6 sChar 1
      [Not Seqeof] Begin
         Increment iCharRead
         If (Mod (iCharRead, 1000)) Eq 0 Begin
            Show "."
         End
         If sChar Eq sTabChar Begin
            If bTabsFound Eq TAB_FOUND_FALSE Begin
               Insert '"' In sLineOut At 1
               Move TAB_FOUND_TRUE To bTabsFound
            End
            Append sLineOut '" , "'
         End
         Else Begin
            If sChar Eq sCrChar Begin
               If bTabsFound Eq TAB_FOUND_TRUE Begin
                  Append sLineOut '"'
               End
               Read_Block Channel 6 sChar 1
               Writeln Channel 5 sLineOut
               Move "" To sLineOut
               Move TAB_FOUND_FALSE To bTabsFound
            End
            Else Begin
               Append sLineOut sChar
            End
         End
      End
   End

   Showln

   Close_Output Channel 5
   Close_Input Channel 6
End_Procedure // DoProcessFile

Procedure DoStart
   Local String sCmdLine

   Cmdline sCmdLine
   If sCmdLine Eq "" Begin
      Error 2000 "You have to pass an extension filter"
      Abort
   End

   Send DoReadDir sCmdLine
End_Procedure // DoStart

Procedure DoCheckExit
   Local String sKey

   Showln
   Input "A key has been pressed, exit?" sKey 1
   If sKey In "yY" Begin
      Abort
   End
End_Procedure // DoCheckExit

Send DoStart
