Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

192.168.1.2; PC_NAME_1

192.168.1.3; 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 ( ....

Thanks

share|improve this question

1 Answer 1

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

Your Answer

 
discard

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.