我会使用ATTRIB命令,例如:
attrib -r c:\folder\*.* /s
attrib
是命令
-r
是删除只读属性的标志
c:\folder\*.*
是你正在运行的文件夹,加上所有文件的通配符
/s
是做所有子目录和文件的标志
这里有更多关于attrib命令的文档和例子。 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/attrib
注意:其他的答案大多只使用-r
,这对设置了system
或hidden
属性的文件可能**不起作用。
所以这里有一个解决方案,可以递归地从目录内的所有文件(包括那些系统或隐藏的文件)中删除只读属性。
attrib -s -h -r "c:\path_to_folder\*.*" /s /d
描述:
-s
删除系统属性
-h
删除隐藏属性
-r
删除只读属性/s
设置/删除当前文件夹和包括子文件夹的属性/d
也设置/删除文件夹的属性。
我创建了这个批处理文件来做这件事。基本上这个批处理文件将清除其所在目录或其所在目录和所有下级目录中的只读属性。希望有人能找到它的用途。请原谅任何看似 “拙劣 "的代码,因为我自己也是刚开始学习批处理文件。
@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
这里有很多选项,但这个批处理文件支持将文件夹/s和/或文件/s放到批处理文件本身。
将下面这段代码保存到Read-only Off.bat
。
注意代码中的drop位如何工作。
@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