1

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?

Paul Houle
  • 735
  • 9
  • 17
  • If I was to check for a file named xfile in Z:\ I would use If Exist Z:\xfile, If I was checking for a directory named xfile in Z:\ I would use If Exist Z:\xfile\ _I haven't seen the use of `\nul` for well over a decade_. – Compo May 19 '17 at 00:11
  • What about this work-around: `dir /B /A:D "Z:\xfile" > nul 2>&1 && echo -exists-` to check a directory, and `dir /B /A:-D "Z:\xfile" > nul 2>&1 && echo -exists-` to check a file for existence? – aschipfl May 19 '17 at 00:30
  • @Compo If exist \\server\drv\fname\ will (incorrectly) return true when fname is a file. I formerly used whatever\fname\. to check for directory, but that gave false positive when fname was a file. I was recently told I should use the \nul construct [link](http://stackoverflow.com/questions/138981/how-to-test-if-a-file-is-a-directory-in-a-batch-script) but now that is failing in network cases. – Paul Houle May 19 '17 at 07:01
  • @aschipfl Yes I can do a messy dir workaround but that makes my skin crawl. Really my problem is a simple one... I have a .BAT file argument that is a name that could be pointing anywhere (local drive, network path, subst drive). I want to know IS IT AN EXISTING FILE or IS IT AN EXISTING DIRECTORY or DOES IT NOT EXIST AS EITHER. In each of these three cases I need to do a particular thing. – Paul Houle May 19 '17 at 07:08
  • 1
    for network paths why don't just map to a drive using `net use`? – phuclv May 19 '17 at 09:11
  • The problem with `if exist` and appending `\ `or `\nul` does not appear to be linked to `subst`, it seems to be the same for a drive mapped by `net use`; so you have to use something other, like querying the `Directory` attribute (by `dir /A` as I suggested above, for instance)... – aschipfl May 19 '17 at 09:34

2 Answers2

0

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
Compo
  • 36,585
  • 5
  • 27
  • 39
  • very nice... I can't break that and it solves my problem! Now I have to hunt down the definition of the 9 chars in %~a1 for future reference. Doesn't appear to be the same at the attrib output (for instance, there's no indexed "I" entry). – Paul Houle May 19 '17 at 22:16
0

Dont use the \Nul mechanism.

Better use this: (Simply add a backslash)

if exist "%ALLUSERSPROFILE%\" (
    echo Folder exist
) else (
    echo Folder does not exist
)
Norbert
  • 234
  • 1
  • 9