93

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
  • 1
    Out of my head without testing attrib /S -R
    – nixda
    Oct 3, 2013 at 15:54

5 Answers 5

122

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

5
  • 23
    Add a /d if you want it to process the actual folders too.
    – LawrenceC
    Oct 3, 2013 at 17:00
  • 2
    Note that this doesn't work with files markes as "hidden" or "system". In order to remove the read-only attribute from those files you have to remove the other attributes as well (for example attrib -h -r).
    – Excelcius
    Apr 8, 2015 at 8:43
  • 3
    if path contain spaces put in "" like: attrib -r "c:\Program Files (x86)\SmartGit*.*" /s Jun 15, 2016 at 18:09
  • Page at that link no longer exists. Jan 8, 2019 at 15:27
  • 1
    cd /d "path" & attrib -r "%cd%\*" /s can be used from current directory if required. Also as Daniel has said, wrap the path with one " either side.
    – Ste
    Jan 28, 2020 at 15:21
25

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.

23

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

3
  • 4
    This should be set as the best answer.
    – Julius
    May 30, 2018 at 11:09
  • attrib -s -h -r "c:\path_to_folder\*.*" /s /d how do you apply this to the current working directory and all of its subdirs? In other words, if I am already in the c:\path_to_folder dir, and don't want to write the path again. Jul 26, 2020 at 0:52
  • @user3026965 try the following: attrib -s -h -r *.* /s /d right from that folder Jul 27, 2020 at 7:14
2

I created this batch file to do this. Basically this batch file will clear read only attributes in the directory its in or the directory its in and all lower directories. Hope someone finds a use for it. Excuse any code that may seem "poor" as I am just starting to learn batch files myself.

@ECHO off
:begin
ECHO Would you like to only remove read only attributes
ECHO from this director or from all the sub directores as
ECHO well?
ECHO.
ECHO [A] This directory only
ECHO [B] All directories - cascading
ECHO [C] Cancel
SET /P actionChoice="Option(A,B,C): "
ECHO.
IF "%actionChoice%" == "A" GOTO A
IF "%actionChoice%" == "B" GOTO B
IF "%actionChoice%" == "C" GOTO C
GOTO badChoice

:A
CLS
ECHO Are you sure you want to remove all read-only
ECHO attributes from this directory only?
ECHO.
ECHO Directory:
ECHO.
ECHO %CD%
ECHO.
SET /P continueChoice="Continue? (Y, N): "
IF "%continueChoice%" == "N" GOTO abort
ECHO Removing Read Only Attributes From Local Directory...
SET currectDirectory=%CD%
ECHO Current directory is: %currectDirectory%
FOR %%G IN (%currectDirectory%\*) DO (
ECHO %%G
ATTRIB -R "%%G"
)
GOTO end

:B
CLS
ECHO Are you sure you want to remove all read-only
ECHO attributes from this directory and all sub-directories?
ECHO.
ECHO Directory:
ECHO.
ECHO %CD%
ECHO.
SET /P continueChoice="Continue? (Y, N): "
IF "%continueChoice%" == "N" GOTO abort
ECHO Removing Read Only Attributes Cascading...
FOR /R %%f IN (*) DO (
ECHO %%f
ATTRIB -R "%%f"
)
GOTO end

:C
CLS
ECHO Cancel: no files have been changed
GOTO end

:badChoice
CLS
ECHO Unknown Option
ECHO.
ECHO.
ECHO.
GOTO begin

:abort
CLS
ECHO No files have been changed
ECHO.
ECHO.
ECHO.
GOTO begin

:end
ECHO Read only attributes removed
PAUSE
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .