MSFN Forum: For loop: split string and echo every token - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

For loop: split string and echo every token Rate Topic: -----

#1 User is offline   mellimik 

  • Newbie
  • Group: Members
  • Posts: 46
  • Joined: 04-November 06

Posted 31 December 2007 - 06:43 AM

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 off

REM DEFINE THE HOSTS THAT SHOULD BE SKIPPED (SEPARATE WITH SPACE)
set DISCARD=DC1 DC2 MGW

FOR /F "delims= " %%a IN ('@echo %DISCARD%') DO @(
	
	echo DISCARDED : %%a
)


but this only echoes the word before the first space.

C:\>skip.cmd
DISCARDED : DC1

C:\>


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.cmd
DISCARDED : DC1
DISCARDED : DC2
DISCARDED : MGW

C:\>


Is this possible?


#2 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 31 December 2007 - 09:41 AM

Cmd Promt is not my best langauge, but to do what you want would be simple in VBS script.
Save As Demo.vbs

Quote

Dim D1, D2, DISCARD 
 DISCARD = Array("DC1", "DC2", "MGW")
  For Each D1 In DISCARD 
   D2 = D2 & "DISCARDED : " & D1 & vbCrLf 
  Next 
 MsgBox D2,4128,"Finish"


#3 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 31 December 2007 - 10:44 AM

@ECHO OFF&SETLOCAL
:: DEFINE THE HOSTS THAT SHOULD BE SKIPPED (SEPARATE WITH SPACE)
SET DISCARD=DC1 DC2 MGW
FOR %%a IN (%DISCARD%) DO ECHO/DISCARDED : %%a
PING -n 6 127.0.0.1>NUL


#4 User is offline   mellimik 

  • Newbie
  • Group: Members
  • Posts: 46
  • Joined: 04-November 06

Posted 02 January 2008 - 01:56 AM

View PostYzöwl, on Dec 31 2007, 06:44 PM, said:

@ECHO OFF&SETLOCAL
:: DEFINE THE HOSTS THAT SHOULD BE SKIPPED (SEPARATE WITH SPACE)
SET DISCARD=DC1 DC2 MGW
FOR %%a IN (%DISCARD%) DO ECHO/DISCARDED : %%a


Exactly 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%')

#5 User is offline   phkninja 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 466
  • Joined: 28-February 05

Posted 02 January 2008 - 11:26 AM

Th difference is

FOR %%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 do

FOR %%a IN (%DISCARD%)
break down the variable it its commpoent parts
e.g. for <every part> of (<variable>) is what you instructed it to do

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy