Method of splitting a single-line string using the for command in CMD

Source: Internet
Author: User

We all know that the for command in CMD is a loop command, and the data source can be a file, a result of a command, or a string, only 3 of these sources

If it's a file, loop through all the strings of the file.

If it is a command result, then each line of the result of this command is processed in a loop

If it is a string, then the string is processed only once

such as: for/f ["Options"]%variable in ("string") do command [Command-parameters]

Even the ability to use the "delims=[character] tokens=x" is simply to pass an item that is split from the current line into a loop statement

Instead of looping through a line of strings one by one, you want to implement a string similar to that in C #. Split (' character ') function

Finally, the method is to use the Goto statement to force the loop of the entire for statement method to implement

The code is as follows:

@echoOff&title String CuttingSetstring=aaa/bbb/CCC: SplitREMdivide the string to be cut by the/symbol and get the first value after the splitREMRe- assign the remaining value of the partition to the environment variable string and execute the forREMFirst time%string%=aaa/bbb/ccREMsecond time%STRING%=BBB/CCCREMthird time%STRING%=CCCREMfourth time%string%= empty, end Goto, Exit for/F "tokens=1,* delims=/"%%i in ("%string%") Do(Echo%%iSetstring=%%j)if  not"%string%" = = ""GotoSplitPause


Operation Result:

--Originally published in 2012-3-11 08:57

To cut a string with a loop-only for command, which is purely artifice, the function of batch processing in CMD is very weak, but thanks to its very loose syntax, you can play a variety of tricks

That's a big part of why I like scripting languages. :)

Use the for command to split a single-line string in cmd

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.