批处理文件 - 将USB从读/写切换为只读,反之亦然

时间:2016-06-08 08:48:01

标签: batch-file

我想知道你是否可以写一个批处理文件来使USB从读/写变为只读,反之亦然?

程序应该做的是,如果你双击批处理文件,它会搜索USB驱动器,检查它是读/写还是只读,然后将其交换到另一个设置。

修改的 我知道USB的名称以及驱动器号。 修改

我需要这个用于我们使用USB驱动器在人们的笔记本电脑上安装程序的工作,之前我们使用过CD,但是它们在内部丢失了。

代码将不胜感激,但对网站的指示也会有所帮助。

2 个答案:

答案 0 :(得分:2)

我测试的4个驱动器经过测试和完美运行。

如果您愿意,我可以为您提供以下内容。它需要3个文件。批处理然后每个只读或读写命令1个文本文件。简单的设置,但要求你有一个输入,它将覆盖你目前所选择的任何内容。

感谢JBin建议它需要以管理员身份运行,我忘了这是件事。

cls
@Echo OFF
@Echo Toggle between read only and Read write
:choice
set /P c=Change to [R]ead Only or Read/[W]rite?
if /I "%c%" EQU "R" goto :read
if /I "%c%" EQU "W" goto :write

:read
DISKPART /s Readonly.txt
cls
@Echo  -----------------------
@Echo ^| Disk is now READ ONLY ^|
@Echo  -----------------------
@Echo.
goto :choice

:write
DISKPART /s Readwrite.txt
cls
@Echo  ------------------------
@Echo ^| Disk is now READ WRITE ^|
@Echo  ------------------------
@Echo.
goto :choice

制作两个文本文件,其中包含以下内容。将它们命名为您喜欢的但不要忘记更改批处理中的* .txt。这也假设您只有1个USB设备插入计算机

Readonly.txt文件:

Sel disk 1
att dis set readonly
exit

Readwrite.txt文件:

sel disk 1
att dis clear readonly
exit

答案 1 :(得分:0)

可以这样做是Batch(通过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及更高版本,请以管理员身份运行批处理文件以使其正常工作。