Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!cernvax!chx400!ugun2b!ugsc2a!fisher From: fisher@sc2a.unige.ch Newsgroups: comp.os.msdos.misc Subject: Re: Passing path as Batch parameter Message-ID: <1991May5.210006.417@sc2a.unige.ch> Date: 5 May 91 19:00:06 GMT References: <375@galileo.rtn.ca.boeing.com> Organization: University of Geneva, Switzerland Lines: 65 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