Paul Houle
Paul Houle

Reputation: 735

Batch exist test returns true for file\

I need to check if my 1st .bat argument is an existing file and not a directory, so I tried the simple:

if not exist "%~1\." if exist "%~1" echo exists as file

but "%~1\." tests as existing when "%~1" is a file (strange), so the test fails. I don't see a simple way around the problem.

This does duplicate: How to test if a file is a directory in a batch script?

Didn't know about the "name\NUL" trick to test for a directory. I've thought one had to use "name\." to do this. A scintilla of new info here might be that if exist "name\." is unexpectedly true when "name" exists as a file. This is counter-intuitive to me.

Upvotes: 0

Views: 304

Answers (1)

Magoo
Magoo

Reputation: 80033

if exist "%~1\." echo file or dir&if exist "%~1\.\*" (echo dir) else (echo file)

worked for me.

Upvotes: 1

Related Questions