Пакетный файл для создания папки на каждом съемном устройстве

812
Marcos Eduardo Lustosa

Мне нужен командный файл для создания папки «test» на каждом съемном устройстве, чем я подключаю свой компьютер (pendrives и т. д.), я пытаюсь что-то вроде этого

set "extDrive=" for /f %%D in ('wmic volume get driveLetter^ where drivertype=2 ) do set extDrive=%%D mkdir "%extDrive%\test" 

Кто-то может сказать мне, почему это не работает ??

1

2 ответа на вопрос

1
DavidPostill

Why is this not working?

'wmic volume get driveLetter^ where drivertype=2

You have several errors in the above wmic command:

  • drivertype should be drivetype.
  • There is a missing trailing '.
  • The `^ is not needed.
  • The where should come before the get.
  • Not all disk volumes have drive letters (but logical disks do).

Use the following batch file:

@echo off setlocal for /f "skip=1 tokens=1,2" %%d in ('wmic logicaldisk get caption^, drivetype') do ( if [%%e]==[2] echo mkdir %%d\test ) endlocal 

Notes:

  • Remove the echo before mkdir when you have tested the batch file.
  • You may need to do something with drive type 3 as well.
  • I have a removable drive of type 3. In the below output C: is a fixed hard disk, D: is my CD ROM drive, E: is a removable USB stick and F: is a removable external USB drive.

    F:\test>wmic logicaldisk get deviceid, drivetype DeviceID DriveType C: 3 D: 5 E: 2 F: 3 

Further Reading

0
Khaos

to get the drive letter of a removable drive use:
wmic logicaldisk where drivetype=2 get deviceid
in my case this returns:

C:\Windows\system32>wmic logicaldisk where drivetype=2 get deviceid
DeviceID
E:

i would put this to a txt file, and then loop through that text file... Im not a big batch programmer, this way is easier to debug for me. :)