简体   繁体   中英

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 (eg 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.

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM