查看set /?
帮助。您将需要使用set /p var=promptText
来询问用户输入。用户类型将存储在指示的变量中。
Diskpart
接受来自脚本文件和标准输入的命令。所以我们可以通过管道向它发送命令以向用户显示信息并检查选择。
以此为基础(另存为.cmd
文件)并适应您的需求。
@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 file
已禁用,生成的脚本仅回显到控制台。当一切按预期工作时,删除该type
行(不需要)和rem
之前的diskpart