[comp.os.msdos.misc] Passing path as Batch parameter

rfh3273@galileo.rtn.ca.boeing.com (Dick Harrigill) (05/04/91)

I have a generic problem with writing batch files.
Can anyone provide a simple solution?

PROBLEM:
  To be able to pass a path/directory to a batch file as a parameter, 
  and be able to act upon a file within that directory.

FOR EXAMPLE:
  1. doit c:
  2. doit a:\mydir
  3. doit c:\mydir\

 where file doit.bat references a file using %1 as the path.

 Within doit.bat:

  %1FILENAME will work with example 1 & 3 but not 2.

  %1\FILENAME will work with example 2 but not 1 (as it will
              incorrectly point to the root) and not 3
              (as you will get a double \).

How do you write a generic batch file that will work with all cases above?

-- 
Dick Harrigill, an independent voice from:     Boeing Commercial Airplanes 
M/S 9R-49  PO BOX 3707                       Renton Avionics/Flight Systems
Seattle, WA  91824                                  Computing Support
(206) 393-9539     rfh3273@galileo.rtn.ca.boeing.com

ts@uwasa.fi (Timo Salmi) (05/04/91)

In article <375@galileo.rtn.ca.boeing.com> rfh3273@galileo.rtn.ca.boeing.com (Dick Harrigill) writes:
>PROBLEM:
>  To be able to pass a path/directory to a batch file as a parameter, 
>  and be able to act upon a file within that directory.
:

Test for the existence of the file in each of the relevant
directories and set an enrivroment variable to reflect which path is
true.  Then branch with a goto depending on the result of the test. 
There are probably neater solutions, but this is one.

fisher@sc2a.unige.ch (05/06/91)

In article <375@galileo.rtn.ca.boeing.com>, rfh3273@galileo.rtn.ca.boeing.com
(Dick Harrigill) writes:
> I have a generic problem with writing batch files.
> Can anyone provide a simple solution?
> 
> PROBLEM:
>   To be able to pass a path/directory to a batch file as a parameter, 
>   and be able to act upon a file within that directory.
> 
> FOR EXAMPLE:
>   1. doit c:
>   2. doit a:\mydir
>   3. doit c:\mydir\
    4. doit .
    5. doit ..

  SOLUTION:
    Use a little known trick to check the existence of a directory and
    a bit of fall-through logic.

  THE TRICK:
    In DOS the file NUL "exists" in every directory on any drive (even those
    beyond the LastDrive parameter or not yet SUBST'ed drives).

    How many installation batch files have you seen that ask you to create
    the target directory before running it?

  BATCH FILE: (Public Domain :-)
    @echo off

    rem    Find out if %1 is a valid directory, store the result in '_vd'
    rem    using the "directory\NUL" test.
    rem                                    (Markus G. Fischer, Geneva CH, 1991)

    if no%1_arg == no_arg goto no_arg

    set _vd=
    if exist %1NUL if exist %1\NUL set _vd=%1
    if no%_vd% == no if exist %1\NUL set _vd=%1\
    if no%_vd% == no if exist %1.\NUL set _vd=%1
    if no%_vd% == no goto no_vd

    echo %0: working on "%_vd%filename.ext"
    set _vd=
    goto end

    :no_arg
    echo usage: %0 directory
    echo        checks if "directory" exists
    goto end

    :no_vd
    echo %0 error: directory %1 not found!

    :end
    echo.

  BUGS:
    There must be enough environment space to store the variable.



Hope this helps!

Markus G. Fischer, Dept of Anthropology, Geneva CH

userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) (05/09/91)

In article <1991May5.210006.417@sc2a.unige.ch>, fisher@sc2a.unige.ch writes:
>In article <375@galileo.rtn.ca.boeing.com>, rfh3273@galileo.rtn.ca.boeing.com
>(Dick Harrigill) writes:
>> I have a generic problem with writing batch files.
 
<<<deletions: wants "if exist directoryname" capability>>>
   
> 
>  SOLUTION:
>    Use a little known trick to check the existence of a directory and
>    a bit of fall-through logic.
> 
 
<<< deletions: explanation that NUL exists in all directories>>>
<<< code fragment follows: >>>
> 
>    set _vd=
>    if exist %1NUL if exist %1\NUL set _vd=%1
>    if no%_vd% == no if exist %1\NUL set _vd=%1\
>    if no%_vd% == no if exist %1.\NUL set _vd=%1
>    if no%_vd% == no goto no_vd
 
A neat trick, but not foolproof. It handles even the
oddball "..".."..".."..", but catches you when you try
to back up past the root directory. Where it does fail
is with a directory name such as ".."""""", which it
heralds as an existing directory, even though CD and DIR
object. You could go a step further and check for a file
such as direxist in the tentative directory. If it exists,
the directory must exist; if not, create it and try again.
If it then exists the directory must exist; if not the
directory either doesn't exist or the disk is full.
 
Oh, well, .BAT files aren't quite DCL!
 
 -------------------+-------------------------------------------
 Al Dunbar          | 
 Edmonton, Alberta  |  Disclaimer: "I disclaim disclaimers"
 CANADA             |                              
 -------------------+-------------------------------------------

userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) (05/10/91)

In article <RN.1208@mts.ucs.UAlberta.CA>, I wrote:
>In article <1991May5.210006.417@sc2a.unige.ch>, fisher@sc2a.unige.ch writes:
>>In article <375@galileo.rtn.ca.boeing.com>, rfh3273@galileo.rtn.ca.boeing.com
>>(Dick Harrigill) writes:
>>> I have a generic problem with writing batch files.
> 
><<<deletions: wants "if exist directoryname" capability>>>
> 
>> 
>>  SOLUTION:
>>    Use a little known trick to check the existence of a directory and
>>    a bit of fall-through logic.
>> 
> 
><<< deletions: explanation that NUL exists in all directories>>>
><<< code fragment follows: >>>
>> 
>>    set _vd=
>>    if exist %1NUL if exist %1\NUL set _vd=%1
>>    if no%_vd% == no if exist %1\NUL set _vd=%1\
>>    if no%_vd% == no if exist %1.\NUL set _vd=%1
>>    if no%_vd% == no goto no_vd
> 
>A neat trick, but not foolproof. It handles even the
<<<deleted explanation that ".."""""" appears to be valid>>>
 
Here is another way it fails: If you pass the name "BILLY"
where no such directory exists, but a file called BILLY.NUL
does exist, the above code would declare "BILLY" as a valid
directory. The solution to this glitch is to look for
NUL.XXX (which has the same properties of existence as NUL),
because BILLY.NUL.XXX can't exist to throw the code off the
track. Note also, that other device names (CON, PRN) seem
to work in place of NUL, but using NUL makes more sense.
 
 -------------------+-------------------------------------------
 Al Dunbar          | 
 Edmonton, Alberta  |  Disclaimer: "I disclaim disclaimers"
 CANADA             |                              
 -------------------+-------------------------------------------