"if exist Z:\xfile\NUL" returns true for file xfile if Z: is mapped to a network drive

212 views Asked by At

I have Z: substituted to a network drive, eg:

subst Z: \\fc\c

xfile is a file (not a directory!) that exists in the root of the substituted drive. The below statement incorrectly echoes -exists-

if exist z:\xfile\nul echo -exists-

This makes xfile appear to be a directory, when it's really a file.

A non-substituted drive letter does not cause the problem. A subst to a non-network drive also does not cause the problem.

Is there a workaround to handle what looks like a subst or if-exist bug?

2

There are 2 answers

1
Compo On BEST ANSWER

Here is a general construct which should work from your .BAT file argument, (this one assumes it is the first argument %1):

@Echo Off
For /F "Tokens=1-2 Delims=d" %%A In ("-%~a1") Do (
    If "%%B" NEq "" (
        Echo %1 is a directory
    ) Else If "%%A" NEq "-" (
        Echo %1 is a file
    ) Else (
        Echo %1 does not exist
    )
)
Pause
0
Norbert On

Dont use the \Nul mechanism.

Better use this: (Simply add a backslash)

if exist "%ALLUSERSPROFILE%\" (
    echo Folder exist
) else (
    echo Folder does not exist
)