How to remove read-only attribute recursively on Windows

How to remove read-only attribute recursively on Windows

I need to remove read-only attributes of all files under a directory recursively on Windows using command line. Could you please provide an example on this?

答案1

I would use the ATTRIB command, for example:

attrib -r c:\folder\*.* /s

attrib is the command
-r is the flag for removing read-only attributes
c:\folder\*.* is the folder you are running it on, plus wildcards for all files
/s is the flag for doing all sub directories and files

Here is some more documentation and examples for the attrib command: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/attrib

答案2

First, open up a command prompt. Then cd into the directory where you want to start applying the attribute changes. Finally, enter the following command:

 attrib -R /S

That will remove the read-only attribute from all files in the current directory, then it will recurse down to do the same thing in all the subdirectories.

答案3

Note: Most of the other answers are using only -r which might not work on files which have system or hidden attributes set.

So here is a solution for recursively removing read-only attribute from all the files (including those which are system or hidden) inside a directory:

attrib -s -h -r "c:\path_to_folder\*.*" /s /d

Description:
-s Remove system attribute
-h Remove hidden attribute
-r Remove read-only attribute
/s Set/remove attributes in current folder and including subfolders
/d Set/remove attributes of folders too

答案4

Lots of options here but this batch file supports dropping folder/s and/or file/s to the batch file itself.

Save this code below to Read-only Off.bat.

Note for how the drop bit works inside the code.

@echo off
title ' %~nx0 ' by stephen147
color 5F
rem Place this inside a folder and run to remove the read-only attribute in the root folder and any folders or files within.
rem Or drop the folder/s and/or file/s to the batch file itself.
cd /d "%~dp0"
echo.
echo.Do you want to remove the read-only attributes inside this folder ? [ Ctrl + C to cancel ]
echo.
pause
echo.
echo.%cd%
attrib -s -d -r "%cd%\*.*"
attrib -s -d -r "%cd%"
rem This line supports dropping the folder/s and/or file/s to the batch file itself.
attrib -r "%*"
echo.
echo.Done
timeout /T 5
EXIT

相关内容