How to test if a path is a file or directory in Windows batch file?

前端 未结 6 589
心在旅途
心在旅途 2020-11-30 10:04

I searched here, found someone using this

set is_dir=0
for %%i in (\"%~1\") do if exist \"%%~si\"\\nul set is_dir=1

but didn\'t work, when

6条回答
  •  庸人自扰
    2020-11-30 10:57

    For a 1 liner:

    dir /a:d /b C:\Windows 2>&1 | findstr /i /n /c:"File Not Found">nul && (@echo. Im a file) || (@echo. Im a folder)
    

    e.g. change C:\Windows to C:\Windows\Notepad.exe

    -Sorry Arun, dbenham, didn't read yours! Same as..

提交回复
热议问题