0

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.

Community
  • 1
  • 1
Paul Houle
  • 735
  • 9
  • 17
  • 1
    Possible duplicate of [How to test if a file is a directory in a batch script?](http://stackoverflow.com/questions/138981/how-to-test-if-a-file-is-a-directory-in-a-batch-script) – Mofi May 15 '17 at 05:15
  • I think you simply need to remove the dot `.` behind the backslash... – aschipfl May 15 '17 at 05:31

1 Answers1

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

worked for me.

Magoo
  • 77,302
  • 8
  • 62
  • 84