第二选择的BATCH问题 [英] BATCH issue with second Choice

查看:54
本文介绍了第二选择的BATCH问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一些批处理文件,该文件将从选定的USB驱动器中删除所有文件.该代码有效,但是我想添加第二个选择,以确保用户确定他选择了正确的驱动器,并且第二个选择没有响应.无论我选择哪个选项,都将以某种方式删除此刻的所有文件.

I'm currently working on some batch file that will be deleting all files from selected usb drive. The code works, but i wanted to add second choice to be sure if user is certain that he choose correct drive or so and the second choice is not responding. No matter which option i'll choose it'll somehow delete all files from this point.

我是新来的批处理和编程

I'm new to batch and to programming also

这是一个代码:

@echo off

choice /c YN /t 15 /d n /m "Do you want to delete all files from USB drive? Y-yes, N-no"

setlocal enabledelayedexpansion
Set "USB="

if errorlevel == 1 goto ONE
if errorlevel == 2 goto TWO
if errorlevel == 255 goto ERROR

:ONE
for /f "tokens=1-5" %%a in (
 'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Found drive's:%USB%
set /p Drive=Choose drive: 
if "%Drive%"=="" goto :ERROR
if not exist %drive%:\ goto :ERROR
if %drive% EQU C goto ERROR
if %drive% EQU D goto ERROR
cd /D %Drive%:
tree

:CHOICE
choice /c YN /t 15 /d N /m "Are you sure you want to delete all files? Y-yes, N-no"
if errorlevel == 1 goto DELETE
if errorlevel == 2 goto TWO
goto END

:TWO
echo "Program was cancelled"
goto END

:DELETE
del * /S /F /Q
rmdir /S /Q %Drive%:
echo "Files are deleted"
goto END

:ERROR
echo "There was an error"
goto end

:END
echo "Done"
pause

推荐答案

我认为错误级别的值将在第一个if处被替换.我希望这对您有帮助:

I think that the errorlevel's value will be replaced at the first if. I hope that this will help you:

@echo off

setlocal enabledelayedexpansion
Set "USB="
choice /c YN /t 15 /d n /m "Do you want to delete all files from USB drive? Y-yes, N-no"

set x=%errorlevel%
If %x%==1 goto ONE
If %x%==2 goto TWO

:ONE
for /f "tokens=1-5" %%a in (
 'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Found drive's:%USB%
set /p Drive=Choose drive: 
if "%Drive%"=="" goto ERROR
if not exist %drive%:\ goto :ERROR
if %drive% EQU C goto ERROR
if %drive% EQU D goto ERROR
cd /D %Drive%:
tree

:CHOICE
choice /c YN /t 15 /d N /m "Are you sure you want to delete all files? Y-yes, N-no"
Set x=%errorlevel%
if %x% == 1 goto DELETE
if %x% == 2 goto TWO

:TWO
echo "Program was cancelled"
goto END

:DELETE
del * /S /F /Q
rmdir /S /Q %Drive%:
echo "Files are deleted"
goto END

:ERROR
echo "There was an error"
goto end

:END
echo "Done"
pause

这篇关于第二选择的BATCH问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆