Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file is read-only and change in Windows batch file

How would I go about checking if a file is read-only, and if so change it to be not read-only, and also the other way around?

like image 454
Deniz Zoeteman Avatar asked Feb 09 '12 13:02

Deniz Zoeteman


2 Answers

I'm no DOS expert but if memory serves me, check the "attrib" command. That should tell you what you need to know, and you can use it to set the attributes as well.

attrib -r <filename> make it read/write
attrib +r <filename> make it readonly
like image 119
FreudianSlip Avatar answered Sep 28 '22 07:09

FreudianSlip


This did not work for me until I added a delay. This is my batch script (.bat):

attrib +r %1
ECHO Waiting 1 seconds
PING 1.1.1.1 -n 1 -w 1000 > NUL
%1
ECHO Waiting 3 seconds
PING 1.1.1.1 -n 1 -w 3000 > NUL
attrib -r %1

For bonus points, I wanted to use this to open a certain file type as read-only. So, I right-clicked a file of the time and under "Open With", I chose this batch script. Now I can do that whenever I want to open read-only.

like image 43
Steven Avatar answered Sep 28 '22 06:09

Steven