0

I have always tried to do this but have resorted to entering this in the command line:

CHKDSK C: /f

I tried to look up how to do it but im not that big on bat file programming and more so am just a small programmer learning java atm... I wanted to know how to do this and more so know how to compute it. As it would help me in the long term and also help me in the short term.

Would be much appreciated. :)

2

1 Answer 1

5

Using 'choice /c' command

This script will ask the user which drive to fix, and then show a confirmation message before 'fixing' the drive:

@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


Using 'set /p' command

This script is easier to write and understand, but it shouldn't be used:

@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
2
  • tried the program and getting an error in which it tells me : Cannot open volume for direct access. Complete! Press any key to continue . . .
    – GugiD
    Commented Sep 26, 2016 at 19:54
  • 3
    This type of code (the one that use choice) is the cause that many people thinks that Batch files are rudimentary and crude. A shorter equivalent would be: setlocal EnableDelayedExpansion & set letters= abcdefghijklmnopqrstuvwxyz & choice /n /c %letters% /m "Please...: " & set drv=!letters:~%errorlevel%,1! & echo Are you sure... & etc...
    – Aacini
    Commented Sep 27, 2016 at 0:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.