home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / opus / v4 / arexx / extractfileslha.rexx < prev    next >
OS/2 REXX Batch file  |  1977-12-31  |  7KB  |  331 lines

  1. /*rx
  2.  *
  3.  * ExtractFilesLhA.rexx - Extract selected files from an LhA archive
  4.  *                        previously listed in a DOpus window by ListLha.rexx
  5.  *
  6.  * $VER: ExtractFilesLhA 40.9 (22/05/94) by Geoff Seeley
  7.  *
  8.  * Usage: ARexx command ExtractFilesLhA.rexx (from DOpus)
  9.  *
  10.  */
  11.  
  12. /* configuration variables (change these to suit your setup) */
  13.  
  14. LhaCommand    = 'XFH_Work:C/Archivers/File/LhA '
  15. OutputWindow  = '>CON:30/145/640/100/LhA_Output/CLOSE/SCREENDOPUS.1 '
  16. InputWindow   = '<CON:30/245/640/50/LhA_Input/AUTO/SCREENDOPUS.1 '
  17. ExtractList   = 'T:lha_file_list'
  18. ListLhARexx   = 'RX DOpus:ARexx/ListLhA.rexx'
  19.  
  20. /*--------------------------------------------------------------------------*/
  21. /* misc. variables */
  22.  
  23. DOpusPort = 'DOPUS.1'
  24.  
  25. if ~show(l,"rexxsupport.library") then        
  26.     call addlib("rexxsupport.library",0,-30,0)
  27.  
  28. /* make sure we've got somebody to talk to */
  29.  
  30. if showlist('Ports', DOpusPort) = 0 then do
  31.    say 'Directory Opus ARexx port not found. Aborting.'
  32.    call CleanUp
  33. end
  34.  
  35. address 'DOPUS.1'
  36. options results
  37.  
  38. Busy on
  39.  
  40. /* get the path/name of the LhA archive file */
  41.  
  42. 'Status 14 -1'
  43. LhaArchive = result
  44.  
  45. /* make sure it's an LhA archive listing buffer */
  46.  
  47. if (IsLhAFile(LhaArchive) = 0) then do
  48.  
  49.    /* try other window */
  50.  
  51.    OtherWindow
  52.    'Status 14 -1'
  53.    LhaArchive = Result
  54.    
  55.    if (IsLhAFile(LhaArchive) = 0) then do
  56.  
  57.       /* try first selected file */
  58.  
  59.       OtherWindow
  60.       'GetNextSelected -1'
  61.       LhaArchive = RESULT
  62.       if LhaArchive ~= 0 & IsLhaFile(LhaArchive) then do
  63.  
  64.          /* list it in the window first */
  65.  
  66.          address COMMAND ListLhARexx
  67.  
  68.          Busy on
  69.       end
  70.       else do
  71.  
  72.          /* try first selected file in other window */
  73.  
  74.          OtherWindow
  75.          'GetNextSelected -1'
  76.          LhaArchive = RESULT
  77.          if LhaArchive ~= 0 & IsLhaFile(LhaArchive) then do
  78.  
  79.             /* list it in the window first */
  80.  
  81.             address COMMAND ListLhARexx
  82.  
  83.             Busy on
  84.          end
  85.          else do
  86.  
  87.             Notify "Sorry, no LhA archive buffer found. You must use the ListLha button first."
  88.             call CleanUp
  89.          end
  90.       end
  91.    end
  92.  
  93. end
  94.  
  95. TopText "Extracting File(s) From an LhA Archive"
  96.  
  97. call FindLhAPath
  98. LhaArchive = LhaPath || LhaArchive
  99.  
  100. /* check for existance of archive */
  101.  
  102. if ~exists(LhaArchive) then do
  103.  
  104.    Notify "Can't seem to find '" || LhaArchive || "'. Aborting."
  105.    call CleanUp
  106.  
  107. end
  108.  
  109. /* get the destination path from the other window */
  110.  
  111. OtherWindow
  112.  
  113. 'Status 13 -1'
  114. DestinationPath = result
  115.  
  116. OtherWindow
  117.  
  118. /* check for valid destination path */
  119.  
  120. if DestinationPath = '' then do
  121.  
  122.    Notify "The destination path is invalid\Pick another destination"
  123.    call CleanUp
  124.  
  125. end
  126.  
  127. /* build extract from settings */
  128.  
  129. LhaExtractCmd = GetLhaOpts()
  130.  
  131. /* get list of selected entries */
  132.  
  133. GetSelectedAll
  134. SelectedEntries = result
  135.  
  136. if SelectedEntries = 'RESULT' then do
  137.  
  138.    /* none selected, delete all? */
  139.  
  140.    Request "Extract *ALL* files from LhA archive?"
  141.    DoAll = RESULT
  142.    Busy on
  143.  
  144.    if DoAll = "1" then do
  145.  
  146.       TopText "Extracting file(s) from LhA archive..."
  147.  
  148.       CliCommand = LhaCommand || OutputWindow || InputWindow || LhaExtractCmd ||Quote(LhaArchive)
  149.       CliCommand = CliCommand || ' "*"'
  150.  
  151.       pragma('Directory', DestinationPath)
  152.       address command CliCommand
  153.  
  154.       OtherWindow
  155.       'Rescan -1'
  156.  
  157.       TopText "Finished extracting selected file(s) from LhA archive."
  158.    end
  159.    else
  160.       TopText "Extraction Of File(s) Aborted..."
  161.  
  162.    call CleanUp
  163. end
  164.  
  165. NumberOfEntries = words(SelectedEntries)
  166.  
  167. /* extract the files */
  168.  
  169. call ExtractFileList
  170.  
  171. /* update destination window, tell user we are finished */
  172.  
  173. OtherWindow
  174. 'Rescan -1'
  175.  
  176. TopText "Finished extracting selected file(s) from LhA archive."
  177.  
  178. call CleanUp
  179.  
  180. exit
  181.  
  182. /*----------------------------------------------------------------------------*/
  183.  
  184. ExtractFileList: /* build a list of selected files, extract list */
  185.  
  186.    if exists(ExtractList) then
  187.       delete(ExtractList)
  188.  
  189.    if ~open(FileList, ExtractList, 'W') then do
  190.       Notify "Can't open file " || ExtractList
  191.       call CleanUp
  192.    end
  193.  
  194.    TopText "Creating file(s) list..."
  195.  
  196.    do EntryNumber = 1 to NumberOfEntries
  197.       Index = word(SelectedEntries, EntryNumber)
  198.       GetEntry Index+1
  199.       Entry = result
  200.       File = substr(Entry, 10)
  201.       File = Quote(File)
  202.       call ReplaceMetaChars
  203.       if ExtOpts = '-x0 e ' then do
  204.          File = GetFileInPath(File)
  205.          File = Quote(File)
  206.       end
  207.       writeln(FileList, File)
  208.       selection = Index||' 0 0'
  209.       SelectEntry selection
  210.    end
  211.  
  212.    'DisplayDir -1'
  213.  
  214.    close(FileList)
  215.  
  216.    /* form CLI command and extract the file(s) */
  217.  
  218.    TopText "Extracting file(s) from LhA archive..."
  219.  
  220.    CliCommand = LhaCommand || OutputWindow || InputWindow || LhaExtractCmd ||Quote(LhaArchive)
  221.    CliCommand = CliCommand || ' @' || ExtractList
  222.  
  223.    pragma('Directory', DestinationPath)
  224.    address command CliCommand
  225.  
  226. return
  227.  
  228. /*--------------------------------------------------------------------------*/
  229.  
  230. IsLhAFile: procedure   /* look at extension, return 1 if right, else 0 */
  231.  
  232.    parse arg AFileName
  233.  
  234.    lps = lastpos(".", AFileName)               
  235.    if lps = 0 then                           
  236.       return 0
  237.  
  238.    FileExt = upper(right(AFileName,length(AFileName)-lps))
  239.  
  240.    if FileExt ~= "LHA" & FileExt ~= "LZH" then
  241.       return 0
  242.    else
  243.       return 1
  244.  
  245. return 0
  246.  
  247. /*--------------------------------------------------------------------------*/
  248.  
  249. FindLhAPath: /* grab invisible file path to archive */
  250.  
  251.    /* find number of entries, path is the last one */
  252.  
  253.    'Status 6 -1'
  254.  
  255.    GetEntry Result
  256.    LhaPath = Result
  257.  
  258. return
  259.  
  260. /*--------------------------------------------------------------------------*/
  261.  
  262. ReplaceMetaChars: /* replace special wildcards with ? */
  263.  
  264.    File = translate(File, '???', '()`', '?')
  265.  
  266. return
  267.  
  268. /*--------------------------------------------------------------------------*/
  269.  
  270. GetLhaOpts: procedure expose ExtOpts OvrOpts /* get LhA options */
  271.  
  272.    if open('opts', 'ENV:LHAREXX_EXT_OPTS', 'R') then do
  273.       ExtOpts = readln('opts')
  274.       close('opts')
  275.    end
  276.    else
  277.       ExtOpts = '-x x '
  278.  
  279.    if open('opts', 'ENV:LHAREXX_OVR_OPTS', 'R') then do
  280.       OvrOpts = readln('opts')
  281.       close('opts')
  282.    end
  283.    else
  284.       OvrOpts = '-m0 '
  285.  
  286.    Opts = OvrOpts||ExtOpts
  287.  
  288. return Opts
  289.  
  290. /*--------------------------------------------------------------------------*/
  291.  
  292. GetFileInPath: procedure /* return file from path */
  293.  
  294.    parse arg FilePath
  295.    if lastpos('/', FilePath) = 0 then
  296.       return StripQuotes(FilePath)
  297.    DivPos = max(lastpos(':', FilePath),lastpos('/', FilePath)) +1
  298.    parse var FilePath PathSpec =DivPos FileName
  299.    Filename = StripQuotes(Filename)
  300.  
  301. return Filename
  302.  
  303. /*--------------------------------------------------------------------------*/
  304.  
  305. Quote: procedure /* add quotes to string */
  306.  
  307.    parse arg string
  308.  
  309. return '"'||string||'"'
  310.  
  311. /*---------------------------------------------------------------------------*/
  312.  
  313. StripQuotes: procedure /* strip quotes from string */
  314.  
  315.    parse arg string
  316.  
  317. return strip(string,, '"')
  318.  
  319. /*---------------------------------------------------------------------------*/
  320.  
  321. CleanUp: /* clean up files and exit */
  322.  
  323.    if exists(ExtractList) then
  324.       delete(ExtractList)
  325.  
  326.    Busy off
  327.  
  328.    exit 0
  329.  
  330. return
  331.