home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / os / msdos / programm / 8664 < prev    next >
Encoding:
Internet Message Format  |  1992-08-20  |  2.3 KB

  1. Path: sparky!uunet!mcsun!uknet!axion!spuddy!sweh
  2. From: sweh@spuddy.uucp (Stephen Harris)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: A batch programming contest for you
  5. Message-ID: <1992Aug19.153902.6706@spuddy.uucp>
  6. Date: 19 Aug 92 15:39:02 GMT
  7. References: <1992Aug18.052017.3758@uwasa.fi>
  8. Organization: Spuddy's Public Usenet Domain
  9. Lines: 73
  10.  
  11. In article <1992Aug18.052017.3758@uwasa.fi> ts@uwasa.fi (Timo Salmi) writes:
  12. >I have a puzzle for you gentle fellow programmers who are interested
  13. >in batch programming. The task is the following.  With the standard
  14. >batch commands write a batch that returns the extension of the
  15. >(file) name given to the batch as the parameter (%1). No external
  16. >commands or programs are allowed to perform the task.
  17.  
  18. >Please post your potential suggestions and solutions in here for
  19. >everyone to see. 
  20.  
  21. Well, here goes!
  22.  
  23. Here is an entry for your "what is the extension" BAT file competition.
  24. It uses variables a,x,y.
  25.  
  26.   @echo off
  27.   set x=%1
  28.   if "%x%" == "" goto noext
  29.   :loop
  30.   for %%a in (/%x%) do set y=%%a
  31.   if "%x%" == "%y%" goto noext
  32.   if "%x%" == ".%y%" goto gotit
  33.   set x=%y%
  34.   goto loop
  35.   :noext
  36.   echo No extension specified
  37.   goto end
  38.   :gotit
  39.   echo The extension is %y%
  40.   :end
  41.  
  42. Total is 15 lines.  It has been tested under DOS 5.00, and Compaq DOS 3.31,
  43. but it does not work with 4DOS.
  44.  
  45. It works by using an undocumented (I think!) feature of the for loop as
  46. demonstrated by:
  47.   C> type x.bat
  48.   @echo off
  49.   for %%a in (/hello) do echo %%a
  50.  
  51.   C> x
  52.   h
  53.   ello
  54.  
  55. Basically, preceeding the (string) with a / (/string) makes 'for' take the
  56. first letter as one pass, and the rest as the second pass.  4DOS does not
  57. understand this, and just prints /hello.  In 4DOS I'd just use %@ext[%&]
  58. anyway :-)
  59.  
  60. And so, the program loops until the first character is a . in which case
  61. the rest is an extension.
  62. If there is only one character then there can not be an extension.
  63.  
  64.   C> ext test.me
  65.   The extension is me
  66.  
  67.   C> ext test.
  68.   No extension specified
  69.  
  70.   C> ext test
  71.   No extension specified
  72.  
  73.   C> ext a.b.c.d.e
  74.   The extension is b.c.d.e
  75.  
  76. Of course it needs enough environment space to run in :-)
  77.  
  78. PS: Timo, please don't moan at the .UUCP address :-)
  79. -- 
  80.                                Stephen Harris
  81.                sweh@spuddy.uucp    ...!uknet!axion!spuddy!sweh
  82.  
  83.  * Meeeeow ! Call Spud the Cat on > +44 203 638780 < for free Usenet access *
  84.