Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

is there a way to "split" the value of string line readed by a batch file ? Suppose to have this text file; PC_NAME_1; PC_NAME_3


I would like to read line 1, and split the value into two variables ... So i can use IP address, and also, Pc Name (for other purpose)... for example:

for /f %%x in (txtfile.txt) do ( ....


share|improve this question
up vote 2 down vote accepted

Yes, this can be done. A special FOR syntax is used

for /F "delims=," %A in (filename.txt) do call subbatch.bat %A %B %C %D %E

That'll split it just on a comma. But by default it'll split on space and tab. the 'tokens' term can specify how many of those you want to deal with

for /F "tokens=1,2,3,*" %A in (filename.txt) do call subbatch.bat %A %B %C "%D"

In this case %D will contain everything from the fourth delim and beyond.

The thing to keep in mind, though, is that "Do" is not a procedure block, it's a one-off call. This is where "goto" or "call" can be used to invoke further logic. My examples above call another batch-file and pass in the needed parameters as command-line options, so for those subbatch files the variables will be on %1 and %2.

share|improve this answer
You can also start a block with an opening parenthesis after the do. – Joey Mar 29 '11 at 13:39
You should not use goto after DO or it will break the loop. You are correct about using Call though. @Joey You are correct about using ( <code> ) after the DO block as long as the ( starts on the same line right after the DO. You can also CALL :label to run a sub-procedure within the same batch file. – CoveGeek Mar 7 at 21:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.