第二选择的BATCH问题

时间:2018-11-16 14:09:06

标签: batch-file

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

我是批量处理和编程的新手

这是一个代码:

@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

2 个答案:

答案 0 :(得分:1)

这里是一个示例,其中包括检测USB驱动器的更彻底的方法。

@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For /F "Skip=2 Tokens=*" %%A In ('WMIC DiskDrive Where InterfaceType^="USB"^
     Assoc /AssocClass:Win32_DiskDriveToDiskPartition 2^>Nul') Do (
    For /F UseBackQ^ Delims^=^"^ Tokens^=2 %%B In ('%%A') Do (
        For /F Delims^=^":^ Tokens^=6 %%C In (
            'WMIC Path Win32_LogicalDiskToPartition^|Find "%%B"') Do (
            For /F "Skip=1 Delims=" %%D In ('WMIC LogicalDisk Where^
             "DeviceID='%%C:'" Get DeviceID^, VolumeName') Do Echo( %%D
            Set "_C=!_C!%%C")))
If Not Defined _C Echo( You do not have a USB drive connected && GoTo :EndIt
If "%_C:~,1%" Equ "%_C%" GoTo :Picked
Echo( Enter the USB drive letter from the above [%_C%]:
For /F "Delims=? Tokens=2" %%A In ('Choice /C %_C%') Do Set "Letter=%%A:"
:Picked
If Not Defined Letter (Call :Task %_C%:) Else (Call :Task %Letter%)
:EndIt
>Nul Timeout 5
Exit/B
:Task
Choice /C YN /T 15 /D N /M "Do you want to delete all files from %1"
If ErrorLevel 2 Exit /B
REM Place your commands here for deleting from the selected drive

答案 1 :(得分:0)

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

@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