使用自动化DISKPART磁盘分区,并提示用户进行磁盘或退出 [英] Automate partitioning a disk using DISKPART and prompting user for disk or quit

查看:982
本文介绍了使用自动化DISKPART磁盘分区,并提示用户进行磁盘或退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个批处理文件,该文件的分区和格式化USB驱动器。这是创建一个Windows 7 USB启动盘。

I need to create a batch file which partitions and formats a USB drive. This is to create a Windows 7 USB boot disk.

DISKPART是所使用的工具,并传递给它的指令为:

DISKPART is the tool used and you pass it instructions as in:

DISKPART / S instructions.txt

DISKPART /s instructions.txt

在我来说,我希望我的说明是这样的:

In my case I want my instructions to look like this:

example prepdisk.txt
SELECT DISK <Prompt user to either enter a number or q for quit>
CLEAN
CREATE PARTITION PRIMARY
FORMAT QUICK FS=NTFS LABEL="Windows"
ACTIVE
ASSIGN LETTER="C"

然后我会在这样一个批处理文件:

Then I would have a batch file like this:

prepdisk.bat:

prepdisk.bat:

DISKPART /s prepdisk.txt

但我坚持就如何提示用户对于磁盘ID。我也希望能够不确定是否要退出,因为很明显的格式错误的驱动器可能是危险的。

But I am stuck on how to prompt the user for a disk id. I also want to be able to quit if unsure because obviously formatting the wrong drive could be dangerous.

推荐答案

请参阅设置/?帮助。您将需要使用设定/ p = VAR promptText ,要求输入用户。什么类型的用户将存储在所指示的变量

See set /? help. You will need to use set /p var=promptText to ask a user for input. What the user types will be stored in the indicated variable.

的Diskpart 接受来自一个脚本文件,并从标准输入的命令。因此,我们可以通过管道命令它显示的信息给用户,并检查选择。

Diskpart accepts commands from a script file and from stdin. So we can pipe commands to it to show information to the user and check the selection.

使用此作为基础(保存为的.cmd 文件),并适应您的需求。

Use this as a base (save as a .cmd file) and adapt to your needs.

@echo off
    setlocal enableextensions disabledelayedexpansion

:ask
    rem select disk to format
    call :showDiskTable
    set /p "diskNumber=Type number of disk to format: " || goto :processCancelled

    rem test disk selection
    (   echo select disk %diskNumber%
        echo list disk
    ) | diskpart | findstr /b /c:"*" >nul || (
        echo(
        echo WRONG SELECTION
        echo(
        goto :ask
    )

    rem confirm disk selection
    cls
    call :showDiskTable
    set "answer=%random%%random%"
    set /p "confirm=If you are sure you want to format disk %diskNumber%, type %answer%:"
    if not "%confirm%"=="%answer%" goto :processCancelled

    rem Create script file
    set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
    > "%scriptFile%" (
        echo SELECT DISK %diskNumber%
        echo CLEAN
        echo CREATE PARTITION PRIMARY
        echo FORMAT QUICK FS=NTFS LABEL="Windows"
        echo ACTIVE
        echo ASSIGN LETTER="C"
    )

    rem execute script file
    type "%scriptFile%"
    rem diskpart /s "%scriptFile%"

    rem cleanup and exit
    del /q "%scriptFile%"

    echo(
    echo DONE
    echo(

    exit /b 0

:showDiskTable
    echo =====================================================
    echo list disk | diskpart | findstr /b /c:" "
    echo =====================================================
    echo(
    goto :eof

:processCancelled
    echo(
    echo PROCESS CANCELLED
    echo(
    exit /b 1

请注意,对于测试的diskpart / s的文件被禁用,生成的脚本只回显到控制台。当一切都如预期运作,删除键入行(不需要)和 REM DISKPART

Note that for testing the diskpart /s file is disabled and the script generated is only echoed to console. When everything works as intended, remove the type line (not needed) and the rem before the diskpart

这篇关于使用自动化DISKPART磁盘分区,并提示用户进行磁盘或退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆