mellimik Posted December 31, 2007 Share Posted December 31, 2007 Hey, I've been wondering can I define variable that contains words separated with spaces, then use FOR to go through the string and echo every token?@echo offREM DEFINE THE HOSTS THAT SHOULD BE SKIPPED (SEPARATE WITH SPACE)set DISCARD=DC1 DC2 MGWFOR /F "delims= " %%a IN ('@echo %DISCARD%') DO @( echo DISCARDED : %%a)but this only echoes the word before the first space.C:\>skip.cmdDISCARDED : DC1C:\>I know that I could use a text file containing all the words separated with new lines, but for the sake of simplicity I want to define everything inside one variable. I understand that using the "tokens=" parameter I can capture the amount I want from the line, but I just want to echo every token from the line and do something with them.This is what I would like the output to be:C:\>skip.cmdDISCARDED : DC1DISCARDED : DC2DISCARDED : MGWC:\>Is this possible? Link to comment Share on other sites More sharing options...
gunsmokingman Posted December 31, 2007 Share Posted December 31, 2007 Cmd Promt is not my best langauge, but to do what you want would be simple in VBS script.Save As Demo.vbsDim D1, D2, DISCARD DISCARD = Array("DC1", "DC2", "MGW") For Each D1 In DISCARD D2 = D2 & "DISCARDED : " & D1 & vbCrLf Next MsgBox D2,4128,"Finish" Link to comment Share on other sites More sharing options...
Yzöwl Posted December 31, 2007 Share Posted December 31, 2007 @ECHO OFF&SETLOCAL:: DEFINE THE HOSTS THAT SHOULD BE SKIPPED (SEPARATE WITH SPACE)SET DISCARD=DC1 DC2 MGWFOR %%a IN (%DISCARD%) DO ECHO/DISCARDED : %%aPING -n 6 127.0.0.1>NUL Link to comment Share on other sites More sharing options...
mellimik Posted January 2, 2008 Author Share Posted January 2, 2008 @ECHO OFF&SETLOCAL:: DEFINE THE HOSTS THAT SHOULD BE SKIPPED (SEPARATE WITH SPACE)SET DISCARD=DC1 DC2 MGWFOR %%a IN (%DISCARD%) DO ECHO/DISCARDED : %%aExactly what I was looking for. Thank you! I just don't get what is the difference between FOR %%a IN (%DISCARD%) and FOR %%a IN ('echo %DISCARD%') Link to comment Share on other sites More sharing options...
phkninja Posted January 2, 2008 Share Posted January 2, 2008 Th difference isFOR %%a IN (echo %DISCARD%)echo is a print command, so basically you are asking it to break down something being printed (how do you break down a command???) e.g. for <every part> of (print <variable>) is what you instructed it to doFOR %%a IN (%DISCARD%)break down the variable it its commpoent partse.g. for <every part> of (<variable>) is what you instructed it to do Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now