Batch file to apply read only attr.

October 21, 2009 at 02:29:09
Specs: Windows
In order not to mess with older files (by opening them and accidentally saving), I change their attribute to read only. This can be done by right clicking a folder and selecting the read only box.
I was wondering if this can be done more easily (since I often forget doing it). I was thinking about a batch file that adds the read only feature to all the files in a folder (including sub folders) older than a month (or x days), if they do not already have it.
Can this be done?

Thanks in advance!

See More: Batch file to apply read only attr.

Report •

October 21, 2009 at 02:38:24
attrib +R * /S

would make everything in the current tree read only

With GNU find you could probably get this down to one command, something like:

find / -atime +4w -exec attrib +R {} \S ;

Should set +R on anything that hasn't been accessed in more than 4 weeks.

Report •

October 21, 2009 at 03:17:22
Thank you for your quick reply. I suppose it should be something simple like that.
But help me just a little bit more. Where do I find this "GNU find" program? I've searched a bit around and as far as I’ve seen this is Unix software... I’m using Windows.

Report •

October 21, 2009 at 04:51:18
Look here:

The Berkeley Find does not have -atime, but it does have -mtime, there is a difference between the two, one being that -mtime does not recognize weeks,.

Also, since there is already a find command in Windows, you should probably make a separate directory called Unix and put all your Unix commands there. Then when you need to use the Unix commands simply use the complete path.

So something like:

c:\path\to\unix\find / -mtime +30 -exec attrib +R {} \S ;

You also might want to ask your question in the Programming Forum. There is probably a DOS batch way of doing this.


Report •

Related Solutions

October 21, 2009 at 05:43:11
I am almost there :-)...
Using Berkeley find the batch works, but I still have 2 problems:
find . -mtime +45 -exec attrib {} +r ;
This command works only if the filename does not have spaces within... and I don't know how to solve this.
Second, I would like find to check first if the attribute R is already present, since the folder is quite big and this would probably speed up the process.

P.S. I am not familiar with the Programming Forum, but I am already getting a lot of help here. Thanks!

Report •

October 21, 2009 at 06:06:24
Even "{}" does not work.

Report •

October 21, 2009 at 21:46:40
Sorry, GNU Find has been ported to Windows w/ cygwin - I should have been clearer about that.

My 4.1 version has the same problem you describe, but the new 4.2.20 version escapes spaces in filenames correctly.

Try grabbing the new version at:

(Note the part about the 'setup' utility and required DLL's)

Report •

October 22, 2009 at 00:09:07
Thanks Fist, now it works. Thanks!
Anyhow, this find program is quite versatile. I’ve tried to analyze the help file but it is far from plain English. The thing I would like to add is a test to verify if the attribute is already present. This is because now it takes 4 minutes to perform the task on a intranet folder, but if it would be quicker I would use it also on another - larger folder.
Can you help me?

Report •

October 22, 2009 at 00:44:05
Add this:

-perm 444

(That's POSIX permissions in Octal - see: )

Report •

October 22, 2009 at 00:50:48
Wait, I"m wrong

The above finds files that are already read only

Use this to find files that are writable

-perm +2

Report •

October 22, 2009 at 01:03:29
Well... that’s it! It’s done.
I was hoping to gain more speed with this additional filter, but it is just 30-45 seconds quicker. Still a good improvement.
Thank you all for the help. I was not aware that this technology is so versatile. Good invention this “internet”! :-)

Again, great! Thanks!

Report •

Ask Question