Eri*_*tin 9 batch-file file-attributes
我正在写一个批处理文件,我需要知道文件是否只读.我怎样才能做到这一点 ?
我知道如何使用%~a修饰符来获取它们但我不知道如何处理此输出.它给出了类似-ra ------的东西.我如何在批处理文件中解析它?
Pat*_*uff 13
这样的事情应该有效:
@echo OFF
SETLOCAL enableextensions enabledelayedexpansion
set INPUT=test*
for %%F in (%INPUT%) do (
set ATTRIBS=%%~aF
set CURR_FILE=%%~nxF
set READ_ATTRIB=!ATTRIBS:~1,1!
@echo File: !CURR_FILE!
@echo Attributes: !ATTRIBS!
@echo Read attribute set to: !READ_ATTRIB!
if !READ_ATTRIB!==- (
@echo !CURR_FILE! is read-write
) else (
@echo !CURR_FILE! is read only
)
@echo.
)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下输出:
File: test.bat Attributes: --a------ Read attribute set to: - test.bat is read-write File: test.sql Attributes: -ra------ Read attribute set to: r test.sql is read only File: test.vbs Attributes: --a------ Read attribute set to: - test.vbs is read-write File: teststring.txt Attributes: --a------ Read attribute set to: - teststring.txt is read-write
要测试特定文件:
dir /ar yourFile.ext >nul 2>nul && echo file is read only || echo file is NOT read only
Run Code Online (Sandbox Code Playgroud)
获取只读文件列表
dir /ar *
Run Code Online (Sandbox Code Playgroud)
获取读/写文件列表
dir /a-r *
Run Code Online (Sandbox Code Playgroud)
列出所有文件并报告是只读还是读/写:
for %%F in (*) do dir /ar "%%F" >nul 2>nul && echo Read Only: %%F|| echo Read/Write: %%F
Run Code Online (Sandbox Code Playgroud)
编辑
如果文件名包含,Patrick的答案将失败!.这可以通过在循环内切换延迟扩展来解决,但还有另一种方法来探测%%~aF值而不需要延迟扩展,甚至是环境变量:
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
)
)
Run Code Online (Sandbox Code Playgroud)