0

I was wondering if there's a way to check whether a single file has a read-only attribute using a batch script. Something like: if file is read-only echo read-only!

I've tried the following but to no avail:

set ATTRIBS=%%~aF
set CURR_FILE=C:\a.txt
set READ_ATTRIB=!ATTRIBS:~1,1!
if !READ_ATTRIB!==- (
    @echo !CURR_FILE! is read-write
) else (
    @echo !CURR_FILE! is read only
)

Thanks, Shai.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
ShaiO
  • 153
  • 1
  • 10
  • Having the read-only attibute and being read-only are different things. What do you exactly need? – MC ND May 29 '17 at 09:31
  • The simplest solution might be: `attrib myfile.txt | findstr "^.....R" >nul 2>nul && echo "read-only!"` – Bohdan Levchenko May 29 '17 at 09:46
  • @BohdanLevchenko, `dir /ar myfile.txt >nul 2>&1 && echo read-only` will filter for read-only files without requiring a pipe or external programs such as attrib.exe or findstr.exe. – Eryk Sun May 29 '17 at 09:52
  • something like this: If a.txt is read-only do something.... – ShaiO May 29 '17 at 10:14
  • Batch never dies. Depending on if you are allowed to touch that file, how about: "(>nul call;>>myfile.txt) && echo I can write!" - The trick is the "call;" to generate nothing, which is tried to append it to the file. – Tom Stein Sep 25 '20 at 13:19

0 Answers0