1

I have a simple batch to write to C:\Program Files (x86)\Data\ori.csv file the folowing information: division, originator name

@echo off

    
CHOICE /C NS /M  "Please Choose Division:"
echo.
if errorlevel 1 set division=A8-NN
if errorlevel 2 set division=A8-NS


:PROMPT
set /P ori= "Add %division% Originator? [(Y)=yes / (N)=No] "
IF /I "%ori%" NEQ "N" goto add (
) else (
goto exit
)

:add
set /p oriname= "Please Enter %division% Originator Name "
echo Division %division% Originator %oriname% has been Sucessfully added
echo %division%,%oriname% >>C:\%programfiles(x86)%\data\Ori.csv 
echo.

goto prompt

:exit
pause

the output of csv to be e.g.

A8-NN,Chris
A8-NN,Alfredo
A8-NS,Joe
A8-NN,Patrick
A8-NS,Ann
etc

the data of this .csv is gonna change every 2 months for the divisions (new people assigned in each division)

My problem is that i want in a seperate batch file from ori.csv file to read the data and for a specific division use the choice command to choose one originator

As far i have done this:

CHOICE /C NS /M  "Please Choose Division:"
    echo.
    if errorlevel 1 set division=A8-NN
    if errorlevel 2 set division=A8-NS

count=

for /f "tokens=1-20* delims=," %%a in ('type "C:\%programfiles(x86)%\data\Ori.csv"') do (
if %%a== ("%division%)
        set b = %%b
        set "count=!count!+1"
        echo %count% %%b
        )
)

What i tried to do is to the %count% variable store a number identifier and to the %b variable store the originator name. How can I use those two variables as input to a choice command?

dan1st
  • 12,568
  • 8
  • 34
  • 67
Chris Evan
  • 25
  • 4
  • 1
    You are missing a left parentheses to open the command block for the `IF` command. Also do not use spaces with the `SET` command. You created a variable named `b[space]` with a value of `[space]value`. – Squashman Apr 29 '21 at 19:08

1 Answers1

1

You'll need a slightly different approach. After some clarification in the comments.. I would recommended that you move towards set /p here, simply because we never know how many options there will be, you say max 20, but tomorrow suddenly there are 27, then what? So I will rather be safe than sorry :)

@echo off & set cnt=0
setlocal enabledelayedexpansion
for /f "usebackq tokens=1* delims=," %%a in ("%programfiles(x86)%\data\Ori.csv") do (
    if not defined %%a (
       set /a cnt+=1
       set "%%a=%%a"
       set "!cnt!=%%a"
       echo !cnt!. %%a
    )
)
set /p "oper=Please choose Division (1 to !cnt!): "

if not %oper% gtr !cnt! (
    set cnt=0
    for /f "usebackq tokens=1-20* delims=," %%a in ("%programfiles(x86)%\data\Ori.csv") do if "%%a" == "!%oper%!" set /a cnt+=1 & echo !cnt! %%b
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • I don't understand, can you please explain what you mean by the parameters must be the originators? – Gerhard Apr 29 '21 at 20:50
  • so you want to first read the file to get all the seperate entitities, then use them in the choice? in other words if the file contained `A8-1` `A8-2` and `A8-3` you want to read them in as options? – Gerhard Apr 29 '21 at 20:55
  • ok, so there are some issues when doing that. What amount of entries can there be in the file? in other words, how many choice can a user expect to have? – Gerhard Apr 29 '21 at 21:06