0

My arqtext.txt has the following dataset:

A,B,C,
(123 or 456) and (789 or 012),1,5,
(456 or 654) and (423 or 947),3,6,
(283 or 335) and (288 or 552),2,56,

I want to change the 1st column of the last 3 rows to a new string set in the script, with the result like:

A,B,C,
roi1,1,5,
roi2,3,6,
roi3,2,56,

But my code only output the header "A,B,C,":

@echo off 
setlocal EnableDelayedExpansion EnableExtensions

set roi1="(123 or 456) and (789 or 012)"
set roi2="(456 or 654) and (423 or 947)"
set roi3="(283 or 335) and (288 or 552)"

set /p "header="<"arqtext.txt"
echo %header%>arqtextnovo.txt

for /f "skip=1 tokens=1,* delims=," %%a in ("arqtext.txt") do (
    if %%a=="roi1" (
        echo roi1,%%b>>arqtextnovo.txt
    )
    if %%a=="roi2" (
        echo roi2,%%b>>arqtextnovo.txt
    )
    if %%a=="roi3" (
        echo roi3,%%b>>arqtextnovo.txt
    )
)
rem EXIT /B
pause>nul
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 2
    ```%%a``` is never going to ```==``` ```"roi1"```, ```"roi2"```, or ```"roi3"```. Not least because you already told us that it will be ```"(123 or 456) and (789 or 012)"```, ```"(456 or 654) and (423 or 947)"```, and ```"(283 or 335) and (288 or 552)"```. Perhaps you should be using the variables you defined instead! ```if "%%~a" == %roi1%```, ```if "%%~a" == %roi2%```, and ```if "%%~a" == %roi3%```. Then you'd need to ```echo``` ```%roi1%,%%b```, ```%roi2%,%%b``` and ```%roi3%,%%b```, or ```!roi1!,%%b```, ```!roi2!,%%b``` and ```!roi3!,%%b```. – Compo Aug 31 '22 at 21:50
  • 2
    Please however be very careful with the positioning of your redirection handles. Use either ```>>arqtextnovo.txt echo...``` or this syntax instead, ```(echo %roi1%,%%b)>>arqtextnovo.txt```. The former would probably be simpler for you. – Compo Aug 31 '22 at 21:54
  • 2
    In addition to the comments already posted, `set roi1="(123 or 456) and (789 or 012)"` will **include** the `"` in the value set. You need to use `set "roi1=(123 or 456) and (789 or 012)"` and then `if "%%a"=="%roi1%". – Magoo Aug 31 '22 at 23:34

1 Answers1

0

This is the way I would do it:

@echo off
setlocal EnableDelayedExpansion

set "roi[(123 or 456) and (789 or 012)]=1"
set "roi[(456 or 654) and (423 or 947)]=2"
set "roi[(283 or 335) and (288 or 552)]=3"

set /P "header=" < "arqtext.txt"

> arqtextnovo.txt (

   echo %header%

   for /f "usebackq skip=1 tokens=1,* delims=," %%a in ("arqtext.txt") do (
       echo roi!roi[%%a]!,%%b
   )

)

rem EXIT /B
pause>nul
  • The for /F command requires "usebackq" option if the filename is enclosed in quotes. Otherwise, it process the literal string enclosed in quotes.
  • It is more efficient to (redirect the whole output) > to a file, instead of append >> every new line. This also avoid the problems of redirect lines that ends in numbers.
  • If you have several values and want to select a result value based on the first one, it is much simpler and more efficient to use an array instead of test each individual value.

Let's suppose this method:

set /P "selector=Enter selector: "
if "%selector%" equ "nine" set result=9
if "%selector%" equ "seven" set result=7
if "%selector%" equ "five" set result=5

Instead, you may define an array called "value":

set "value[nine]=9"
set "value[seven]=7"
set "value[five]=5"

... and then directly get the result value this way:

set "result=!value[%selector%]!"

The same method is used in this code. However, you have not specified what happen if the input value is not one of the array elements.

For a further description on array management in Batch files, see this answer

Aacini
  • 65,180
  • 12
  • 72
  • 108