Batch for loop using wmic

unseen_rider

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?

JosefZ

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%

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

wmic uninstall using batch file

From Dev

Using wmic to get workgroup in a batch file

From Dev

Windows Batch: Set variables for FOR loop, WMIC diskdrive get size

From Dev

How to get the Percentage of memory utilization using WMIC in batch script?

From Dev

WMIC in for-loop

From Dev

Batch rename file using for loop

From Dev

Using if conditions at the end of a FOR loop in Batch

From Dev

Batch File FOR loop using variable

From Dev

Text garble in batch script for wmic command

From Dev

Batch Script: Merging the output of WMIC calls

From Dev

How to run wmic command in a batch file

From Dev

Get product name from WMIC for a variable in batch

From Dev

Combine Batch/WMIC + ANSI/UNICODE Output formatting

From Dev

Batch Script: Merging the output of WMIC calls

From Dev

How to treat the "No instance found" of WMIC command in batch?

From Dev

How to write a batch file to return a WMIC result

From Dev

Issues Renaming PC with WMIC in batch script

From Dev

loop through directory names using a batch file?

From Dev

How to execute If loop and find text using batch

From Dev

Loop on directories using a batch file and paste folders

From Dev

Endless loop in batch file using 'goto'

From Dev

How to Loop imacro script using batch file

From Dev

Endless loop in batch file using 'goto'

From Dev

Windows batch - Breaking out of a loop using variables

From Dev

How to execute If loop and find text using batch

From Dev

Using a for loop Windows batch executes twice

From Dev

Batch file FOR loop using incrementing variable

From Dev

Using update with batch method in a loop in Firestore

From Dev

Escaping strings when using wmic

Related Related

HotTag

Archive