可以通过批处理(通过 Diskpart 命令)执行此操作,但会非常复杂。
无论如何,这段代码会检测可移动驱动器(我认为软盘也包括在内),创建一个菜单,然后交换设置(只读/读写)。它使闪存驱动器“写保护”,但我测试了我的代码......
@echo off
setlocal enabledelayedexpansion
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Detect removable drives using DiskPart...
set "diskcnt=0"
for /f "tokens=1-2 delims= " %%A in (
'^(echo list disk^)^|diskpart^|findstr /ic:"Online"'
) do (
for /f "tokens=1-3 delims= " %%X in (
'^(echo select disk %%B^&echo detail disk^)^|diskpart^|findstr /ic:"Removable"'
) do (
set /a diskcnt+=1
set "vol!diskcnt!=%%B_%%Z:"
)
)
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::After detecting drives, print a menu...
echo.The script detected %diskcnt% removable drive/s:
echo.
if %diskcnt% equ 0 echo No drives found.
for /l %%X in (1,1,%diskcnt%) do (
echo. %%X. Drive !vol%%X:~2,2!
)
echo.
:choice_loop
set /p "choice=Choose a Number: "
if !choice! geq 1 if !choice! leq %diskcnt% goto :doIt
goto choice_loop
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Get and set the setting...
:doIt
echo.
set "disknum=!vol%choice%:~0,1!"
for /f "tokens=1-2 delims=:" %%F in (
'^(echo select disk %disknum%^&echo attribute disk^)^|diskpart^|findstr /bic:"Read-Only"'
) do (
set "ro_state=%%G"&set "ro_state=!ro_state: =!"
)
if /i "%ro_state%" equ "No" (
(echo select disk %disknum%&echo attribute disk set readonly)|diskpart >nul
echo Drive !vol%choice%:~2,2! is now read-only from being read/write.
) else (
(echo select disk %disknum%&echo attribute disk clear readonly)|diskpart >nul
echo Drive !vol%choice%:~2,2! is now read/write from being read-only.
)
endlocal
pause
编辑:我唯一担心的是这取决于语言,所以如果语言不是英语,这将不起作用。
编辑#2:请原谅我对问题的误解。如果您已经有 USB 名称要修改,请尝试以下操作:(确保没有 2 个同名闪存驱动器同时插入。)
@echo off
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Put the name of flash drive here... (Case-sensitive)
set "name=[PUT NAME HERE]"
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Detect that removable drive using DiskPart...
set "disk_num="
for /f "tokens=1-2 delims= " %%A in (
'^(echo list disk^)^|diskpart^|findstr /ic:"Online"'
) do (
for /f "tokens=1-4 delims= " %%W in (
'^(echo select disk %%B^&echo detail disk^)^|diskpart^|findstr /ic:"Removable"'
) do (
if "%%Z"=="%name%" set "disk_num=%%B"
)
)
if not defined disk_num (
echo Flash drive '%name%' not found.
goto end_this
)
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Get and set the setting...
for /f "tokens=1-2 delims=: " %%F in (
'^(echo select disk %disk_num%^&echo attribute disk^)^|diskpart^|findstr /bic:"Read-Only"'
) do (
set "ro_state=%%G"
)
if /i "%ro_state%" equ "No" (
(echo select disk %disk_num%&echo attribute disk set readonly)|diskpart >nul
echo Flash drive '%name%' is now read-only from being read/write.
) else (
(echo select disk %disk_num%&echo attribute disk clear readonly)|diskpart >nul
echo Flash drive '%name%' is now read/write from being read-only.
)
:end_this
pause
exit /b
编辑#3:对于 Windows 8 及更高版本,以管理员身份运行批处理文件以使其正常工作。