如何制作一个bat文件,要求用户提供他们想要修复的驱动器?

0 command cmd batch-file

我一直试图这样做,但已经在命令行中输入:

CHKDSK C: /f
Run Code Online (Sandbox Code Playgroud)

我试图查找如何做到这一点,但我不是那么大的蝙蝠文件编程和更多,所以我只是一个小程序员学习java atm ...我想知道如何做到这一点,所以知道如何计算它.因为它可以长期帮助我,并在短期内帮助我.

非常感谢.:)

sam*_*mdd 5

使用'choice/c'命令

此脚本将询问用户修复哪个驱动器,然后在"修复"驱动器之前显示确认消息:

@echo off
:start
    setlocal EnableDelayedExpansion
    set letters= abcdefghijklmnopqrstuvwxyz
    choice /n /c %letters% /m "Please enter the drive letter you would like to fix: "
    set drv=!letters:~%errorlevel%,1!
    echo Are you sure... to fix %drv%:\?
    choice
    if errorlevel 2 goto :start
    chkdsk %drv%: /f
    echo Complete!
pause
Run Code Online (Sandbox Code Playgroud)


使用'set/p'命令

此脚本更易于编写和理解,但不应使用它:

@echo off
:start
:: Clears the contents of the %drv% variable, if it's already set 
    set "drv="
:: Queries the user for input
    set /p "drv=Please enter the drive letter you would like to fix: "
:: Check if input was blank
    if "%drv%"=="" echo Don't leave this blank&goto :start
:: Check if input contained more then 1 letter (Doesn't account for numbers or special characters)
    if not "%drv:~1,1%"=="" echo Please enter the drive letter&goto :start

    echo Are you sure you want to fix %drv%:\?
    choice
    if errorlevel 2 goto :start
    chkdsk %drv%: /f
    echo Complete!
    pause
Run Code Online (Sandbox Code Playgroud)

  • 这种类型的代码(使用`choice`的代码)是许多人认为批处理文件是基本和粗糙的原因.较短的等价物是:`setlocal EnableDelayedExpansion`&`set letters = abcdefghijklmnopqrstuvwxyz`&`choice/n/c%letters%/ m"Please ...:"`&`set drv =!letters:〜%errorlevel%, 1!```echo你确定......`等等...... (3认同)