我正在写一个批处理文件,我需要知道,如果只读文件时。我该怎么办呢?

我知道如何让他们使用%〜修饰符,但我不知道如何处理这种输出做。它给像-ra ------。我怎样才能在批处理文件解析这个?

有帮助吗?

解决方案

像这样应该工作:

@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

其他提示

要测试特定文件:

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

要得到的只读文件列表

dir /ar *

要获得的读/写文件的列表

dir /a-r *

要列出所有文件和报告是否只读或读/写:

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

<强> 修改

如果文件名包含! 帕特里克回答失败。这可以通过切换接通和断开所述环路内延迟扩展来解决,但还有另一种方式来探测%%~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
  )
)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top