VBScript: please read Rob Haupt's five VBScript 'best practices' and Rob van der Woude's Debugging Your Scripts; in short:
Batch script: please read Rob van der Woude's Debugging your batch files.
Why not to stay in a batch script for all given task? Let's apply wmic
command: the Win32_Volume class (represents an area of storage on a hard disk) or the Win32_LogicalDisk WMI class (represents a data source that resolves to an actual local storage device on a computer system running Windows).
Try yourself *** see edit below:
wmic path Win32_Volume get BlockSize, DriveLetter, DriveType, Label
wmic path Win32_LogicalDisk get DeviceID, DriveType, Description, VolumeName, FileSystem
rem or full output:
wmic path Win32_Volume get /value
wmic path Win32_LogicalDisk get /value
Here's possible skeleton of your batch script (put it together with your usb.bat
script yourself):
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_drives="
for /F "tokens=*" %%G in ('
wmic path Win32_LogicalDisk where "DriveType=2" get DeviceID /value ^|findstr "="
') do for %%g in ("%%~G") do (
set "__%%~g"
echo(
rem echo going to process drive __%%~g
call :processDrive
)
ENDLOCAL
goto :eof
:processDrive
echo processing drive %__DeviceID%
pushd "%__DeviceID%\"
2>NUL dir /S /B /A *.ini
2>NUL dir /S /B /A *.lnk
popd
goto :eof
Here the for
loops are
%%G
to retrieve the DeviceID
value;
%%g
to remove the ending carriage return in the value returned: wmic
behaviour: each output line ends with 0x0D0D0A
(<CR><CR><LF>
) instead of common 0x0D0A
(<CR><LF>
).
See Dave Benham's WMIC
and FOR /F
: A fix for the trailing <CR>
problem
Output:
==> D:\bat\SF\786392.bat
processing drive F:
F:\Shortcut.lnk
F:\vbScriptDoc\Hey_Scripting_Guy.lnk
processing drive G:
G:\SPSS\admin\SCRIPTS.INI
G:\SPSS\admin\system32\GroupPolicy\GPT.INI
G:\VB_scripts\Net\nethood_create_a_link.vbs.lnk
==>
Further resources (required reading for a batch sripter):
*** Edit: unfortunately, querying DriveType
property in both Win32_Volume
and Win32_LogicalDisk
wmi classes could give false results, see next output where both F:
and G:
are USB removable media so that DriveType
property should be 2:
==> wmic path Win32_LogicalDisk get DeviceID,DriveType,Description,VolumeName, FileSystem,Size
Description DeviceID DriveType FileSystem Size VolumeName
Local Fixed Disk C: 3 NTFS 119664537600
Local Fixed Disk D: 3 NTFS 1000202039296 DataDisk
CD-ROM Disc E: 5
Removable Disk F: 2 FAT 519274496 KINGSTON
Local Fixed Disk G: 3 FAT32 500044136448 GOG
==> wmic path Win32_Volume get BlockSize, DriveLetter, DriveType, Label, Capacity
BlockSize Capacity DriveLetter DriveType Label
4096 1000202039296 D: 3 DataDisk
4096 366997504 3 Rezervováno systémem
8192 519274496 F: 2 KINGSTON
65536 500044136448 G: 3 GOG
4096 119664537600 C: 3
E: 5
You need to combine next WMI/WMIC queries to get right drive letters of removable disks:
==> wmic path Win32_DiskDrive get DeviceID, InterfaceType, MediaType
DeviceID InterfaceType MediaType
\\.\PHYSICALDRIVE1 IDE Fixed hard disk media
\\.\PHYSICALDRIVE2 USB Removable Media
\\.\PHYSICALDRIVE0 IDE Fixed hard disk media
\\.\PHYSICALDRIVE3 USB External hard disk media
==> wmic path Win32_DiskDriveToDiskPartition get /value
Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE1""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #0""
Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE1""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1""
Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE2""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0""
Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE0""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0""
Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE3""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #0""
==> wmic path Win32_LogicalDiskToPartition get /value
Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="C:""
EndingAddress=120032591871
StartingAddress=368050176
Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="D:""
EndingAddress=1000203091967
StartingAddress=1048576
Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="F:""
EndingAddress=519569407
StartingAddress=16384
Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="G:""
EndingAddress=500105249279
StartingAddress=32256