Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is an argument a non-relative reference to a file or directory ?

27 views
Skip to first unread message

dr.s.l...@gmail.com

unread,
Dec 18, 2016, 7:30:17 AM12/18/16
to
I have a batch file in which the first argument is required to be either
absent or a _non-relative_ reference to a directory, as it will be used
by a called unalterable EXE that dislikes non-relative arguments.

So I wish to test whether the argument is non-relative, and GOTO
accordingly - or auto-convert it to a relative equivalent.

(I wish also to test whether it is an existing directory; but I
can do that already.)

I can, if necessary, echo either an empty string '' or a chosen string
such as ':::' to indicate whether or not the argument starts with
something like 'C:' or '\' ('..\' is OK) and I would then need to use
that indication to steer a GOTO command.

I have no need to consider a full UNC reference, since I know that the
author of the EXE did not do so.

An error in the result will lead to annoyance or confusion, but not
to disaster.

Any suggestions?

--
SL

JJ

unread,
Dec 20, 2016, 10:23:54 AM12/20/16
to
Not fully tested, but I think below code can do it.

@echo off
if not "%~1" == "" (
if /i "%~1" equ "%~f1" (
echo absolute path is not acceptable
goto :eof
)
echo relative path is specified: %1
if not exist "%~1\nul" (
echo path is not found
goto :eof
)
) else (
echo no path is specified
)

dr.s.l...@gmail.com

unread,
Dec 30, 2016, 5:05:03 PM12/30/16
to
On Tuesday, 20 December 2016 15:23:54 UTC, JJ wrote:
Sorry for the delay in responding - caused by a combination of a vicious seasonal ailment affecting self, family, & neighbours, with Christmas, with next door's hob blowing itself twice, on the first occasion taking out its own circuit's 30A fuse and on the second taking out instead the "company fuse" down below so killing next door's complete electricity supply - and fighting off attempts to bill *me* for the professional replacement of the latter fuse.

I no longer think that the unalterable EXE necessarily dislikes only non-relative arguments, so I've gone for a more rudimentary approach of setting a "bad" marker for any known-bad type of first argument and for any others that become known-bad (H'mmm ... test: like leading double-dot). I currently have the following, which does what I at present want:


REM argument 1 gives directory to scan, else use current directory

set dir=%1
if "%dir%"=="" set dir=.
set dir=%dir%\
echo Argument 1 is '%1' ; %%dir%% is '%dir%'

if not exist %dir%NUL (
echo %1 is not a directory; Quit.
goto QUIT )

set NoCh=0
if "%dir:~0,1%"=="\" set NoCh=slash& rem as in \pc
if "%dir:~1,1%"==":" set NoCh=colon& rem as in c:\pc
if "%dir:~0,2%"==".." set NoCh=dotty& rem as in ..\pc
REM echo Variable 'NoCh' now has value '%NoCh%'.
if not %NoCh%==0 echo '%1' is unsuitable ; so, XXX.EXE not used.

Thanks for your interest; I must understand "~" better.

--
SL

dr.s.l...@gmail.com

unread,
Jan 10, 2017, 6:05:15 PM1/10/17
to
On Friday, 30 December 2016 22:05:03 UTC, dr.s.l...@gmail.com wrote:
> On Tuesday, 20 December 2016 15:23:54 UTC, JJ wrote:
> > On Sun, 18 Dec 2016 04:30:16 -0800 (PST), SL wrote:
> > > I have a batch file in which the first argument is required to be either
> > > absent or a _non-relative_ reference to a directory, as it will be used
> > > by a called unalterable EXE that dislikes non-relative arguments.
> > > ...

> I no longer think that the unalterable EXE necessarily dislikes only non-
> relative arguments, so I've gone for a more rudimentary approach of setting a
> "bad" marker for any known-bad type of first argument and for any others that
> become known-bad (H'mmm ... test: like leading double-dot). I currently have
> the following, which does what I at present want:

Now simplified after the realisation that if I do PUSHD %dir% as soon as %dir%
is known, and POPD at the end, then all subsequent references to %dir% are
unwanted and the unalterable EXE will be working in its originally-intended
conditions.

--
SL

0 new messages