Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch script if else command

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.

like image 233
CharlieTango92 Avatar asked Apr 03 '13 21:04

CharlieTango92


1 Answers

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
)
like image 107
Ansgar Wiechers Avatar answered Oct 03 '22 23:10

Ansgar Wiechers