문제
배치 파일을 작성하고 파일을 읽는 지 알아야합니다. 어떻게 할 수 있습니까?
%~ 수정자를 사용하는 방법을 알고 있지만이 출력으로 무엇을 해야할지 모르겠습니다. -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
)
)
제휴하지 않습니다 StackOverflow