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

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

    I just tried in this way. Hope this helps.

    @ECHO OFF
    SET CURR_DIR=%CD%
    SET IS_DIR=0
    CD %1% 
    IF "%ERRORLEVEL%"=="0" SET IS_DIR=1
    CD %CURR_DIR%
    ECHO IS DIRECTORY %IS_DIR% 
    

    Output:

    D:\Work\Stand alone Java classes>test.bat D:\Work\Training
    IS DIRECTORY 1
    
    D:\Work\Stand alone Java classes>test.bat D:\Work\Training\SRT.txt
    The directory name is invalid.
    IS DIRECTORY 0
    

提交回复
热议问题