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