home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / htmtitle.zip / htmtitle.cmd
OS/2 REXX Batch file  |  1996-02-20  |  6KB  |  139 lines

  1. /*
  2. **     Extract title from HTML document and set file name & WPS title to match.
  3. **
  4. ** On a FAT drive, the file name probably won't change, and an
  5. ** ignorable SYS0206 error will be displayed by 'rename'
  6. **
  7. ** On both FAT and HPFS drives, the .LONGNAME EA will be set to match
  8. ** the <title> tag (if present) ane will be display by WPS.
  9. **
  10. ** Create a program object for this rountine, and just drag a .htm file
  11. ** to it to rename/retitle.
  12. */
  13.  
  14. rc = RxFuncAdd('SysSearchPath', 'RexxUtil', 'SysSearchPath')
  15. rc = RxFuncAdd('SysFileTree', 'RexxUtil', 'SysFileTree')
  16. rc = RxFuncAdd('SysPutEA', 'RexxUtil', 'SysPutEA')
  17.  
  18. parse arg fspec
  19. if fspec = '' then fspec = "*.htm"
  20.  
  21. /* remove any quotes from fspec */
  22. loc = pos('"', fspec)
  23. do while loc <> 0
  24.        fspec = delstr(fspec, loc, 1)
  25.        loc = pos('"', fspec)
  26.        end
  27.  
  28. /* get list of matching files */
  29. rc = SysFileTree(fspec, file, 'FO')
  30. if file.0 = 0 then
  31.        do
  32.        say 'no files matching' '|'fspec'|'
  33.        exit
  34.        end
  35.  
  36. orgdir = directory()
  37.  
  38. /* loop thru file list */
  39. do j = 1 to file.0
  40.        say 'processing' file.j
  41.        dir = FILESPEC("drive", file.j)||FILESPEC("path", file.j)
  42.        len = length(dir)
  43.        if substr(dir, len, 1, ' ') = '\' then
  44.                dir = left(dir, len-1)
  45.        name = FILESPEC("name", file.j)
  46.        newdir = directory(dir)
  47.  
  48.        /* look for <TITLE> tag */
  49.        do while lines(name)
  50.                line = linein(name)
  51.                loc = pos("<TITLE>", line)
  52.                if loc = 0 then
  53.                        loc = pos("<title>", line)
  54.  
  55.                if loc <> 0     then
  56.                        do
  57.  
  58.                        title = substr(line, loc + 7)
  59.                        loc = pos("</TITLE>", title)
  60.                        if loc = 0 then
  61.                                loc = pos("</title>", title)
  62.  
  63.                        do while loc = 0
  64.                                if 0 = lines(name) then
  65.                                        do
  66.                                        say 'mal-formed title in' file.j
  67.                                        leave
  68.                                        end
  69.                                line = linein(name)
  70.                                title = title||line
  71.                                loc = pos("</TITLE>", title)
  72.                                if loc = 0 then
  73.                                        loc = pos("</title>", title)
  74.                                end
  75.  
  76.                        rc = stream(name, 'C', 'CLOSE')
  77.  
  78.                        if loc <> 0 then
  79.                                title = substr(title, 1, loc - 1)
  80.  
  81.                        if title = '' then do
  82.                                say 'No title found in' file.j
  83.                                leave
  84.                                end
  85.  
  86.                        LongName = title||".htm"
  87.                        /* map real file name to valid characters */
  88.                        title = strip(title,, '.')
  89.                        title = strip(title,, ' ')
  90.                        title = translate(title, '!!!!!!_ ', ':"\/<>|.')
  91.                        newname = title||".htm"
  92.  
  93.                        if name <> newname then
  94.                                do
  95.  
  96.                                /* generate unique name */
  97.                                do i = 1 to 100 while '' <> stream(newname, 'c', 'query exists')
  98.                                        newname = title||"!"||i||".htm"
  99.                                        end
  100.  
  101.                                /* change the real file name */
  102.                                /*
  103.                                RC = FileMoveFile(name, newname)
  104.                                */
  105.  
  106.                                '@rename' '"'||name||'"' '"'||newname||'"'
  107.                                if RC = 2 then
  108.                                        /* we're probably on a FAT partition */
  109.                                        newname = name
  110.  
  111.                                if (RC <> 0) & (RC <> 2) then
  112.                                        say 'error' RC 're-naming' name 'to' newname
  113.                                else do
  114.                                        /* change the LONGNAME */
  115.                                        say '.LONGNAME('||name||') ->' LongName
  116.                                        size= Length(LongName)
  117.                                        /* Set up the data for the call to SysPutEA */
  118.                                        /* Note: the FDFF is the value for length-proceeded ASCII from the */
  119.                                        if size > 255 then
  120.                                                EA_Data = 'FDFF'x || reverse(d2c(Size))  || LongName
  121.                                        else
  122.                                                EA_Data = 'FDFF'x || d2c(Size) || d2c(0) || LongName
  123.  
  124.                                        /* Call the REXX function SysPutEA to write the icon data */
  125.                                        result = SysPutEA(newname, ".LONGNAME", EA_Data)
  126.  
  127.                                        /* Inform the user of the result code */
  128.                                        if result <> 0 then
  129.                                                say 'Error' result 'setting .LONGNAME to' LongName
  130.                                        end
  131.                                end
  132.                        LEAVE
  133.                        end
  134.                end
  135.                rc = stream(name, 'C', 'CLOSE')
  136.        end
  137. orgdir = directory(orgdir)
  138. exit
  139.