如何在 CMD 中一次格式化多个 USB 驱动器?

如何在 CMD 中一次格式化多个 USB 驱动器?

我在一所学校工作。考试的时候,我们会给学生提供 U 盘。

检查后,我们需要格式化所有驱动器。到目前为止,我们一直在使用以下 CMD 脚本

但是,我需要对每个驱动器按 Enter 键。有没有办法跳过“按 Enter 键进行格式化”?

ECHO PRESS ANY KEY TO START DRIVE FORMATTING OR CLOSE THIS WINDOW NOW
PAUSE
rem SET DRIVES[0]=D:
SET DRIVES[1]=E:
SET DRIVES[2]=F:
SET DRIVES[3]=G:
SET DRIVES[4]=H:
SET DRIVES[5]=I:
SET DRIVES[6]=J:
SET DRIVES[7]=K:
SET DRIVES[8]=L:
SET DRIVES[9]=M:
SET DRIVES[10]=N:
SET DRIVES[11]=O:
rem SET DRIVES[12]=P:
rem SET DRIVES[13]=Q:
rem SET DRIVES[14]=R:
rem SET DRIVES[15]=S:
rem SET DRIVES[16]=T:
rem SET DRIVES[17]=U:
rem SET DRIVES[18]=V:
rem SET DRIVES[19]=W:
rem SET DRIVES[20]=X:
rem SET DRIVES[21]=Y:
rem SET DRIVES[22]=Z:
ECHO PRESS ANY KEY TO START DRIVE FORMATTING OR CLOSE THIS WINDOW NOW.  LAST CHANCE!
PAUSE

FOR /F "TOKENS=1* DELIMS==" %%D IN ('SET DRIVES[') DO (

REM IF EXIST %%E\KILLSWITCH.INI ECHO FORMATTING %%E
REM IF EXIST %%E\KILLSWITCH.INI 
    FORMAT %%E /FS:FAT32 /V:BESTSCHOOLEVER-USB /X /Q 
REM & ECHO > %%E\KILLSWITCH.INI

)
ECHO PROCESS FINISHED
PAUSE ``` 

答案1

@echo off

for /f usebackq^tokens^=^4delims^=^>^< %%i in =;(`
     wmic logicaldisk where drivetype^=2 get name /format:xml ^| find "VALUE"
        `);= do if exist "%%~i\killswitch.ini" =;(
             echo\Formatting %%~i...
             <nul echo\ | format %%~i /fs:fat32 /v:School-USB /x /q | findstr ") : ,"
             cd.> %%~i\KILLSWITCH.INI
             start "" /w RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll
            );=

如果驱动器根文件夹中的文件的存在killswitch.ini足以过滤目标驱动器进行格式化,则使用它作为驱动器查找,并重定向|以跳过确认。


如果这还不够的话,您还可以通过以下方式进行预先过滤VolumeName='SCHOOL-USB'

@echo off

for /f usebackq^tokens^=^4delims^=^>^< %%i in =;(`
     wmic logicaldisk where "DriveType=2 and VolumeName='SCHOOL-USB'" get name /format:xml ^| find "VALUE"
        `);= do if exist "%%~i\killswitch.ini" =;(
             echo\Formatting %%~i...
             <nul echo\ | format %%~i /fs:fat32 /v:School-USB /x /q | findstr ") : ,"
             cd.> %%~i\KILLSWITCH.INI
             start "" /w RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll
            );=

如果您需要将其用于多个闪存驱动器,则通过循环:label/loop可以提供帮助并让您有时间切换设备:

@echo off

set "_check_USB=wmic logicaldisk where "DriveType=2 and VolumeName='SCHOOL-USB'""

%:^(
2>nul %_check_USB% | find ":" >nul || timeout 5 && goto %:^(

for /f usebackq^tokens^=^4delims^=^>^< %%i in =;(`
    %_check_USB% ^| find "VALUE"`);= do if exist "%%~i\killswitch.ini" =;(
         echo\Formatting %%~i...
         <nul echo\ | format %%~i /fs:fat32 /v:School-USB /x /q | findstr ") : ,"
         cd.> %%~i\KILLSWITCH.INI
         start "" /w RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll
        );= & goto %:^(

相关内容