DOS string parsing

June 24, 2009 at 02:44:48
Specs: Windows Vista
I and trying to write a batch file that looks at a list of servers and then pings each in turn to determine if they are contactable.

The list of servers is held in a variable and are delimited by and exclamation mark (!). An example would be
There may be more or less than three servers in the string.
How do I loop through each element of the server_string variable string to get the individual server names into a new variable called server so I can ping it?

So on loop 1 the output variable is set to server1, on the 2nd loop it is server2 etc. continuing until there are no more server names in the server_string variable.

I have been trying using the FOR command but cannot quite get the syntax.

Thank you very much for your kind help.


See More: DOS string parsing

Report •

June 24, 2009 at 04:16:36
Using the exclamation mark as delimiter is the worst choice as it prevents to enable "delayed expansion" for dynamic variables. Anyway a workaround is

set scan_list=%server_list%
for /F "tokens=1* delims=!" %%j in ('echo.%scan_list%') do (
  call :SUB %%j
  if "%%k"=="" goto :END
  call :NEXT %%k
goto :EOF

  set scan_list=%*
goto :EOF

  set server=%*
  [Here Your Batch]
goto :EOF

Report •
Related Solutions

Ask Question