如何在Windows 7上递归删除只读属性


Answers:


87

我将使用ATTRIB命令,例如:

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

attrib是命令,
-r是删除只读属性的标志,
c:\folder\*.*是您正在其上运行的文件夹
/s的标志,加上所有文件的通配符,是执行所有子目录和文件的标志

这是attrib命令的更多文档和示例:https : //docs.microsoft.com/zh-cn/windows-server/administration/windows-commands/attrib


16
/d如果您还希望它也处理实际的文件夹,请添加一个。
LawrenceC

2
请注意,这不适用于标记为“隐藏”或“系统”的文件。为了从这些文件中删除只读属性,您还必须删除其他属性(例如attrib -h -r)。
Excelcius

2
如果路径包含放在“”中的空格,例如:attrib -r“ c:\ Program Files(x86)\ SmartGit *。*” / s
DanielHári16年

该链接上的页面不再存在。
Broots Waymb

25

首先,打开命令提示符。然后cd进入要开始应用属性更改的目录。最后,输入以下命令:

 attrib -R /S

这将从当前目录中的所有文件中删除只读属性,然后向下递归以在所有子目录中执行相同的操作。


9

注意:其他大多数答案仅在使用-r,可能对设置了或属性的文件不起作用systemhidden

因此,这是一种从目录中的所有文件(包括系统文件或隐藏文件)中递归删除只读属性的解决方案:

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

说明:
-s删除系统属性
-h删除隐藏属性
-r删除只读属性 /s设置/删除当前文件夹中的属性,包括子文件夹也 /d设置/删除文件夹的属性


2
这应该被设置为最佳答案。
朱利叶斯

2

我创建了此批处理文件来执行此操作。基本上,该批处理文件将清除其所在目录或其中的目录以及所有较低目录中的只读属性。希望有人能找到它的用处。请原谅我刚刚开始学习批处理文件的任何看似“较差”的代码。

@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

这里有很多选项,但是此批处理文件支持将文件夹和/或文件拖放到批处理文件本身。

将此代码保存到以下位置Read-only Off.bat

请注意放置位在代码内部的工作方式。

@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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.