0

I am trying to get two parameters out of a single string using a batch script. I want to take a string, in this case %%a, and split it into two parts, based on where a comma is placed (essentially comma delimited). So, for example, a single string %%a with a value of "Parameter1, Parameter2", would be turned in two separate strings "Parameter1" and "Parameter2". How would I do this in batch?

for /f "delims=" %%a in (Text.txt) do (
echo %%a|find "#" >nul
if errorlevel 1 (

    for /f "delims=, tokens=1,2" %%x in (%%a) do (
        set "param1=%%x"
        set "param2=%%y"
    )

    echo Param1: %param1%
    echo Param2: %param2%
    echo.
)

The first loop is to ignore lines in the text file that contain #.

FyreeW
  • 357
  • 1
  • 5
  • 18
  • Yes, it is possible! **`;)`** – Aacini Dec 04 '15 at 17:50
  • @Aacini I am confused by that answer. What? – FyreeW Dec 04 '15 at 17:51
  • 2
    Your question is: "Is this possible in batch?". The answer is: "Yes". – Aacini Dec 04 '15 at 17:52
  • @Aacini I guess that answer is not wrong. I changed my question, since I am trying to learn how to do it. – FyreeW Dec 04 '15 at 17:54
  • 1
    Two points here: **1.** In my solution the value of %%a is enclosed in quotes: `for ... in ("%%a") do (`. **2.** You must change `%param1%` by `!param1!` and insert `setlocal EnableDelayedExpansion` command at beginning; for a further explanation of this point, see [this answer](http://stackoverflow.com/questions/20300102/batch-variable-inside-a-variable-not-working-when-called/20301404#20301404). – Aacini Dec 04 '15 at 20:54
  • Ok, I think it is working. Thank you! – FyreeW Dec 04 '15 at 20:57

2 Answers2

2
for /F "delims=, tokens=1,2" %%x in ("%%a") do (
   set "part1=%%x"
   set "part2=%%y"
)

EDIT: Example added

@echo off
setlocal

for %%a in ("Parameter1,Parameter2") do (
   echo Value of percent-percent-a: %%~a

   for /F "delims=, tokens=1,2" %%x in ("%%~a") do (
      set "part1=%%x"
      set "part2=%%y"
   )

)

echo Part1: "%part1%"
echo Part2: "%part2%"

Output:

Value of percent-percent-a: Parameter1,Parameter2
Part1: "Parameter1"
Part2: "Parameter2"

2nd EDIT

Excuse me; I apologize in advance for this edit, but I can't resist the temptation of post it...

When you post questions related to any programming language, but particularly in the case of Batch files, you should be very clear and describe the real problem you are trying to solve; otherwise the proposed solutions may work even worst than any other different code written with the complete specifications.

For example, if your question would be this instead: "I want to read a text file, ignoring lines that contain #, and get two comma delimited values into two separate variables", I would posted this solution:

@echo off
setlocal EnableDelayedExpansion

for /F "delims=, tokens=1,2" %%a in ('findstr /V "#" Text.txt') do (
   set "param1=%%a"
   set "param2=%%b"

   echo Param1: !param1!
   echo Param2: !param2!
   echo/
)

Also, when you report "errors in a solution" you should be very careful that the errors you are reporting were not introduced by yourself; otherwise you are only bother the people that waste their time trying to provide you a solution for free! :(

Aacini
  • 61,954
  • 12
  • 66
  • 99
  • May the `%%a` string contain quotes? Please, change `"%%a"` by `"%%~a"` to fix this problem... – Aacini Dec 04 '15 at 18:18
  • it does not contain quotes, and I am still getting the same error. – FyreeW Dec 04 '15 at 18:25
  • I added a small example. Please, run my example and compare it vs. your code. Perhaps there is a point you have not mentioned about the value of `%%a` – Aacini Dec 04 '15 at 18:38
  • it is correctly printing out the first part, but it is not getting the second value (shows up as blank for %%y). – FyreeW Dec 04 '15 at 19:06
  • Is the error in my code or in yours? If _your code_ fails, how can I have a clue of what is failing if I don't see your code? You may post the smallest code segment that show the error. Anyway, it seems that Stephan's answer solved your problem already, so I suggest you to just use such method... – Aacini Dec 04 '15 at 19:17
  • it solved part of it, but I am unable to figure out how to assign two variables that I can use. – FyreeW Dec 04 '15 at 19:58
  • I am afraid I don't follow you. In my code `part1` and `part2` are "two variables that you can use"! Isn't it **???** – Aacini Dec 04 '15 at 20:26
  • I added the code that I have right now into my post, hopefully that will help. – FyreeW Dec 04 '15 at 20:29
  • lol - the famous [delayed Expansion trap](http://stackoverflow.com/a/30284028/2152082) – Stephan Dec 04 '15 at 20:37
  • See my comment below your question. I don't understand how is possible that Stephan's answer "worked perfectly" with all these problems, as you said 2 hours ago! – Aacini Dec 04 '15 at 20:57
  • @Aacini answered too quickly. Forgot I needed to do more than just echo the two variables (it worked perfectly for that). I actually ended up using your solution to fix the issue. – FyreeW Dec 04 '15 at 21:03
1

for is the way to go:

for %%i in (%%a) do @echo %%i

adapted to your comments to Aacinis answer and your edited question:

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type text.txt ^|find /v "#"') do (
    set count=0
    for %%i in (%%a) do (
        set /a count+=1
        set "param!count!=%%i"
    )
    echo --!param1!--!param2!--
    set param
)
echo the Parameters are %param1% and %param2%

I used a different method of eliminating lines that contain # (less parantheses). If there are more than two Parameters, additional variables will be generated automatically (%param3%, %param4%,...)

Stephan
  • 50,835
  • 10
  • 55
  • 88