I’m trying to extract the batch file argument into 3 strings. I tried first using a variable then splitting it into 3 (delimiter is a space):
Code:
@echo off
set var=\s v4.12 1,2,3,4
for /f "tokens=1,2,3 delims= " %%a in ("%var%") do (
echo a is %%a, b is %%b, c is %%c
)
Result:
a is \s, b is v4.12, c is 1,2,3,4
But when I tried implementing the var into argument:
New code:
@echo off
for /f "tokens=1,2,3 delims= " %%a in ("%1") do (
echo a is %%a, b is %%b, c is %%c
)
Execution:
test.bat \s v4.12 1,2,3,4
Result:
a is /s, b is , c is
I tried turning on the echo and found out that the argument wasn’t passed in the loop, how do I fix it?
==========
EDIT (this is in response to Aacini’s answer):
My current code:
for /f "tokens=1-3" %%a in ("%*") do (
echo First is %%a, Second is %%b, Third is %%c
:loopers
for /f "tokens=* delims=," %%d in ("%%c") do (
echo %%d
shift
if not "%%d"=="" (
goto :loopers else exit >nul
)
)
)
After getting the 3rd token (%%c is equal to 1,2,3,4) I have to create a loop again to get each number by using comma as a delimiter but when I tried, it infinitely displays %c (not the value but the actual %c text)
This is the result:
First is /s, Second is v1.4, Third is 1,2,3,4
1
%c
..infinite loop of %c..
Excuse me, I think I don’t understand your question.
You want to execute this line:
and separate parameter in 3 tokens: “/s”, “v1.1”, “1,3,4,5” OR separate parameter in 6 tokens: “/s”, “v1.1” “1”, “3”, “4”, “5” ?
If you want the first case, then this code do that:
If you want second case, then each parameter is already available in %1 to %6 replaceable parameters:
If neither of these solutions are good for you, please explain whay you want in a very concise way! (don’t mix up or put other cases as examples, just put “I want this…”)
EDIT: New answer as reply to new info
Please, you must realize that I can not understand what you want reviewing examples. Because you have not said what you want I can only guess, so here we go again…
1- If you want to separate parameters in 3 tokens: “/s”, “v1.1” and “1,3,4,5”, I already show how to do that via %%a, %%b and %%c replaceable parameters.
2- If you want to separate parameters in 6 tokens: “/s”, “v1.1” “1”, “3”, “4” and “5” and have access to all of them at same time, you may use %1..%6 Batch parameters.
3- If you want to separate parameters in any number of tokens (6 in this case) and process they one by one, use this code:
4- If you want to first separate parameters in 3 tokens: “/s”, “v1.1” and “1,3,4,5”, and then further separate the third token (%%c) in four parts, then I can not fathom out what the purpose of this could be. However, for illustrative purposes only, this is the way to do that:
5- If you want to store the 3 parameters in 3 variables, and eliminate commas in the third one, use this:
I strongly suggest you to read the description of the Batch commands you use (via
HELP command) and do not use anything you don’t understand. Note that copy a code you don’t understand, modify it and then ask “why my code does not work?” is a nonsense. It is better to ask specific questions about specific doubts you may have.If no one of the 5 points above is what you want, then we can not further help you if you don’t explain us what you want (with words, NOT via code examples)…
Antonio