Search code examples
variablesbatch-filefor-loopcmdwmic

Batch for loop using wmic


The output of the below

wmic logicaldisk get providername /value

using cmd (with limited user rights) on the computer I'm using is:

ProviderName=


ProviderName=


ProviderName=\\xxx-SN-NA01\Users2\N\ni


ProviderName=\\xxx-sn-na01\Software_Ca


ProviderName=\\xxx-sn-na01\TSO

I'm tring to get to the following stored in a variable named %drives_paths_wmic%:

\\xxx-SN-NA01\Users2\N\ni \\xxx-sn-na01\Software_Ca \\xxx-sn-na01\TSO

Then for the content of this to be transferred into the variable %file%.

The batch code I currently have is:

@echo off
setlocal enabledelayedexpansion 
for /F "tokens=2 delims='='" %%A in ('wmic logicaldisk get providername /value') do (
  echo %%A >> %drive_paths_Wmic%
)
echo %drive_paths_Wmic%
echo %drive_paths_Wmic% >> %file%

Edit: the problem is that nothing gets stored in the %drive_paths_Wmic% variable.

Since the batch file would need to be run on Windows 7 and XP computers, I can't use PowerShell. Please advise changes to the batch code in order to do this?


Solution

  • You do not specify a problem. However:

    '1'. delims== syntax

    for /F "tokens=2 delims==" %%A in ('wmic logicaldisk get providername /value') do @echo %%A
    

    Is the output from above command right? I dare say not, because

    '2'. all output from wmic is Unicode. To convert to ASCII try next code snippet:

    wmic logicaldisk get providername /value >somename.txt
    for /F "tokens=2 delims==" %%A in ('type somename.txt') do @echo %%A
    

    '3'. Result to variable:

    set "file=" 
    rem variable initialized now
    rem ... some code ...
    for /F "tokens=2 delims==" %%A in ('type somename.txt') do set file=!file! %%A
    rem ... some code ...
    echo !file!
    echo %file%
    

    or may be set "file=!file! %%A" as well

    Complete batch:

    @ECHO OFF >NUL
    @SETLOCAL enableextensions enabledelayedexpansion
    set "drive_paths_Wmic=" 
    rem variable initialized now
    rem ... some code ...
    wmic logicaldisk get providername /value >somename.txt
    for /F "tokens=2 delims==" %%A in ('type somename.txt') do (
      set drive_paths_Wmic=!drive_paths_Wmic! %%A
      @rem ... some code ...
      )
    echo !drive_paths_Wmic!
    echo %drive_paths_Wmic%
    Endlocal
    goto :eof
    

    Output (on my computer):

    C:\bat>fivmic
     \\pc1870a\ttemp \\PC1870A\Install
     \\pc1870a\ttemp \\PC1870A\Install
    

    Output (part with echo ON)

    C:\bat>fivmic
    C:\bat>wmic logicaldisk get providername /value  1>somename.txt
    C:\bat>for /F "tokens=2 delims==" %A in ('type somename.txt') do set drive_paths_Wmic=!drive_paths_Wmic! %A
    C:\bat>set drive_paths_Wmic=!drive_paths_Wmic! \\pc1870a\ttemp
    C:\bat>set drive_paths_Wmic=!drive_paths_Wmic! \\PC1870A\Install
    C:\bat>rem ... some code ...
    C:\bat>echo !drive_paths_Wmic!
     \\pc1870a\ttemp \\PC1870A\Install
    C:\bat>echo  \\pc1870a\ttemp \\PC1870A\Install
     \\pc1870a\ttemp \\PC1870A\Install
    

    And if %file% variable contains valid filename, then you could redirect echo to that file, e.g. echo bubu >> anyname.txt appends text bubu (on a new line) to file anyname.txt. In your script:

    set file="anyname.txt"
    echo %drive_paths_Wmic% >> %file%