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

前端 未结 6 598
心在旅途
心在旅途 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:48

    The /ad option for "dir" command lists folders, /b option for bare. Assuming you have checks for the existence of file in place, use:

    dir /ad /b ChangeThisToYourFilename 1> NUL 2> NUL
    if %ERRORLEVEL% EQU 0 (
        echo is a file
    ) else (
        echo is NOT a file
    )
    

提交回复
热议问题