Page 1 of 1

wmic logicaldisk gives word output for drivetype instead of number (batch)

Posted: 28 Jan 2018 08:26
by LiveITA
I need to do stuffs when a network disk is found. To do so i need to look for drivetype 4 using this batch. The problem is that on my computer the drivetype for the network disk is "rete" (that means network in italian). How can i force wmic to output the number instead of the word?

Code: Select all

    for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
    if %%l equ 4 ( 
    stuff I make him do goes here
    )
    )  
Oddly it works well with drivetype 2, which means removable drive.

Re: wmic logicaldisk gives word output for drivetype instead of number (batch)

Posted: 28 Jan 2018 12:06
by aGerman
I assume it's rather an issue of the number of tokens. Just run

Code: Select all

wmic logicaldisk get caption,description,drivetype
in a CMD window. One or more adjacent spaces separate the tokens.

Run

Code: Select all

wmic logicaldisk get caption,description,drivetype /format:csv
This and setting the delimiter to a comma in your FOR /F loop may solve the problem.

Steffen

Re: wmic logicaldisk gives word output for drivetype instead of number (batch)

Posted: 28 Jan 2018 13:30
by LiveITA
I'm not really good with batch, if i set delim=, after tokens it does not work. What can i do?

Re: wmic logicaldisk gives word output for drivetype instead of number (batch)

Posted: 28 Jan 2018 14:12
by aGerman
It would have been good to know if the tests in the CMD prompt worked for you. Now I can only guess.

Code: Select all

@echo off &setlocal
for /f "skip=2 delims=" %%i in (
  '2^>nul wmic logicaldisk get caption^,description^,drivetype /format:csv'
) do for /f "tokens=2-4 delims=," %%j in ("%%i") do (
  if %%l==4 (
    echo Caption     %%j
    echo Description %%k
    echo ~~~~~~~~~~
  )
)
pause
Steffen

Re: wmic logicaldisk gives word output for drivetype instead of number (batch)

Posted: 28 Jan 2018 15:06
by LiveITA
Oh sorry
anyway it works perfectly, thanks :)

Re: wmic logicaldisk gives word output for drivetype instead of number (batch)

Posted: 28 Jan 2018 15:50
by LiveITA
Jusr wondering, will it work on an account without admin privileges?

Re: wmic logicaldisk gives word output for drivetype instead of number (batch)

Posted: 28 Jan 2018 17:36
by aGerman
Your WMIC command will work regardless if it was run elevated or not. But a network drive is always mapped for a specific user account. That means that another account on the same computer (e.g. the SYSTEM account) will not see the drives mapped for your account.

Steffen