views:

1007

answers:

1

Hi, 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 with this output. It gives something like -ra------. How can I parse this in batch file ?

+2  A: 

Something like this should work:

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

When I run this I get the following output:

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
Patrick Cuff
I didn't know about the ~1:1 thanks !
Eric Fortin
Ok this is working for 1 file but if INPUT is a set, ATTRIBS will always have the same value. I tried setLocal EnableDelayedExpansion but it complains when I try to use !ATTRIBS:~1,1! What could I do ?
Eric Fortin
Thanks ! For some reason using !ATTRIBS:~1,1! directly in the comparison didn't work but storing it in another variable works.
Eric Fortin