home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / sosutl12.zip / bin / chname.cmd next >
OS/2 REXX Batch file  |  1993-10-18  |  7KB  |  224 lines

  1. /* ----------
  2.  * chname.cmd (C) SuperOscar Softwares, Tommi Nieminen 1993.
  3.  * ----------
  4.  * Changes HPFS names to FAT names and vice versa. When truncating names
  5.  * for FAT systems, long HPFS names are saved in `.LONGNAME' extended
  6.  * attribute.
  7.  *
  8.  * Usage:
  9.  *      [D:\] chname FILE ... [ /f /h /q /x /? ]
  10.  *
  11.  * Switches:
  12.  *      /f      HPFS to FAT (default)
  13.  *      /h      FAT to HPFS
  14.  *      /q      Quiet mode: no output
  15.  *      /x      Send output to OS2QUE instead of standard output
  16.  *      /?      Show help page and quit.
  17.  *
  18.  * 20-Aug-1993  v1.0: first PD version.
  19.  * 28-Aug-1993  v1.1: Options /q and /x implemented. Structural changes.
  20.  * 4-Oct-1993   v1.1a: fixes in the help page and messages.
  21.  * 5-Oct-1993   v1.1b: /f and /h cannot be used simultaneously; neither
  22.  *              can /q and /x.
  23.  */
  24.  
  25. "@echo off"
  26.  
  27. Parse Arg files "/"switches
  28.  
  29. Signal On Halt Name Quit
  30.  
  31.   /* Program name and version */
  32. prgname = "ChName v1.1b"
  33.  
  34.   /* Constants */
  35. TRUE = 1
  36. FALSE = 0
  37.  
  38.   /* Load SysFileTree(), SysPutEA(), and SysGetEA() */
  39. Call RxFuncAdd "SysFileTree", "RexxUtil", "SysFileTree"
  40. Call RxFuncAdd "SysPutEA", "RexxUtil", "SysPutEA"
  41. Call RxFuncAdd "SysGetEA", "RexxUtil", "SysGetEA"
  42.  
  43.   /* Defaults */
  44. hpfs2fat = TRUE
  45. verbose = 1             /* 0=quiet, 1=stdout, 2=OS2QUE */
  46.  
  47.   /* Check switches */
  48. If switches <> "" Then Do
  49.       /* Convert to upper case */
  50.     switches = Translate(switches)
  51.  
  52.       /* Separators to blanks */
  53.     switches = Translate(switches, " ", "/")
  54.  
  55.     Do i = 1 To Length(switches)
  56.         ch = SubStr(switches, i, 1)
  57.         Select
  58.             When ch == " " Then
  59.                 Iterate
  60.  
  61.             When ch == "F" Then
  62.                 hpfs2fat = TRUE
  63.  
  64.             When ch == "H" Then
  65.                 hpfs2fat = FALSE
  66.  
  67.             When ch == "Q" Then
  68.                 verbose = 0
  69.  
  70.             When ch == "X" Then
  71.                 verbose = 2
  72.  
  73.             When ch == "?" Then
  74.                 Call ShowHelp
  75.  
  76.             Otherwise
  77.                 Call Error "invalid switch '"ch"' (use /? to get help)"
  78.         End
  79.     End
  80. End
  81.  
  82.   /* Aha, you tried to fool me! */
  83. If Pos("F", switches) > 0 & Pos("H", switches) > 0 Then
  84.     Call Error "/f and /h switches cannot be used simultaneously"
  85. If Pos("Q", switches) > 0 & Pos("X", switches) > 0 Then
  86.     Call Error "/q and /x switches cannot be used simultaneously"
  87.  
  88.   /* If there are no file parameters, and we haven't yet encountered
  89.    * /? switch, an error situation results.
  90.    */
  91. If files == "" Then
  92.     Call Error "no files specified (use /? to get help)"
  93.  
  94. Do i = 1 To Words(files)
  95.     mask = Word(files, i)
  96.     flist = mask
  97.  
  98.       /* Expand wildcards if such exist */
  99.     If Pos("?", mask) <> 0 | Pos("*", mask) <> 0 Then Do
  100.         flist = ""
  101.         res = SysFileTree(mask, fstem, "fo")
  102.         Do j = 1 To fstem.0
  103.               /* Spaces cause problems when handling the files, so we
  104.                * change them to bars, which is an invalid character in
  105.                * file names.
  106.                */
  107.             flist = flist || Translate(fstem.j, "|", " ") || " "
  108.         End
  109.     End
  110.  
  111.       /* Rename files */
  112.     Do j = 1 To Words(flist)
  113.           /* Separate one file from the list, and change bars back to
  114.            * spaces if this was done before.
  115.            */
  116.         file = Translate(Word(flist, j), " ", "|")
  117.  
  118.           /* Check whether file exists */
  119.         If Stream(file, "c", "query exists") == "" Then Do
  120.             Say prgname": File '"file"' not found"
  121.             Iterate
  122.         End
  123.  
  124.           /* Compose a new name */
  125.         If hpfs2fat == TRUE Then Do
  126.             newname = Hpfs2Fat(file)
  127.             If newname == file Then Iterate
  128.               /* Save the name in EAs */
  129.             res = SysPutEA(file, ".LONGNAME", file)
  130.         End
  131.         Else Do
  132.             res = SysGetEA(file, ".LONGNAME", newname)
  133.             If res <> 0 | newname == file | newname == "" Then Iterate
  134.         End
  135.  
  136.           /* Show changes unless /q is specified; if /x is specified,
  137.            * send the display to a queue.
  138.            */
  139.         If queout == TRUE Then
  140.             Queue file "->" newname
  141.         Else If quiet == FALSE Then
  142.             Say file "->" newname
  143.  
  144.           /* Rename file */
  145.         'ren "'file'" "'newname'" >nul'
  146.     End
  147. End
  148.  
  149. Quit:
  150.     Exit 0
  151.  
  152.   /* Change HPFS names to FAT format */
  153. Hpfs2Fat: Procedure Expose prgname
  154.     Parse Arg pathname
  155.  
  156.     name = pathname
  157.     path = ""
  158.  
  159.     If Pos("\", name) > 0 Then Do
  160.         name = SubStr(name, LastPos("\", name) + 1)
  161.         path = Left(name, LastPos("\", name))
  162.     End
  163.  
  164.       /* Spaces to underlines */
  165.     name = Translate(name, "_", " ")
  166.  
  167.       /* Separate name and extension */
  168.     parts = Translate(name, " ", ".")
  169.     namepart = Word(parts, 1)
  170.     extpart = ""
  171.     If Words(parts) > 1 Then
  172.         extpart = "." || Word(parts, Words(parts))
  173.  
  174.       /* Does name already satisfy FAT file system? -- At most two dot-
  175.        * separated parts, length of name part 1..8 chars, length of
  176.        * extension part 1..3 chars plus the initial dot.
  177.        */
  178.     If Words(parts) > 2 | Length(namepart) > 8 | Length(extpart) > 4 Then Do
  179.         namepart = DelStr(namepart, 9)
  180.         extpart = DelStr(extpart, 5)
  181.         result = namepart || extpart
  182.  
  183.           /* Does 'result' exist already? */
  184.         If Stream(result, "c", "query exists") <> "" Then Do
  185.             len = 1
  186.             Do n = 0 While Stream(result, "c", "query exists") <> ""
  187.                 namepart = DelStr(namepart, 9 - len)
  188.                 result = namepart || Format(n, len) || extpart
  189.                 len = Length(((n + 1) * 10) % 10)
  190.             End
  191.         End
  192.     End
  193.     Else
  194.         result = name
  195.  
  196. Return Translate(result)
  197.  
  198. ShowHelp: Procedure Expose prgname
  199.     Say prgname "(C) SuperOscar Softwares, Tommi Nieminen 1993."
  200.     Say
  201.     Say "    [D:\] chname FILE ... [ /f /h /q /x /? ]"
  202.     Say
  203.     Say "Change long HPFS file names to FAT format or vice versa. When long"
  204.     Say "HPFS file names are truncated to FAT format, long name is saved to"
  205.     Say "`.LONGNAME'  extended  attribute,  and  the  resulting file can be"
  206.     Say "copied to a FAT drive with the standard OS/2 copying commands."
  207.     Say
  208.     Say "With the  /h  switch,  file names formerly truncated to FAT format"
  209.     Say "can be restored."
  210.     Say
  211.     Say "Switches:"
  212.     Say "    /f  HPFS to FAT (default)"
  213.     Say "    /h  FAT to HPFS (reversal of /f)"
  214.     Say "    /q  Quiet mode"
  215.     Say "    /x  Send output to OS2QUE instead of standard output"
  216.     Say "    /?  Show this help page and quit"
  217. Exit 0
  218.  
  219. Error: Procedure Expose prgname
  220.     Parse Arg errormsg
  221.  
  222.     Say prgname":" errormsg
  223. Exit 1
  224.