📜 ⬆️ ⬇️

HOWTO: How to determine the letter of a CD / DVD in WinPE

Using WAIK, I encountered the following task - to determine the letter of the CD / DVD from which, in fact, Win PE booted. The few recommendations on blogs and forums boiled down to creating a short C program and then analyzing its completion code — a sensible, proven method.

One “but”: in administrative tasks I prefer to use scripting solutions - so I am not tied to the compiler, I can adapt the script to other tasks literally on my knees, without having additional tools at hand. Therefore, having spent some time on experiments, as a result, I obtained the necessary result with built-in tools:
  1. obtain data on volumes in the system from the utility diskpart
  2. filter out too much clearance
  3. find the required volume in the list by the tag or file system feature
Despite the apparent complexity, the code is short and universal: it adapts easily, say, to determine the drive letter of a flash drive.

Diskpart is essentially a command interpreter; to transfer a sequence of commands from a file to it, you must use the / s switch. And in a real project I had created separate command files for partitioning the disk; get the letters of the local HDD and, in fact, get the letters for a bootable DVD. But a separate command can be executed without additional files:
')
echo list volume | diskpart

The output will be something like:
 Microsoft Diskpart, version 6.0.6001
 (C) Microsoft Corporation 1999-2007.
 On the computer: test

 DISKPART>
     Volume ### Name Label FS Type Size Status Information
   ---------- --- ----------- ----- ---------- ------- ---- ----- --------
      Volume 0 E DVD-ROM 0 b No wears          
      Volume 1 F LRMCFRE_RU_ UDF DVD-ROM 2389 MB Good working           
      Volume 2 C NTFS Section 373 GB Good   
      Volume 3 D Data NTFS Section 373 GB Good
 DISKPART>


At first glance, the “Type” fields are quite enough for us, but in reality there may be several drives - it is better to focus on the file system or on a disk label. The label is more straightforward, but the script is less versatile. Because In the label, I usually use the build number or date, it turned out to be easier to focus on the file system. Minus - forgotten in the second disk drive can lead to unpredictable results.

Please note: the output format depends on how the command is transmitted to the interpreter — when using an external command file, strings
DISKPART>
no extradition.

To filter the list of volumes from unnecessary formatting and superfluous data, apply the extended syntax of the for command. Here we also have a choice: to transfer the result through the file, or by complicating the command syntax. Using additional files will give relatively easy-to-read code:

diskpart / s getvolumes.dp> volumes.txt
for / F "skip = 7 tokens = 3.5" %% i in (volumes.txt) do if %% j == UDF SET CDDRIVE = %% i

We just need to take care that the current directory allows the entry, perhaps specify the full paths for the files. But the complication of the syntax allows you to avoid the hassle of ways and write access:

for / F "skip = 8 tokens = 3.5" %% i in ( ' echo list volume ^ | diskpart ' ) do if %% j == UDF SET CDDRIVE = %% i

Actually, that's it - the only line of code will give us the letter of our boot disk. Just do not neglect the error handling, before using the variable it is worth checking:

if - % CDDRIVE% == - goto error

Source: https://habr.com/ru/post/49908/


All Articles