Testing for file attribute in batch file

后端 未结 2 1227
执念已碎
执念已碎 2021-01-02 09:25

I\'m writing a batch file and I need to know if a file is read only. How can I do that ?

I know how to get them using the %~a modifier but I don\'t know what to do w

2条回答
  •  臣服心动
    2021-01-02 10:06

    To test a specific file:

    dir /ar yourFile.ext >nul 2>nul && echo file is read only || echo file is NOT read only
    

    To get a list of read only files

    dir /ar *
    

    To get a list of read/write files

    dir /a-r *
    

    To list all files and report whether read only or read/write:

    for %%F in (*) do dir /ar "%%F" >nul 2>nul && echo Read Only:  %%F|| echo Read/Write: %%F
    

    EDIT

    Patrick's answer fails if the file name contains !. This can be solved by toggling delayed expansion on and off within the loop, but there is another way to probe the %%~aF value without resorting to delayed expansion, or even an environment variable:

    for %%F in (*) do for /f "tokens=1,2 delims=a" %%A in ("%%~aF") do (
      if "%%B" equ "" (
        echo "%%F" is NOT read only
      ) else (
        echo "%%F" is read only
      )
    )
    

提交回复
热议问题