🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #1074

Re: A batch programming contest for you

#1074
From: Stephen Harris
Date: Wed, 19 Aug 1992 15:39
73 lines
2467 bytes
In article <1992Aug18.052017.3758@uwasa.fi> ts@uwasa.fi (Timo Salmi) writes:
>I have a puzzle for you gentle fellow programmers who are interested
>in batch programming. The task is the following.  With the standard
>batch commands write a batch that returns the extension of the
>(file) name given to the batch as the parameter (%1). No external
>commands or programs are allowed to perform the task.

>Please post your potential suggestions and solutions in here for
>everyone to see. 

Well, here goes!

Here is an entry for your "what is the extension" BAT file competition.
It uses variables a,x,y.

  @echo off
  set x=%1
  if "%x%" == "" goto noext
  :loop
  for %%a in (/%x%) do set y=%%a
  if "%x%" == "%y%" goto noext
  if "%x%" == ".%y%" goto gotit
  set x=%y%
  goto loop
  :noext
  echo No extension specified
  goto end
  :gotit
  echo The extension is %y%
  :end

Total is 15 lines.  It has been tested under DOS 5.00, and Compaq DOS 3.31,
but it does not work with 4DOS.

It works by using an undocumented (I think!) feature of the for loop as
demonstrated by:
  C> type x.bat
  @echo off
  for %%a in (/hello) do echo %%a

  C> x
  h
  ello

Basically, preceeding the (string) with a / (/string) makes 'for' take the
first letter as one pass, and the rest as the second pass.  4DOS does not
understand this, and just prints /hello.  In 4DOS I'd just use %@ext[%&]
anyway :-)

And so, the program loops until the first character is a . in which case
the rest is an extension.
If there is only one character then there can not be an extension.

  C> ext test.me
  The extension is me

  C> ext test.
  No extension specified

  C> ext test
  No extension specified

  C> ext a.b.c.d.e
  The extension is b.c.d.e

Of course it needs enough environment space to run in :-)

PS: Timo, please don't moan at the .UUCP address :-)
-- 
                               Stephen Harris
               sweh@spuddy.uucp    ...!uknet!axion!spuddy!sweh

 * Meeeeow ! Call Spud the Cat on > +44 203 638780 < for free Usenet access *
Technical Details
Message-ID: <1992Aug19.153902.6706@spuddy.uucp>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!axion!spuddy!sweh
References: <1992Aug18.052017.3758@uwasa.fi>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!axion!spuddy!sweh
From: sweh@spuddy.uucp (Stephen Harris)
Newsgroups: comp.os.msdos.programmer
Subject: Re: A batch programming contest for you
Message-ID: <1992Aug19.153902.6706@spuddy.uucp>
Date: 19 Aug 92 15:39:02 GMT
References: <1992Aug18.052017.3758@uwasa.fi>
Organization: Spuddy's Public Usenet Domain
Lines: 73