0

I've got a task on university to write a script that will be deleting files from selected usb flash drive. Trick part is drive must be defined by adding a letter after script's name - like "script.bat D" <- Deleting files from D. I've managed to write a script that will be deleting files from defined drive, but i don't have an idea how to check if defined drive is USB or not. Could anyone help me with that?

My code:

    @echo off
set zew=""
for /f "tokens=1-5" %%a in (
 'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set zew=!zew! %%a

if %1=="" goto :BLAD
setlocal enabledelayedexpansion
Set USB=%1
if %1 NEQ %zew% goto :BLAD
if not exist %USB%:\ goto :BLAD
if %USB% EQU C goto BLAD
cd /D %USB%:
del * /S /F /Q
rmdir /S /Q %USB%:
echo Files deleted
goto :END

:BLAD
echo The was an error
goto :END

:END
echo Success
pause
  • 2
    Parsing the (fixed width) output of `wmic logicaldisk list brief` is more difficult than using the csv variant with predetermined columns. `wmic logicaldisk where "drivetype=2" get driveletter /format:csv` see [batch-list-usb-drives ...](https://stackoverflow.com/questions/27303297/batch-list-usb-drives-then-copy-content) –  Nov 24 '18 at 20:44
  • @LotPings `wmic logicaldisk where "drivetype=2" get driveletter` doesn't work in all cases. I have an external USB drive which returns a drivetype of 3. – DavidPostill Nov 25 '18 at 09:09
  • @DavidPostill Look on example code at [Win32_LogicalDisk class](https://learn.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-logicaldisk). `DriveType` with value `2` is a `removable drive`, for example a USB flash stick. `DriveType` with value `3` is a `hard disk` connected via USB. – Mofi Nov 25 '18 at 09:26
  • @Mofi. I know. It is ambiguous. It may be a hard disk connected by USB, but it is still removable. I can do a safely remove hardware (eject) on it and unplug it just like I can with a USB stick. – DavidPostill Nov 25 '18 at 09:33
  • 1
    @LotPings `wmic logicaldisk where "drivetype=2" get driveletter /format:csv` does not work on Windows 7. The result are the error messages `Invalid query` because of `DriveLetter` instead of `DriveType` and `Invalid XSL format (or) file name.` because of `/Format:csv` not available on Windows 7. For that reason I use for such tasks `wmic.exe LOGICALDISK GET DeviceID,DriveType` and parse the UTF-16 LE encoded output with __FOR__ which works on Windows XP / 7 / 8 /10. – Mofi Nov 25 '18 at 09:39
  • Like DavidPostill already wrote, the `DriveType` value is ambiguous for hard disks because `3` means __local hard disk__ independent on being connected with ATA (PATA), SATA, eSATA or USB. – Mofi Nov 25 '18 at 09:45
  • Possible duplicate of [BATCH issue with second Choice](https://stackoverflow.com/questions/53339474/batch-issue-with-second-choice). If you take a look at the question you asked previously, which is effectively the same question, I provided an answer, which should do exactly as you need. The only thing I did not include in it, is the `Del` and `RD` commands. It works by predetermining the USB drive letters and offers a selection menu for them. If there is only one USB connected, it will auto select that and ask you if you wish to delete the contents! – Compo Nov 25 '18 at 11:09
  • @Compo I dissagree. This script needs to take a defined letter of drive when the scripts starts, not during it's work. That's the difference. – Daniel Minder Nov 25 '18 at 13:01
  • @DanielMinder, the question is specific`i don't have an idea how to check if defined drive is USB or not. Could anyone help me with that?` As I said, that is exactly what my code does, adding a few `If`'s and `Else`'s, you should be able to implement it yourself with an input parameter. It's not me that's at university, please make some more effort yourself. – Compo Nov 25 '18 at 14:27

1 Answers1

1

Here's an untested modification of the example I provided in my previous answer:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "_I=%~1"
If Not Defined _I (Echo= No input parameter provided.
    Echo= Exiting . . . & Timeout 3 /NoBreak>Nul & Exit /B)
Mountvol|Find /I "%_I:~,1%:\">Nul && (Set "_I=%_I:~,1%") || (
    Echo= Invalid parameter provided.
    Echo= Exiting . . . & Timeout 3 /NoBreak>Nul & Exit /B)
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 (
                If /I "%%C"=="%_I%" GoTo :Task)))
Echo= %_I%: is not a connected USB drive.
>Nul Timeout 5
Exit/B
:Task
Choice /C YN /T 15 /D N /M "Do you want to delete all files from %_I%:"
If ErrorLevel 2 Exit /B
CD /D %_I%:
(   Del /A/F/Q *
    RD /S /Q *)>Nul 2>&1
Compo
  • 36,585
  • 5
  • 27
  • 39
  • This is perfect! Does excaclly what i need. I dont know how should i be able to write such things.. I'm not even studying programming. Thank you so much – Daniel Minder Nov 26 '18 at 19:40