1

What I'm aiming for is to assign strings,each from a different line(from a txt file),to each its own variable.

example of the txt:

1
2
3

I tried doing it by assigning type output of the txt file on a variable with the for command

FOR /F "delims=" %%i IN ('type "textfile.txt"') DO set testingVariable=%%i

%testingVariable% being the variable,I then tried string manipulation like this

set newVariable=%testingVariable:~3,1%

hoping it would come out as 2, the only results I had were either 0 on all 3 numbers or just nothing.

Is there a simple a simple solution to this?

(and if possible try to explain as much as you can as i am still somewhat of a beginner)

1 Answers1

1

You want every line into a seperate variable? You need a counter:

setlocal enabledelayedexpansion
set c=0
FOR /F "delims=" %%i IN ('type "textfile.txt"') DO (
  set /a c+=1
  set testingVariable[!c!]=%%i
)
set testingvariable[

... and you need delayed expansion

Note: emty lines are skipped

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Could you explain what the enabledelayedexpansion does here and also why i need it?because as i have stated i am somewhat of a beginner and i do not understand what the delayed expansion thing does and why i would need it –  Apr 19 '16 at 19:25
  • There is a short description in the [linked answer](http://stackoverflow.com/a/30284028/2152082). If you want a deeper insight, look at the links in the comments there. Shoud be enough to let your head smoke. – Stephan Apr 19 '16 at 19:59
  • may i ask why that last line ends with a square bracket? –  Apr 21 '16 at 21:06
  • just a habit. `set user` shows all variables starting with `user`, like `userdomain`, `username`, `userprofile`. `set testingvaraible[` shows all variables starting with `testingvaraible[`. `testingvariable` should be unique enough without the `[`, but other (shorter) variable names may not be unique enough, so I use to expand the names as far as possible - as I said: just a habit. – Stephan Apr 21 '16 at 22:04
  • Oh! ok at first i didnt realize the last line was to actually list the variables beginning with "testingvariable[" thanks a lot ! –  Apr 21 '16 at 23:32