0

I'm currently using batch for this code. I need to make a script that will ask a user how many times a loop needs to be run. In this loop the program will ask the user for numbers, and these numbers need to be added to a global array. I'll post some pseudocode.

@echo off
set /p d=How many times will this loop?
cls 
for /L %%a in (1,1,%d%) do (
    set /p code=Input code: 
    Set codes[%%a]=!code! 
)

my goal with this is to get it to paste all of the codes in a .txt file after it has run through the desired amount of loops.

resulting .txt file would look something like this:

input1
input2
input3
input4

so on and so forth. There would be as many inputs in the .txt as there were loops in the beginning.

  • just missing an empty line between content and code :) – Jonas Wilms Mar 15 '18 at 21:20
  • `but I would be open to any languages for this code.` -> please limit that to ONE language. And could you show us a sample resulting txt? Not everyone speaks bash – Jonas Wilms Mar 15 '18 at 21:25
  • 3
    For the posted code, you need to enable delayed expansion at the top of your script, use exclamation marks instead of percent characters for the variable in the code block and remove the whitespace either side of the equals character. – Compo Mar 15 '18 at 21:25
  • enable delayed expansion? I'm not familiar. – Casey Whitener Mar 15 '18 at 21:28
  • 1
    @CaseyWhitener, it is probably the #1 isssue with batch files on SO. This problem crops up every day and usually multiple times a day. You are actually using the variable `!code!` with delayed expansion but you do not have delayed expansion enabled. You need to do that with a `SETLOCAL` command. `SETLOCAL ENABLEDELAYEDEXPANSION` – Squashman Mar 15 '18 at 22:19
  • 3
    You may directly use just this line inside the `for /L` loop: `set /p codes[%%a]=Input code: `; there is no reason to use two lines for this assignment. I suggest you to read [this answer](https://stackoverflow.com/a/10167990/778560) about array management... – Aacini Mar 16 '18 at 02:50
  • 2
    Why do you need an array for this after all? why not simply writing each input to the text file immediately? – aschipfl Mar 16 '18 at 09:26

0 Answers0