8

I'm trying to make a simple batch script to wipe/zero a flash drive and reformat it. It's for others, so i'm attempting to make it relatively safe, by blocking formatting to C:, D:, etc.

I'm looking for an IF ELSE type command i can use, to be an error catch-all.

Here's (the main portion of) what i have ATM

:again
echo.
cls
echo.
echo  Please select the drive letter for the flash
echo  drive you wish to erase
echo.
echo    **** DO NOT SELECT C: OR D: ****
echo.
echo.
echo   *** Enter letter (no colon) ONLY e.g. "E" ***
echo.
set /p answer=
cls
echo.
echo.
echo.
if /i "%answer:~,1%" EQU "e" format E: /x /fs:FAT32 /v:FORENSICS /p:2
if /i "%answer:~,1%" EQU "f" format F: /x /fs:FAT32 /v:FORENSICS /p:2
if /i "%answer:~,1%" EQU "g" format G: /x /fs:FAT32 /v:FORENSICS /p:2
if /i "%answer:~,1%" EQU "h" format H: /x /fs:FAT32 /v:FORENSICS /p:2
if /i "%answer:~,1%" NOT exist goto again && echo    Please provide a valid drive letter for flash drive
echo.

Now I know that

if /i "%answer:~,1%" NOT exist goto again && echo    Please provide a valid drive letter for flash drive

is not valid syntax, but what could I use in order to achieve the same goal? I know there is a more effecient way to do this (e.g. instead of defining 4 variables for likely drive letters, put a single variable that listens to user input and accepts it if it exists.)

This is my first foray into batch scripting (and scripting in general) and i'm learning on-the-fly, so the "dumber" you can make it, the better.

Thanks in advance.

3
  • You also need to escape the parenthesis in your echo command. Commented Apr 3, 2013 at 22:39
  • where does this parenthesis need to be placed? I placed it after "flash drive" and the command looped. Commented Apr 4, 2013 at 0:08
  • In your code where it has something like echo blah... (no colon) ... you need to escape the brackets. I'm on my phone so you can see my problem. Commented Apr 4, 2013 at 2:21

1 Answer 1

13

Try this:

echo.%answer:~,1% | findstr /r /i "[e-h]"
if %errorlevel% equ 0 (
  format %answer:~,1%: /x /fs:FAT32 /v:FORENSICS /p:2
) else (
  echo Please provide a valid drive letter for flash drive.
  goto again
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.