home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / sosutl11.zip / chname.cmd next >
OS/2 REXX Batch file  |  1993-08-21  |  6KB  |  184 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 ]
  10.  *
  11.  * Switches:
  12.  *      /f  HPFS to FAT (default)
  13.  *      /h  FAT to HPFS
  14.  *      /q  quiet mode
  15.  *
  16.  * 20-Aug-1993  v1.0: first PD version.
  17.  */
  18.  
  19. "@echo off"
  20.  
  21. Parse Arg files "/"switches
  22.  
  23. Signal On Halt Name Quit
  24.  
  25.   /* Program name and version */
  26. program_name = "ChName v1.0"
  27.  
  28.   /* Constants */
  29. TRUE = 1
  30. FALSE = 0
  31.  
  32.   /* Load SysFileTree(), SysPutEA(), and SysGetEA() */
  33. Call RxFuncAdd "SysFileTree", "RexxUtil", "SysFileTree"
  34. Call RxFuncAdd "SysPutEA", "RexxUtil", "SysPutEA"
  35. Call RxFuncAdd "SysGetEA", "RexxUtil", "SysGetEA"
  36.  
  37.   /* Defaults */
  38. hpfs2fat = TRUE
  39. quiet = FALSE
  40.  
  41.   /* Check switches */
  42. If switches <> "" Then Do
  43.     switches = Translate(switches, " ", "/")
  44.     Do i = 1 To Length(switches)
  45.         ch = Translate(SubStr(switches, i, 1))
  46.         Select
  47.             When ch == " " | ch == "/" Then Iterate
  48.             When ch == "F" Then Iterate
  49.             When ch == "H" Then hpfs2fat = FALSE
  50.             When ch == "Q" Then quiet = TRUE
  51.             When ch == "?" Then Call ShowHelp
  52.             Otherwise Call Error "invalid switch '"ch"' (use /? for help)"
  53.         End
  54.     End
  55. End
  56.  
  57.   /* If there are no file parameters, and we haven't yet encountered
  58.    * /? switch, an error situation results.
  59.    */
  60. If files == "" Then Call Error "no files specified"
  61.  
  62. Do i = 1 To Words(files)
  63.     mask = Word(files, i)
  64.     flist = mask
  65.  
  66.       /* Expand wildcards if such exist */
  67.     If Pos("?", mask) <> 0 | Pos("*", mask) <> 0 Then Do
  68.         flist = ""
  69.         res = SysFileTree(mask, fstem, "fo")
  70.         Do j = 1 To fstem.0
  71.               /* Spaces cause problems when handling the files, so we
  72.                * change them to bars, which is an invalid character in
  73.                * file names.
  74.                */
  75.             flist = flist || Translate(fstem.j, "|", " ") || " "
  76.         End
  77.     End
  78.  
  79.       /* Rename files */
  80.     Do j = 1 To Words(flist)
  81.           /* Separate one file from the list, and change bars back to
  82.            * spaces if this was done before.
  83.            */
  84.         file = Translate(Word(flist, j), " ", "|")
  85.  
  86.           /* Check whether file exists */
  87.         If Stream(file, "c", "query exists") == "" Then Do
  88.             Say program_name": File '"file"' not found"
  89.             Iterate
  90.         End
  91.  
  92.         If hpfs2fat == TRUE Then Do
  93.             newname = Hpfs2Fat(file)
  94.             If file <> newname Then Do
  95.                 Say file "->" newname
  96.                 'ren "'file'"' newname '>nul'
  97.                 res = SysPutEA(newname, ".LONGNAME", file)
  98.             End
  99.         End
  100.         Else Do
  101.             res = SysGetEA(file, ".LONGNAME", "newname")
  102.             If res == 0 & newname <> "" & file <> newname Then Do
  103.                 Say file "->" newname
  104.                 'ren' file '"'newname'" >nul'
  105.             End
  106.         End
  107.     End
  108.  
  109. End
  110.  
  111. Quit:
  112.     Exit 0
  113.  
  114.   /* Change HPFS names to FAT format */
  115. Hpfs2Fat: Procedure Expose program_name
  116.     Parse Arg pathname
  117.  
  118.     name = pathname
  119.     path = ""
  120.  
  121.     If Pos("\", name) > 0 Then Do
  122.         name = SubStr(name, LastPos("\", name) + 1)
  123.         path = Left(name, LastPos("\", name))
  124.     End
  125.  
  126.       /* Spaces to underlines */
  127.     name = Translate(name, "_", " ")
  128.  
  129.       /* Separate name and extension */
  130.     parts = Translate(name, " ", ".")
  131.     namepart = Word(parts, 1)
  132.     extpart = ""
  133.     If Words(parts) > 1 Then
  134.         extpart = "." || Word(parts, Words(parts))
  135.  
  136.       /* Does name already satisfy FAT file system? -- At most two dot-
  137.        * separated parts, length of name part 1..8 chars, length of
  138.        * extension part 1..3 chars plus the initial dot.
  139.        */
  140.     If Words(parts) > 2 | Length(namepart) > 8 | Length(extpart) > 4 Then Do
  141.         namepart = DelStr(namepart, 9)
  142.         extpart = DelStr(extpart, 5)
  143.         result = namepart || extpart
  144.  
  145.           /* Does 'result' exist already? */
  146.         If Stream(result, "c", "query exists") <> "" Then Do
  147.             len = 1
  148.             Do n = 0 While Stream(result, "c", "query exists") <> ""
  149.                 namepart = DelStr(namepart, 9 - len)
  150.                 result = namepart || Format(n, len) || extpart
  151.                 len = Length(((n + 1) * 10) % 10)
  152.             End
  153.         End
  154.     End
  155.     Else
  156.         result = name
  157.  
  158. Return Translate(result)
  159.  
  160. ShowHelp: Procedure Expose program_name
  161.     Say program_name "(C) SuperOscar Softwares, Tommi Nieminen 1993."
  162.     Say
  163.     Say "    [D:\] chname FILE ... [ /f /h /q ]"
  164.     Say
  165.     Say "Change long HPFS file names to FAT format or vice versa. When long"
  166.     Say "HPFS file names are truncated to FAT format, long name is saved to"
  167.     Say "`.LONGNAME'  extended  attribute,  and  the  resulting file can be"
  168.     Say "copied to a FAT drive with the standard OS/2 copying commands."
  169.     Say
  170.     Say "With the /h switch,  file names formerly truncated formerly to FAT"
  171.     Say "format can be restored."
  172.     Say
  173.     Say "Switches:"
  174.     Say "    /f  HPFS to FAT (default)"
  175.     Say "    /h  FAT to HPFS (reversal of /f)"
  176.     Say "    /q  Quiet mode"
  177. Exit 0
  178.  
  179. Error: Procedure Expose program_name
  180.     Parse Arg errormsg
  181.  
  182.     Say program_name":" errormsg
  183. Exit 1
  184.