我正在尝试创建一个批处理文件来复制一些文件,然后删除它们的“只读”属性。
使用Windows 8.1。
我的文件看起来像这样:
@echo off
set s1=copy successful
set s2=remove read only successful
set timer=2
XCOPY C:\my_dir_1\my_dir_2 C:\my_dir_2 /D /E /C /R /I /K /Y
echo %s1%
timeout /T %timer%
attrib -r C:\my_dir_2 /s /d
echo %s2%
timeout /T %timer%
复制工作正常,当我执行此操作时,我会在复制所有文件时看到 cmd 提示符。计时器也按预期工作,但删除只读则不然。提示直接转到第二个计时器部分。我使用 this 链接作为教程。
attrib -r C:\my_dir_2 /s /d
设置文件夹的属性。您想为文件设置它,所以:
attrib -r C:\my_dir_2\* /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