0

I have these two commands

C:\Users\xyz\AppData\Local\Temp>WMIC LOGICALDISK where volumename="abc" get deviceid>%temp%/path.txt

which creates file path.txt with content:

DeviceID
F:      

the file has lots of additional spaces which I have added in the above file^

I then use (although I know I could use the | char I am trying to avoid it) the command C:\Users\xyz\AppData\Local\Temp>FINDSTR [:] path.txt>path2.txt which gives the output F : which is incredibly frustrating as I obviously just need the "F"/alphabetic char that would be in its position and store it in a variable.

Could someone please show me how to do this - or ultimately get the drive letter of a usb device using cmd only and store it in a variable?

I don't mind if it takes a lot of lines just no | char please or quote marks " :)

edit: it MUST be in command prompt and can be written line by line (no cop and paste commands) and I need the individual letter (eg. F:) by itself - stored in a variable or the clip board. and obviously, no pipe character or quote marks pls :)

kidders
  • 13
  • 6
  • 1
    Does this answer your question? [Detecting Removable drive letter in CMD](https://stackoverflow.com/questions/31356732/detecting-removable-drive-letter-in-cmd) – JosefZ Apr 05 '22 at 16:17
  • There can be used __in a batch file__ `for /F "tokens=2 delims==:" %%I in ('%SystemRoot%\System32\wbem\wmic.exe LOGICALDISK where VolumeName^="abc" GET DeviceID /VALUE 2^>nul') do set "DriveLetter=%%I"` to (re)define the environment variable `DriveLetter` with the just the letter of the drive of the volume with name `abc`. Replace all `%%` by just `%` for usage in a command prompt window. – Mofi Apr 05 '22 at 17:32
  • For a full explanation of a similar command line see [How to get the drive letter of a drive with a specific drive name on Windows?](https://stackoverflow.com/a/62830106/3074564) – Mofi Apr 05 '22 at 17:46

1 Answers1

0

No | char please :) I think you mean the pipe character | (U+007C, Vertical Line)?

Batch file (set a desired string to the _volume variable instead of my debugging value Elements):

@ECHO OFF
@SETLOCAL
rem set the variable _volume and delete the variable _letter
set "_volume=Elements"
set "_letter="

for /F "tokens=2 delims==:" %%G in ('
  WMIC LOGICALDISK where volumename^="%_volume%" get deviceid/Value
') do set "_letter=%%G"

echo letter of volume "%_volume%" is "%_letter%"    

Output: .\SO\71753240.bat

letter of volume "Elements" is "F"

Command prompt (copy and paste the following code snippet into an open cmd.exe window):

set "_volume=Elements"
set "_letter="
for /F "tokens=2 delims==:" %G in ('WMIC LOGICALDISK where volumename^="%_volume%" get deviceid/Value') do @set "_letter=%G"
@echo letter of volume "%_volume%" is "%_letter%"    

Note that wmic output redirected to a file is UTF-16-BOM:

WMIC LOGICALDISK where volumename="%_volume%" get deviceid>%temp%\path71753240.txt
hexdump.exe -H %temp%\path71753240.txt
000000  ff fe 44 00 65 00 76 00 69 00 63 00 65 00 49 00  ..D.e.v.i.c.e.I.
000010  44 00 20 00 20 00 0d 00 0a 00 46 00 3a 00 20 00  D. . .....F.:. .
000020  20 00 20 00 20 00 20 00 20 00 20 00 20 00 0d 00   . . . . . . ...
000030  0a 00
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • hi thanks for your response, i do indeed mean the pip character. however, I must be able to run it in command prompt not a batch file and it cant be a copy and paste job - my apologies I will edit the question now. I tried the bottom method you put in cmd and it side 'hexdump.exe' is not recognized as an internal or external command, operable program or batch file. - not sure if I have done something weird there. please advice :) – kidders Apr 06 '22 at 18:55
  • Just type `for /F "tokens=2 delims==:" %G in ('WMIC LOGICALDISK where volumename^="abc" get deviceid/Value') do @set "_letter=%G"`. And forget that note about `wmic` output redirected to a file… BTW, you could download (useful utility) `hexdump.exe` from https://www.di-mgt.com.au/hexdump-for-windows.html – JosefZ Apr 06 '22 at 20:59
  • omg it actually finally worked thank you so much :))) – kidders Apr 10 '22 at 15:54
  • im sorry but is there anyway you could do it without using the " quote character as well please? – kidders Apr 10 '22 at 19:42
  • @kidders *do it without using the `"` quote character* ```for /F usebackq^ tokens^=2^ delims^=^=^: %G in (`WMIC LOGICALDISK where volumename^='abc' get deviceid/Value`) do @echo _letter=%G``` returns `_letter=F` (see https://ss64.com/nt/for_cmd.html) – JosefZ Apr 11 '22 at 07:31