🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: A batch programming contest for you

#986
From: Mike Levis
Date: Wed, 19 Aug 1992 07:24
75 lines
3155 bytes
In article <1992Aug19.045036.5132@uwasa.fi> ts@uwasa.fi (Timo Salmi) writes:
>In article <1992Aug18.233207.1159@cs.ubc.ca> yogi@cs.ubc.ca (Yossi Gil) writes:
>>I am away from my DOS machine now, but I think that the above ideas
>>will work after some polishing. 
>>
>>	erase echo.*
>>	copy %1 echo.*
>>	for %%f in (echo.*) do %%f > ext 
>>
>>The extension of %1 should be in the file ext now.
>>
>>Joseph (Yossi) Gil                 {alberta,uw-beaver,uunet}!ubc-cs!yogi
>
>@echo off
>rem That's a very inventive trick, Yossi.  I expanded a bit. The snag is
>rem the the file must exist in order to get the extension. Another
>rem question is how do you get the extension name into an environment
>rem variable so that we can utilize it.
>
>if not exist %1 goto _out
>if exist echo.* erase echo.*
>copy %1 echo.* > nul
>for %%f in (echo.*) do %%f > ext
>type ext
>if exist ext erase ext
>:_out

I modified this.  The following batch file will work somewhat.
It is not efficient (it copies the file that you want
to find the extension of), and it does not do much
error checking (it fails if the filename has no extension),
but it works if the filename either has an extension or
the filename is not given.

It creates a variable xyz to have the filename extension.
It creates (and later deletes) a temporary batch file
called tmp.bat .  Essentially, tmp.bat is a clone of the
original batch file below, but it also has the filename
extension as part as the batch file code.

@echo off
set xyz=
if %1.==. goto end_batch
del echo.*
copy %1 echo.* > nul
for %%f in (echo.*) do %%f > tmp.dat
if exist %0 copy %0+tmp.dat tmp.bat > nul
if not exist %0 copy %0.bat+tmp.dat tmp.bat > nul
call tmp.bat
del tmp.bat
:end_batch
if %xyz%.==. set xyz=^Z

Important!!!  The ^Z means Control-Z (End of file).  It
must be on the same line as the last command, not on the
line following it.  Since ^Z is on the same line as the
last command, we can append the filename extension to
a copy of the batch file and run the copy to set the
variable.

Even though this code works somewhat, I'm sure there is a
much better way of doing it.

>..................................................................
>Prof. Timo Salmi
>Moderating at garbo.uwasa.fi anonymous FTP archives 128.214.87.1
>Faculty of Accounting & Industrial Management; University of Vaasa
>Internet: ts@uwasa.fi Bitnet: salmi@finfun   ; SF-65101, Finland


-- 
==== Mike Levis   mlevis@ringer.cs.utsa.edu ====
::   ftp ftp.cs.widener.edu (147.31.254.132)  ::  .--.
::   for ``The Simpsons'' information.  Get   :: (OS/2)
:::: ``Simpsons'' FAQL in dir /pub/simpsons ::::  ~--~
Technical Details
Message-ID: <1992Aug19.072440.21996@ringer.cs.utsa.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!wupost!sdd.hp.com!swrinde!ringer!mlevis
References: <1992Aug18.052017.3758@uwasa.fi> <1992Aug18.233207.1159@cs.ubc.ca> <1992Aug19.045036.5132@uwasa.fi>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!wupost!sdd.hp.com!swrinde!ringer!mlevis
From: mlevis@ringer.cs.utsa.edu (Mike Levis)
Newsgroups: comp.os.msdos.programmer
Subject: Re: A batch programming contest for you
Message-ID: <1992Aug19.072440.21996@ringer.cs.utsa.edu>
Date: 19 Aug 92 07:24:40 GMT
References: <1992Aug18.052017.3758@uwasa.fi> <1992Aug18.233207.1159@cs.ubc.ca> <1992Aug19.045036.5132@uwasa.fi>
Organization: University of Texas at San Antonio
Lines: 75