home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zippy.zip / zippy.cmd < prev   
OS/2 REXX Batch file  |  1997-07-12  |  11KB  |  348 lines

  1. /********************************************************/
  2. /* zippy.cmd                                           */
  3. /* Created in 1997                                      */
  4. /* Author: Daniel Szmulewicz                            */
  5. /* Purpose: Zip up whole directory trees, useful as a   */ 
  6. /*          general purpose back-up tool.               */
  7. /********************************************************/
  8. signal on halt
  9. begin = setlocal()
  10. env = "OS2ENVIRONMENT"
  11. if Rxfuncquery('SysLoadFuncs') \= 0 then
  12.    rc = RxFuncAdd('SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs')
  13. call SysLoadFuncs
  14. Parse Source . . ThisProc
  15. ThisDrive = filespec('D',ThisProc)
  16. ThisDir  = filespec('P',ThisProc)
  17. ThisProc = filespec('N',ThisProc)
  18.  
  19. /***general stuff***/
  20. LowerCase = xrange('a','z')
  21. UpperCase = xrange('A','Z')
  22.  
  23. /***color****/
  24. '@ansi on 1>nul 2>&1'
  25. redonblue = '1B'x || '[37;44m'
  26. othergreen = '1B'x || '[46;43m'
  27. whiteongreen = '1B'x || '[37;46m'
  28. normal = '1B'x || '[0m'
  29. bright = '1B'x || '[1m'
  30.  
  31. /*********Check that paths on the command line are valid, and gather info*************/
  32.  
  33. arg target source 
  34.  
  35. i = words(source)
  36. source.0 = i 
  37. if source = '' | target = '' | pos('\', source) = 0 | pos('\', target) = 0 | pos(':', target) = 0 | pos(':', source) = 0 then call help
  38. do i = 1 to source.0
  39.   source.i = strip(word(source, i))
  40.   if length(source.i) > 3 & substr(source.i, length(source.i), 1) = '\' then source.i = left(source.i, length(source.i) -1)
  41.   else if length(source.i) < 3 then call help  
  42.     else nop 
  43.   if pos('*', source.i) > 0 then do
  44.       source.i.wildcard = substr(source.i, lastpos('\', source.i) + 1)
  45.       source.i = left(source.i, lastpos('\', source.i) -1 )
  46.     end
  47.   else source.i.wildcard = 'X'
  48.   rc = sysfiletree(source.i, file, DO)
  49.   if file.0 = 0 then do 
  50.     say 'can''t find 'source.i
  51.     exit
  52.   end
  53. end
  54.  
  55. drop file.
  56. target = strip(target)
  57.  
  58.  
  59. rc = sysfiletree(target, file, DO)
  60. if file.0 = 0 then do 
  61.     say 'can''t find 'target
  62.     exit
  63.   end
  64.  
  65. targetdrive = filespec('D', target)
  66. targetpath = right(target, length(target) - (pos(':', target) + 1))
  67. targetroot = right(target, length(target) - lastpos("\", target)) 
  68.  
  69. do p= 1 to source.0
  70.   if length(source.p) > 3 then do 
  71.     sourcedrive.p = filespec('D', source)
  72.     sourcepath.p = right(source.p, length(source.p) - (pos(':', source.p) + 1))
  73.     sourceroot.p = right(source.p, length(source.p) - lastpos("\", source.p))  
  74.   end
  75.   else sourceroot.p = left(source.p, 1) || '__'
  76. end
  77.  
  78. do i= 1 to source.0
  79.   say 'source: 'source.i
  80. end
  81.  
  82. say 'target: 'target
  83. cur = directory()
  84.  
  85.  
  86. if length(target) > 3 & substr(target, length(target), 1) <> '\' then target = target || '\' /*left(target, length(target) -1)*/
  87. if stream(target || 'zippy.log', 'c', 'query exists') <> '' then rc = SysFileDelete(target || 'zippy.log')
  88.  
  89. logfile = target || 'zippy.log' 
  90.  
  91. do i = 1 to source.0
  92.   if length(source.i) = 3 then source.i = left(source.i, 2)
  93.   if source.i.wildcard = 'X' then do
  94.         rc = Sysfiletree(source.i || '\*.*', source.i_files_in_root, 'F')
  95.         rc = Sysfiletree(source.i || '\*.*', source.i_dir_in_root, 'D')
  96.         say source.i_files_in_root.0' files in root of' source.i'.'  
  97.         say source.i_dir_in_root.0' directories beneath' source.i'.'  
  98.       end
  99.   else do
  100.         rc = Sysfiletree(source.i || '\' || source.i.wildcard, source.i.matchedfiles_in_root, 'F')
  101.         rc = Sysfiletree(source.i || '\' || source.i.wildcard, source.i.matcheddirs_in_root, 'D')
  102.         say source.i.matchedfiles_in_root.0' files in root of' source.i ' match the wildcard.'  
  103.         say source.i.matcheddirs_in_root.0' directories beneath' source.i ' match the wildcard.'  
  104.       end
  105.   say
  106.   if length(source.i) = 2 then source.i = source.i || '\'
  107. end
  108.  
  109. /***search zip in current directory***/
  110.  
  111. /*    rc = Sysfiletree(cur || '\zip.exe', zipfile, 'F') 
  112.       rc = Sysfiletree(cur || '\unzip.exe', unzipfile, 'F') 
  113.       rc = Sysfiletree(cur || '\unzipsfx.exe', stubfile, 'F') 
  114.       if zipfile.0 >= 1 & unzipfile.0 >= 1 & unzipsfx.0 >= 1 then 
  115.         do
  116.           say 'ok, zip executables located!'
  117.           say
  118.         end 
  119.       else
  120. */
  121.  
  122. /*****search zip executables in path, otherwise exit***********/
  123.  
  124. zipflag = 0
  125. stubflag = 0
  126. unzipflag = 0
  127. path = value('PATH',,env) 
  128. if right(path,1) <> ';' then path = insert(';', path, length(path))
  129. do while path <> ''
  130.   pathone = left(path, pos(';',path) -1) 
  131.   path = right(path, length(path) - pos(';',path))
  132.   rc = Sysfiletree(pathone||'\zip.exe',zipfile,'F')
  133.   rc = Sysfiletree(pathone||'\unzipsfx.exe',stubfile,'F')
  134.   rc = Sysfiletree(pathone||'\unzip.exe',unzipfile,'F')
  135.   if zipfile.0 > 0 then 
  136.     do; call charout, 'locating zip.exe...' 
  137.        zipflag = zipflag + 1 
  138.     end
  139.   if stubfile.0 > 0 then 
  140.     do;  call charout, 'unzipsfx.exe...' 
  141.         stubflag = stubflag + 1
  142.     end 
  143.   if unzipfile.0 > 0 then 
  144.     do; call charout, 'unzip.exe...' 
  145.         unzipflag = unzipflag + 1
  146.     end 
  147.   if stubflag >= 1 & zipflag >= 1 & unzipflag >= 1 then 
  148.     do
  149.       say 'ok, zip executables found!'
  150.       say
  151.       leave
  152.     end
  153. end
  154.  
  155.   if \(stubflag >= 1) | \(zipflag >= 1) | \(unzipflag >= 1) then 
  156.     do
  157.       say '...zip executables missing!'
  158.       if \(zipflag >= 1) then say 'zip.exe is not in your path'
  159.       if \(unzipflag >= 1) then say 'unzip.exe is not in your path'
  160.       if \(stubflag >= 1) then say 'unzipsfx.exe is not in your path'
  161.       exit
  162.     end
  163.  
  164. /**********Main program***********/
  165.  
  166. do n = 1 to source.0
  167.   new = directory(source.n)
  168.   Select 
  169.     when source.n.wildcard = 'X' then do
  170.         call Ziponefile
  171.       end
  172.     Otherwise do
  173.         if source.n.matcheddirs_in_root.0 <> 0 then call Zipmanyfiles
  174.         if source.n.matchedfiles_in_root.0 <> 0 then call ZipRoot
  175.      end
  176.   End
  177. end
  178.  
  179. say
  180. say bright||'...all done'||normal
  181. exit
  182.  
  183.  
  184. Ziponefile:  
  185. if length(source.n) = 3 then zipfile = 'drive' || left(source.n, 1)
  186. else do
  187.     zipfile = Reverse(source.n)
  188.     parse var zipfile zipfile '\' .
  189.     zipfile = translate(zipfile, lowercase, uppercase)
  190.     zipfile = Reverse(zipfile)
  191.     zipfile = stripblanks(zipfile)
  192.     zipfile = stripdots(zipfile)
  193.     if length(zipfile) > 8 then do
  194.         zipfile = left(zipfile, 8)
  195.       end  
  196. end
  197. call zippy zipfile, 'one'
  198. return 
  199.  
  200. Zipmanyfiles:
  201. flagthis = 0
  202. if source.n.wildcard = 'X' then rc = sysfiletree(source.n'\*.*',
  203.      'directories', 'DO')
  204. else if length(source.n) > 3 then rc = sysfiletree(source.n || '\' || source.n.wildcard, 'directories', 'DO')
  205.         else rc = sysfiletree(source.n || source.n.wildcard, 'directories', 'DO')
  206. do p=1 to directories.0
  207.   zipfile.p = directories.p
  208.   zipfile.p = Reverse(zipfile.p)
  209.   parse var zipfile.p zipfile.p '\' .
  210.   zipfile = translate(zipfile, lowercase, uppercase)
  211.   zipfile.p = Reverse(zipfile.p)
  212.   zipfile.p = stripblanks(zipfile.p)
  213.   zipfile.p = stripdots(zipfile.p)
  214.   if length(zipfile.p) > 8 then
  215.         do
  216.           zipfiletemp = zipfile.p
  217.           flagthis = flagthis + 1 
  218.           do b=1 to (p-1)
  219.              if left(zipfiletemp, 8) = left(zipfile.p, 8) then 
  220.                 zipfile.p = left(zipfiletemp, 6)  || flagthis
  221.              else 
  222.                zipfile.p = left(zipfiletemp, 8)
  223.           end
  224.        end
  225.   call zippy zipfile.p, 'many'
  226. end
  227. return
  228.  
  229. ZipRoot:
  230. parse value left(sourceroot.n, 3) with root
  231. root = translate(root, lowercase, uppercase)
  232. if souceroot.n > 3 then root = root || '_' || 'root'
  233. else root = 'root'
  234. if source.n.wildcard = 'X' then wildcard = '*.*'
  235. else wildcards = source.n.wildcard
  236. call zippy root, 'root', wildcards
  237. return
  238.  
  239. zippy:
  240. zipfile = arg(1)
  241. flag = arg(2)
  242. wildcards = arg(3)
  243. say 'zipping' zipfile || '.zip on' left(target, length(target) - 1)
  244. if flag = 'one' then '@ZIP -r -q 'target || zipfile ||'.zip *.*'     
  245. if flag = 'many' then '@ZIP -r -q 'target || zipfile ||'.zip' '"' || right(directories.p, length(directories.p) - lastpos("\", directories.p)) || '"'     
  246. if flag = 'root' then '@Zip -q -j 'target || zipfile || '.zip' wildcards     
  247. if rc <> 0 & rc <> 18 then call error
  248. else do
  249.     say 'appending self-executable stub to' zipfile || '.zip'
  250.     '@copy /b 'pathone || '\UNZIPSFX.EXE + 'target || zipfile||'.zip' target || zipfile||'.exe 1>nul'
  251.     say 'adjusting headers for' zipfile || '.exe'
  252.     '@ZIP -A 'target || zipfile||'.exe 1>nul 2>&1'
  253.     if rc <> 0 then call error
  254.     say 'testing integrity of 'zipfile || '.exe'
  255.     '@ZIP -T 'target || zipfile||'.exe >>'target || 'zippy.log 2>&1'
  256.     if rc <> 0 then call error
  257.   end
  258. if rc <> 12 then rc = Sysfiledelete(target || zipfile||'.zip')
  259. if rc <> 0 then say 'error: could not delete 'zipfile
  260. say
  261. return
  262.  
  263. Stripblanks:
  264. zipfile = arg(1)
  265. do forever 
  266.   if pos(" ", zipfile) > 0 then 
  267.     do
  268.       zipfile = overlay('_', zipfile, pos(" ", zipfile))
  269.       call Stripblanks(zipfile)
  270.     end
  271.   else leave
  272. end
  273. return zipfile
  274.  
  275. Stripdots:
  276. zipfile = arg(1)
  277. do forever 
  278.   if pos(".", zipfile) > 0 then 
  279.     do
  280.       zipfile = overlay('_', zipfile, pos(".", zipfile))
  281.       call Stripblanks(zipfile)
  282.     end
  283.   else leave
  284. end
  285. return zipfile
  286.  
  287. help:
  288. say whiteongreen'     Usage:'normal
  289. say ''
  290. say '     zippy <target> <src1> <src2> <src3> ... '
  291. say ''
  292. say whiteongreen'     Where:'normal
  293. say '       '
  294. say '     <src(n)> and <target> are expressed in absolute paths.'
  295. say '     '
  296. say '     'bright||'Note:'||normal' zip files are automatically converted to self-extractable (*.exe).' 
  297. say
  298. say whiteongreen'     Example:'normal
  299. say ''
  300. say '         zippy d:\archives c:\os2 d:\programs\* e:\data\misc\t*.r* '
  301. say '           '
  302. say whiteongreen'     Meaning:'normal
  303. say '       Create the following files on d:\archives:'
  304. say '         -os2.exe: complete directory tree beneath c:\os2'
  305. say '         -subdirectories of d:\programs as separate zipfiles'
  306. say '         -subdirectories of e:\data\misc that match' 
  307. say '          the wildcard ''t*.r*'', as separate zipfiles'
  308. exit
  309.  
  310. /*Installcmd:
  311. if stream(zipplace'install.cmd', C, query exists) <> '' then
  312.         do
  313.         say 'Replace existing install.cmd?'
  314.         pull answer
  315.         if abbrev(answer, 'Y') then
  316.            call sysfiledelete zipplace'install.cmd'
  317.         else return
  318.         end
  319. file = zipplace'install.cmd'
  320. do j=1 to file.0
  321.         call lineout file, rname.j' -o'
  322. end /* do */
  323. call lineout file
  324. return
  325. */
  326. error:
  327. Select 
  328.   when rc = 9 then do
  329.     say "interrupted by user... zippy terminated..."
  330.     call lineout logfile,  "interrupted by user... zippy terminated..."
  331.     exit
  332.   end
  333.   when rc = 12 then do
  334.       call lineout logfile, zipfile 'deleted'
  335.       return   
  336.     end
  337.   otherwise do 
  338.     say 'zip error, rc =' rc
  339.     say 'zippy got error from zip.exe, archives won''t be complete...'
  340.     call lineout logfile,  rc 'rc error: check archive'
  341.     return
  342.   end
  343. end
  344.  
  345. halt:
  346. say 'interrupted by user... exiting'
  347. exit
  348.