2

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.

Azeem
  • 11,148
  • 4
  • 27
  • 40
Sage00
  • 31
  • 6
  • 2
    You cannot nest delayed expansion; neither can you use immediate `%`-expansion in the loop, of course; however, you can use a `for` loop, like `for %%i in (!l!) do echo/!dfm[%%i]!`, to achieve another delay of expansion, which happens still earlier than delayed `!`-expansion. Note that you forgot to escape some of the `<` and `>` characters in your `echo` command lines... – aschipfl Oct 10 '17 at 09:08
  • https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990 – Aacini Oct 10 '17 at 12:37

0 Answers0