批处理文件 – 在批处理文件中测试文件属性

我正在编写一个批处理文件,我需要知道一个文件是否只读.我怎样才能做到这一点 ?

我知道如何使用%〜a修饰符,但我不知道该怎么做这个输出.它给出了类似于——的东西.如何在批处理文件中解析?

最佳答案
这样的事情应该有效:

@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.
)

当我运行这个我得到以下输出:

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

转载注明原文:批处理文件 – 在批处理文件中测试文件属性 - 代码日志