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

How to make a bat file that asks the user for the drive they want to fix?

我一直尝试这样做,但不得不在命令行中输入:

CHKDSK C: /f

我试图查找如何做到这一点,但我对 bat 文件编程并没有那么了解,所以我只是一个学习 java atm 的小程序员... 我想知道如何做到这一点,更想知道如何计算它。因为它对我长期有帮助,对我短期也有帮助。

将不胜感激。 :)

使用'choice /c'命令

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

@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


使用'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