0

Hopefully something simple.....

In a batch file, I want CMD to prompt a user for the number of project names they want to enter:

Example:

Enter number of projects:

Next if the user entered 2 they should be prompted to enter in the study project numbers:

Example:

  • Enter first project number: PROJ001
  • Enter second project number:PROJ002

Finally after they enter in all their project numbers for however many projects, there should be one new variable that contains them all in the form of:

Example:

'PROJ001','PROJ002'

So if they only entered 1 then the final variable that I would need would be 'PROJ001' but if they entered 2 or 3 or 4 etc. then it would be 'PROJ001','PROJ002 etc.

How input is taken I will leave up to you, for me it would be easiest if just:

Example:

Enter projects numbers separated by a space:

The the code would string these all together.

I do see some replies to this looking to close this post.....Yes I call Windows Command line DOS

I have tried this myself but my knowledge is limited.

For example

I have

set /p proj1=Enter Project:
set /p proj2=Enter Project:
set /p proj3=Enter Project:

set newvar='%proj1%','%proj2%','%proj3%'

However this would require constant updates if users wanted to add more than 3, and also one like to collect the data would be better.

Hopefully this clarifies the issue a bit more.

5
  • 2
    Are you really using DOS in 2020 or is it the Windows Command Line? What have you tried? Where are you stuck? Show us your research.
    – gronostaj
    Jun 4, 2020 at 14:35
  • IF statement found here ss64.com/nt/if.html might be a good place for you to start.
    – vssher
    Jun 4, 2020 at 14:39
  • 4
    This is not a free script-writing site. What have you already tried?
    – harrymc
    Jun 4, 2020 at 14:40
  • 2
    I’m voting to close this post because the poster should have done some initial study of the question before asking.
    – harrymc
    Jun 4, 2020 at 14:41
  • I have updated my initial post @gronostaj
    – Colm
    Jun 4, 2020 at 15:32

1 Answer 1

0

with delayed expansion input can be stored as list and array, allowing simple access to individual elements of the list using an index number. the below stops taking input when no input is provided.

@Echo Off & Setlocal EnableDelayedExpansion
:input
Set /A i+=1 & Set /P "Project[!i!]=Enter project ID: "& For %%i in (!i!) Do IF "!Project[%%i]!" == "" (Goto :Next) Else Goto :input
:Next
For /L %%i in (1,1,%i%) Do If not "!project[%%i]!" == "" Set "ProjectList=!ProjectList!,!project[%%i]!"
Set Project
Exit /B
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .