windows 批处理脚本 if else 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15798930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch script if else command
提问by CharlieTango92
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.
我正在尝试制作一个简单的批处理脚本来擦除/归零闪存驱动器并重新格式化它。它是针对其他人的,所以我试图通过阻止格式化为 C:、D: 等来使其相对安全。
I'm looking for an IF ELSE type command i can use, to be an error catch-all.
我正在寻找一个我可以使用的 IF ELSE 类型命令,作为一个错误包罗万象的。
Here's (the main portion of) what i have ATM
这是(的主要部分)我有什么 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.)
不是有效的语法,但我可以使用什么来实现相同的目标?我知道有一种更有效的方法来做到这一点(例如,不是为可能的驱动器号定义 4 个变量,而是放置一个侦听用户输入并接受它(如果存在)的变量。)
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.
提前致谢。
回答by Ansgar Wiechers
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
)