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

reparse points and "if exist file\" bug

19 views
Skip to first unread message

Christoph Mueller

unread,
Dec 12, 2006, 2:10:26 PM12/12/06
to
hi
just a short info about some weird behaviour, since I have spend some
time traking down this bug and did not find anything about it online.
Apologies if this has been posted before.

To detect whether an object is a file or a directory/folder I used
if exist "%~1\" echo DIR
This should echo DIR only if the object is a directory

however, if the path to the file contains a reparse point/junction,
this test will always return DIR, even if the object is a file:

if exist "dir1\junction\dir2\file.txt\" echo DIR
will always echo DIR

This can be quite confusing if you do not not know that there is a
reparse point in the path.

What seems to work fine is the alternative method
dir /AD %1 | find "<DIR>" > nul
if not errorlevel 1 echo DIR
however this can be slow.

Are there any other such traps with reparse points?

Timo Salmi

unread,
Dec 13, 2006, 9:10:46 PM12/13/06
to
Christoph Mueller <> wrote:
> To detect whether an object is a file or a directory/folder I used
> if exist "%~1\" echo DIR
> This should echo DIR only if the object is a directory

As has been discussed fairly extensively quite some time ago, it is not
that simple
75} How do I detect if an object is a file or a folder?
179284 Oct 21 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Christoph Mueller

unread,
Dec 13, 2006, 11:00:07 PM12/13/06
to
Timo Salmi wrote:
> As has been discussed fairly extensively quite some time ago, it is not
> that simple
> 75} How do I detect if an object is a file or a folder?
> 179284 Oct 21 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
> tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

I am well aware of that resource, it is quite useful, thanks
In fact this is where I got the way I am currently using to detect
directories, namely
dir %1 | find "<DIR>" > nul
if errorlevel 1 ...

I modified the first line to
2>nul dir /AD %1 | find "<DIR>" > nul
in hopes of speeding it up a bit for directories with lots of entries.

But the tips also still contain the solution
if exist "%~1\" (...
which apparently works fairly well as long as the path does not contain
any reparse points, but fails silently if it does.
I found no mention of that in previous discussions about the topic.

So I would suggest adding a warning to that tip.

Timo Salmi

unread,
Dec 14, 2006, 1:28:57 AM12/14/06
to
Christoph Mueller <net...@gmx.net> wrote:
> Timo Salmi wrote:
>> As has been discussed fairly extensively quite some time ago, it is not
>> that simple
>> 75} How do I detect if an object is a file or a folder?

> But the tips also still contain the solution


> if exist "%~1\" (...
> which apparently works fairly well as long as the path does not contain
> any reparse points, but fails silently if it does.
> I found no mention of that in previous discussions about the topic.
>
> So I would suggest adding a warning to that tip.

Thanks Christoph. I'll add a Message-Id pointer to your contribution.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

0 new messages