我在大学里有一个任务,要编写一个脚本,该脚本将从选定的USB闪存驱动器中删除文件。技巧部分是驱动器,必须通过在脚本名称后添加字母来定义驱动器-例如“ script.bat D” <-从D中删除文件。 我设法编写了一个脚本,该脚本将从已定义的驱动器中删除文件,但是我不知道如何检查已定义的驱动器是否为USB。有人可以帮我吗?
我的代码:
@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
答案 0 :(得分:1)
这是我在上一个答案中提供的示例的未经测试的修改:
@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