batch-filefile-existssubst

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


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?


Solution

  • 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