I have a batch script which creates some arrays in order to store some WMI data regarding disk space in partitions, which I'd then like to loop through. It seems there is some problem with the way I'm trying to take advantage of the delayed expansion.
First of all I created 3 arrays for partition name, free space and total space like this:
SET i=0
SET j=0
SET k=0
FOR /F "tokens=1,2 delims== usebackq" %%A IN (`WMIC %remoteaccess% Path Win32_PerfRawData_PerfDisk_LogicalDisk Get Name^,FreeMegabytes^,PercentFreeSpace_Base /VALUE`) DO (
IF /I %%A EQU Name SET "names[!i!]=%%B"
IF /I %%A EQU Name SET /a i=i+1
IF /I %%A EQU FreeMegabytes SET "dfm[!j!]=%%B"
IF /I %%A EQU FreeMegabytes SET /a j=j+1
IF /I %%A EQU PercentFreeSpace_Base SET "dfb[!k!]=%%B"
IF /I %%A EQU PercentFreeSpace_Base SET /a k=k+1
)
This works fine. But later on I'm attempting to display it as XML, using a loop to go through each as so (%calcaddress%
is a link to a calculator batch file):
SET l=0
FOR /F "tokens=2 delims==" %%s in ('set names[') DO (
ECHO ^<result^>
ECHO <title>%%s Disk Free^<title>
FOR /F "tokens=* usebackq" %%A IN (`"%calcaddress% round0 !dfm[%l%]!*1024*1024"`) DO ECHO ^<Value^>%%A^</Value^>
ECHO ^</result^>
SET /a l=l+1
)
The problem is here that the value of %l%
seems to always be 0
on each iteration, so it doesn't work. I tried changing it to !dfm[!l!]!
and probably all variations but I can't get any to work. I do also have ECHO off
and SETLOCAL EnableDelayedExpansion
at the top of the file.