【问题标题】:Batch file - Switch USB from read/write to read-only and vice versa批处理文件 - 将 USB 从读/写切换为只读,反之亦然
【发布时间】:2016-10-08 10:21:06
【问题描述】:

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

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

编辑 我知道 USB 的名称以及驱动器号。 编辑

我需要这个用于我们使用 USB 驱动器在人们的笔记本电脑上安装程序的工作,我们以前使用过 CD,但它们在里面迷路了。

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

【问题讨论】:

    标签: batch-file


    【解决方案1】:

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

    【讨论】:

    • 它打开了Diskpart,但是打开后我该怎么办?
    • 等等,我不能通过找到它的名称来搜索 USB,因为我知道设备的名称是什么。
    • 另外我知道驱动器号是什么,我会编辑我的问题
    • @JBin 所以它不起作用?输出必须类似于链接:i.imgur.com/OMu4OHW.jpg
    【解决方案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
    

    【讨论】:

    • 好的,我已经测试过了,但实际上并没有发生任何事情。这就是我得到的:imgur.com/13aTU5u
    • 并且这两个文件都与 BAT 文件位于同一位置。拼写正确,一切如你所说。
    • 发送位置、文件名和文本文件内容的屏幕截图。你也修改了批次吗?
    • 我的似乎工作正常。打开 CMD,键入 DISKPART > LIST DIS。如果只有 1 个 USB,那么应该只显示磁盘 0 和磁盘 1(磁盘 1 是 USB)。 i.imgur.com/VcNdPfD.png
    • 一模一样,只有 Disk 0 和 Disk 1,其中 Disk 1 是 USB。 imgur.com/NXie5b8
    猜你喜欢
    • 2016-04-22
    • 2017-11-02
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2023-04-03
    • 2014-02-15
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多