home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / sfn10.zip / sfn.cmd < prev   
OS/2 REXX Batch file  |  1998-02-28  |  8KB  |  223 lines

  1. /* Short Filename Namer 1.0 - (C) 1998  Samuel Audet <guardia@cam.org> */
  2.  
  3. /* Load the usual garbage ... */
  4. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  5. call SysLoadFuncs
  6.  
  7. '@echo off'
  8.  
  9. parse arg realpath virtualpath
  10. realpath = strip(realpath)
  11. virtualpath = strip(virtualpath)
  12.  
  13. if (realpath = '') | (virtualpath = '') then do
  14.    say 'sfn <realpath> <virtualpath>'
  15.    say 'ex.: sfn c: x:'
  16.    say '     sfn f:\mp3 x:\lalala'
  17.    exit
  18. end
  19.  
  20. /* Remove trailing backslash */
  21.  
  22. if lastpos('\',realpath) = length(realpath) then realpath = left(realpath,length(realpath) - 1)
  23. if lastpos('\',virtualpath) = length(virtualpath) then virtualpath = left(virtualpath,length(virtualpath) - 1)
  24.  
  25. /*  sfns -> short filename list
  26.     lfns -> long filename list */
  27.  
  28. filecounter = 0
  29. dircounter = 0
  30.  
  31. call ProcessOneDirectory realpath,virtualpath
  32.  
  33. say 'Total files processed:' filecounter
  34. say 'Total directories processed:' dircounter
  35.  
  36. exit
  37.  
  38.  
  39. ProcessOneDirectory: Procedure expose filecounter dircounter
  40.  
  41.    parse arg realpath,virtualpath
  42.  
  43.    filesfns.0 = 0
  44.    call SysFileTree realpath'\*','filelfns','FO'
  45.    dirsfns.0 = 0
  46.    call SysFileTree realpath'\*','dirlfns','DO'
  47.  
  48.    /* process files */
  49.  
  50.    do i=1 to filelfns.0
  51.       filecounter = filecounter + 1
  52.  
  53.       lifilename = substr(filelfns.i,lastpos('\',filelfns.i)+1)
  54.  
  55.       period = lastpos('.',lifilename)
  56.       select
  57.          when period > 8 then do
  58.             leftpart = left(lifilename,8)
  59.             rightpart = '.'substr(lifilename,period+1,3)
  60.             end
  61.          when period = 0 then do
  62.             if(length(lifilename)) > 8 then
  63.                leftpart = left(lifilename,8)
  64.             else
  65.                leftpart = lifilename
  66.             rightpart = ''
  67.             end
  68.          otherwise
  69.             leftpart = left(lifilename,period-1)
  70.             rightpart = '.'substr(lifilename,period+1,3)
  71.       end
  72.  
  73.       leftpart = translate(leftpart,'_','.')  /* remove periods from left part */
  74.       sifilename = leftpart||rightpart
  75.       /* change illegal DOS 8.3 chars, gimme more! - no this is not a smily */
  76.       sifilename = translate(sifilename,'_-()!-__',' +[];=,~')
  77.  
  78.       /* check for duplicates only if filename is modified */
  79.       number = 1
  80.  
  81.       if(sifilename \= lifilename) then do until checkagain = 0
  82.          checkagain = 0
  83.  
  84.          do e=1 to filesfns.0
  85.  
  86.             sefilename = substr(filesfns.e,lastpos('\',filesfns.e)+1)
  87.             lefilename = substr(filelfns.e,lastpos('\',filelfns.e)+1)
  88.  
  89.             /* if the long version DOES NOT compare, but short version DOES, we have a problem */
  90.             if ((translate(sefilename) = translate(sifilename)) & (lefilename \= lifilename)) then do
  91.                endpos = pos('.',sefilename) - 1
  92.                if endpos < 0 then endpos = length(sefilename)
  93.                tildepos = pos('~',sefilename)
  94.                if tildepos > 0 then
  95.                   number = substr(sefilename, tildepos+1, endpos - tildepos) + 1
  96.                endpos = pos('.',sifilename) - 1
  97.                if endpos < 0 then endpos = length(sifilename)
  98.                sifilename = left(sifilename,endpos-length(number)-1)'~'number||substr(sifilename,endpos+1)
  99.                checkagain = 1
  100.             end
  101.  
  102.          end
  103.  
  104.          do e=1 to dirsfns.0
  105.  
  106.             sefilename = substr(dirsfns.e,lastpos('\',dirsfns.e)+1)
  107.             lefilename = substr(dirlfns.e,lastpos('\',dirlfns.e)+1)
  108.  
  109.             /* if the long version DOES NOT compare, but short version DOES, we have a problem */
  110.             if ((translate(sefilename) = translate(sifilename)) & (lefilename \= lifilename)) then do
  111.                endpos = pos('.',sefilename) - 1
  112.                if endpos < 0 then endpos = length(sefilename)
  113.                tildepos = pos('~',sefilename)
  114.                if tildepos > 0 then
  115.                   number = substr(sefilename, tildepos+1, endpos - tildepos) + 1
  116.                endpos = pos('.',sifilename) - 1
  117.                if endpos < 0 then endpos = length(sifilename)
  118.                sifilename = left(sifilename,endpos-length(number)-1)'~'number||substr(sifilename,endpos+1)
  119.                checkagain = 1
  120.             end
  121.  
  122.          end
  123.  
  124.       end /* if short filename does not equal long filename */
  125.  
  126.       filesfns.0 = i
  127.       filesfns.i = virtualpath'\'sifilename
  128.  
  129.       say filelfns.i '->' filesfns.i
  130.       'TvLink "'filesfns.i'" "'filelfns.i'" -rw'
  131.  
  132.    end
  133.  
  134.  
  135.    /* process directories */
  136.  
  137.    do i=1 to dirlfns.0
  138.       dircounter = dircounter + 1
  139.  
  140.       lifilename = substr(dirlfns.i,lastpos('\',dirlfns.i)+1)
  141.  
  142.       period = lastpos('.',lifilename)
  143.       select
  144.          when period > 8 then do
  145.             leftpart = left(lifilename,8)
  146.             rightpart = '.'substr(lifilename,period+1,3)
  147.             end
  148.          when period = 0 then do
  149.             if(length(lifilename)) > 8 then
  150.                leftpart = left(lifilename,8)
  151.             else
  152.                leftpart = lifilename
  153.             rightpart = ''
  154.             end
  155.          otherwise
  156.             leftpart = left(lifilename,period-1)
  157.             rightpart = '.'substr(lifilename,period+1,3)
  158.       end
  159.  
  160.       leftpart = translate(leftpart,'_','.')  /* remove periods from left part */
  161.       sifilename = leftpart||rightpart
  162.       /* change illegal DOS 8.3 chars, gimme more! - no this is not a smily */
  163.       sifilename = translate(sifilename,'_-()!-_',' +[];=,')
  164.  
  165.       /* check for duplicates only if filename is modified */
  166.       number = 1
  167.  
  168.       if(sifilename \= lifilename) then do until checkagain = 0
  169.          checkagain = 0
  170.  
  171.          do e=1 to filesfns.0
  172.  
  173.             sefilename = substr(filesfns.e,lastpos('\',filesfns.e)+1)
  174.             lefilename = substr(filelfns.e,lastpos('\',filelfns.e)+1)
  175.  
  176.             /* if the long version DOES NOT compare, but short version DOES, we have a problem */
  177.             if ((translate(sefilename) = translate(sifilename)) & (lefilename \= lifilename)) then do
  178.                endpos = pos('.',sefilename) - 1
  179.                if endpos < 0 then endpos = length(sefilename)
  180.                tildepos = pos('~',sefilename)
  181.                if tildepos > 0 then
  182.                   number = substr(sefilename, tildepos+1, endpos - tildepos) + 1
  183.                endpos = pos('.',sifilename) - 1
  184.                if endpos < 0 then endpos = length(sifilename)
  185.                sifilename = left(sifilename,endpos-length(number)-1)'~'number||substr(sifilename,endpos+1)
  186.                checkagain = 1
  187.             end
  188.  
  189.          end
  190.  
  191.          do e=1 to dirsfns.0
  192.  
  193.             sefilename = substr(dirsfns.e,lastpos('\',dirsfns.e)+1)
  194.             lefilename = substr(dirlfns.e,lastpos('\',dirlfns.e)+1)
  195.  
  196.             /* if the long version DOES NOT compare, but short version DOES, we have a problem */
  197.             if ((translate(sefilename) = translate(sifilename)) & (lefilename \= lifilename)) then do
  198.                endpos = pos('.',sefilename) - 1
  199.                if endpos < 0 then endpos = length(sefilename)
  200.                tildepos = pos('~',sefilename)
  201.                if tildepos > 0 then
  202.                   number = substr(sefilename, tildepos+1, endpos - tildepos) + 1
  203.                endpos = pos('.',sifilename) - 1
  204.                if endpos < 0 then endpos = length(sifilename)
  205.                sifilename = left(sifilename,endpos-length(number)-1)'~'number||substr(sifilename,endpos+1)
  206.                checkagain = 1
  207.             end
  208.  
  209.          end
  210.  
  211.       end /* if short filename does not equal long filename */
  212.  
  213.       dirsfns.0 = i
  214.       dirsfns.i = virtualpath'\'sifilename
  215.  
  216.       say 'Making' dirsfns.i
  217.       call sysmkdir dirsfns.i
  218.       call ProcessOneDirectory dirlfns.i,dirsfns.i
  219.  
  220.    end
  221.  
  222. return
  223.