2

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

Here's a code:

@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
    Enclose errorlevel variable in percent signs: `if %errorlevel% == 1 goto ONE` (in the same way you did with `USB` and `drive` variables). – Aacini Nov 16 '18 at 14:19
  • 1
    Even better: First line after `choice` is: `if errorlevel 255 goto ERROR` which means __IF__ `errorlevel` is __GREATER or EQUAL__ `255` __THEN__ `goto`. Second line is: `if errorlevel 2 goto TWO` and next the command lines for `:ONE` follows. Run in a command prompt window `if /?` for help on special syntax for evaluating the exit code of previous command or application. Main advantage of `if errorlevel X goto ...` in comparison to `if %errorlevel% == 2 goto ...` is that the latter does not work inside a command block starting with `(` and ending with matching `)`. – Mofi Nov 16 '18 at 14:30
  • My answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) could be also interesting for you. And I suggest taking a look on results list of a Stack Overflow search using search term [\[batch-file\] usb drive wmic](https://stackoverflow.com/search?q=%5Bbatch-file%5D+usb+drive+wmic). It is possible to get a list of USB drives currently connected with their drive letters and let the user choose from those drive letters. That would be much better for this task. – Mofi Nov 16 '18 at 14:32
  • @Aacini Thank's a lot. Such an stupid issue, but happend. – Daniel Minder Nov 16 '18 at 14:36
  • @Mofi Thank you. Now i understand where my problem came from. – Daniel Minder Nov 16 '18 at 14:37

2 Answers2

1

Here's an example, which includes a more thorough method of detecting USB drives.

@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
Compo
  • 36,585
  • 5
  • 27
  • 39
0

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
FZs
  • 16,581
  • 13
  • 41
  • 50