# Solved: batch file reading a text file word for word



## vista_narvas (Nov 6, 2012)

hi im making a game in cmd 
iv come pretty far
but now iv come to level design
and i like to do this in a text file so i can write and know how it looks
instead of having every variable below each other
what i need is a batch file to read it every punctuation mark and put it in its own variable

(punctuation mark? google translate)

i was thinking a bout something that looks like this

```
[SIZE=3]set N=0
set D=0
echo >this4

:top 

[B](the code for reding the text file here 
using %N% to know where it need's to be
and %D% to know what file
with as out put T)[/B]


echo set N0%N%-%D%=%T%>>this4  
if {%N%}=={99} goto here
set /a N=%N%+1
goto top

:here 
set N=0
set /a D=%D%+1
if {%D%}=={100} exit[/SIZE]
```
%N% = a counter that go's to 99 than it adds 1 by %D%
%D% = also a counter if its 99 exit
N is used t count the pixels in 1 level there are 100 pixels in 1 level
D is used to count the levels there are 100 levels

i hope i gave enough info that you can understand and help


----------



## foxidrive (Oct 20, 2012)

Try this example - does it do what you need to do?


```
@echo off
(
echo.line 1
echo.line 2
echo.line 3
echo.line 4
echo.line 5
echo.line 6
echo.line 7
) > file.txt

setlocal enabledelayedexpansion
set c=0
for /f "delims=" %%a in (file.txt) do (
set /a c=c+1
set "NN[!c!]=%%a"
)
set nn
pause
```


----------



## vista_narvas (Nov 6, 2012)

foxidrive said:


> Try this example - does it do what you need to do?
> 
> 
> ```
> ...


i only get echo.line #
what i mean is it read's a text file with this in it

```
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
```
and puts out


```
set N00-0=#
set N01-0=#
set N02-0=#
set N03-0=#
set N04-0=#
set N05-0=#
set N06-0=#
set N07-0=#
set N08-0=#
set N09-0=#
set N010-0=#
set N011-0=-
set N012-0=-
set N013-0=-
set N014-0=-
set N015-0=-
etc
```


----------



## foxidrive (Oct 20, 2012)

Your array is 10 x 10 

It doesn't match what you say you want to output.


----------



## vista_narvas (Nov 6, 2012)

dint think about starting count from 0
but i think you get what i wand


----------



## foxidrive (Oct 20, 2012)

Yes, I see.

Try this with your data in the file:


```
@echo off
setlocal enabledelayedexpansion
set c=-1
for /f "delims=" %%a in (file.txt) do (
set /a c=c+1
set num=00!c!
set num=!num:~-2!
set line=%%a
for /L %%b in (0,1,9) do (
set N[!num!-%%b]=!line:~%%b,1!
)
)
set n
pause
```


----------



## vista_narvas (Nov 6, 2012)

foxidrive said:


> Yes, I see.
> 
> Try this with your data in the file:
> 
> ...


this works
but...

i dont know how it works so i just ask
now i get as output N[0-00]=#
but i like it to be N0%number%-%othernumber%
and that %number% go's to 99 and when it hits 99 it resets to 0 and adds 1 to the %othernumber%
till %othernumber% hits and exits

so if the input is 

```
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
```
that the out put will be

```
set N00-0=#
set N01-0=#
set N02-0=#
set N03-0=#
set N04-0=#
set N05-0=N
set N06-0=#
set N07-0=#
set N08-0=#
set N09-0=#
set N010-0=#
set N011-0=#
set N012-0=#
set N013-0=#
set N014-0=t
set N015-0=t
set N016-0=t
set N017-0=-
set N018-0=-
set N019-0=#
set N020-0=#
set N021-0=#
set N022-0=#
set N023-0=t
set N024-0=t
set N025-0=t
set N026-0=-
set N027-0=-
set N028-0=-
set N029-0=#
set N030-0=#
set N031-0=-
set N031-0=t
set N032-0=t
set N033-0=t
set N034-0=t
set N035-0=t
set N036-0=-
set N037-0=-
set N038-0=-
set N039-0=#
set N040-0=#
set N041-0=t
set N042-0=t
set N043-0=t
set N044-0=t
set N045-0=-
set N046-0=-
set N047-0=-
set N048-0=-
set N049-0=#
set N050-0=#
set N051-0=t
set N052-0=#
set N053-0=#
set N054-0=t
set N055-0=t
set N056-0=-
set N057-0=-
set N058-0=-
set N059-0=E
set N060-0=#
set N061-0=t
set N062-0=#
set N063-0=#
set N064-0=-
set N065-0=-
set N066-0=-
set N067-0=-
set N068-0=-
set N069-0=#
set N070-0=#
set N071-0=-
set N072-0=-
set N073-0=-
set N074-0=-
set N075-0=-
set N076-0=-
set N077-0=-
set N078-0=-
set N079-0=#
set N080-0=#
set N081-0=-
set N082-0=-
set N083-0=-
set N084-0=-
set N085-0=-
set N086-0=-
set N087-0=-
set N088-0=-
set N089-0=#
set N090-0=#
set N091-0=#
set N092-0=#
set N093-0=#
set N094-0=#
set N095-0=#
set N096-0=#
set N097-0=#
set N098-0=#
set N099-0=#

set N00-1=#
set N01-1=#
set N02-1=#
set N03-1=#
set N04-1=#
set N05-1=N
set N06-1=#
set N07-1=#
set N08-1=#
set N09-1=#
set N010-1=#
set N011-1=-
set N012-1=-
set N013-1=-
set N014-1=-
set N015-1=-
set N016-1=-
set N017-1=-
set N018-1=-
set N019-1=#
set N020-1=#
set N021-1=-
set N022-1=-
set N023-1=-
set N024-1=-
set N025-1=-
set N026-1=-
set N027-1=-
set N028-1=-
set N029-1=#
set N030-1=#
set N031-1=-
set N032-1=-
set N033-1=-
set N034-1=-
set N035-1=X
set N036-1=-
set N037-1=-
set N038-1=-
set N039-1=#
set N040-1=#
set N041-1=-
set N042-1=-
set N043-1=-
set N044-1=-
set N045-1=-
set N046-1=-
set N047-1=-
set N048-1=-
set N049-1=#
set N050-1=W
set N051-1=-
set N052-1=-
set N053-1=-
set N054-1=-
set N055-1=-
set N056-1=-
set N057-1=-
set N058-1=-
set N059-1=E
set N060-1=#
set N061-1=-
set N062-1=-
set N063-1=-
set N064-1=-
set N065-1=-
set N066-1=-
set N067-1=-
set N068-1=-
set N069-1=#
set N070-1=#
set N071-1=-
set N072-1=-
set N073-1=-
set N074-1=-
set N075-1=-
set N076-1=-
set N077-1=-
set N078-1=-
set N079-1=#
set N080-1=#
set N081-1=-
set N082-1=-
set N083-1=-
set N084-1=-
set N085-1=-
set N086-1=-
set N087-1=-
set N088-1=-
set N089-1=#
set N090-1=#
set N091-1=#
set N092-1=#
set N093-1=#
set N094-1=#
set N095-1=#
set N096-1=#
set N097-1=#
set N098-1=#
set N099-1=#
```
(isnt there a way to get a scroll bar?)
(this are the first 2 levels there are some other thing inside there but you know what i mean)


----------



## foxidrive (Oct 20, 2012)

Just one change. set num=!num:~-2! to set num=!num:~-3!

Try that:


```
@echo off
setlocal enabledelayedexpansion
set c=-1
for /f "delims=" %%a in (file.txt) do (
set /a c=c+1
set num=00!c!
set num=!num:~-3!
set line=%%a
for /L %%b in (0,1,9) do (
set N[!num!-%%b]=!line:~%%b,1!
)
)
set n
pause
```


----------



## vista_narvas (Nov 6, 2012)

foxidrive said:


> Just one change. set num=!num:~-2! to set num=!num:~-3!
> 
> Try that:
> 
> ...


the output with this is
N[000-0]=#
N[000-1]=#
N[000-2]=#
N[000-3]=#
etc

can that be
N00-0=#
N01-0=#
N02-0=#
N03-0=#
etc


----------



## foxidrive (Oct 20, 2012)

Oh I see. Try this:


```
@echo off
setlocal enabledelayedexpansion
set c=-1
for /f "delims=" %%a in (file.txt) do (
set /a c=c+1
set num=00!c!
set num=!num:~-2!
set line=%%a
for /L %%b in (0,1,9) do (
set N!num!-%%b=!line:~%%b,1!
)
)
set n
pause
```


----------



## vista_narvas (Nov 6, 2012)

foxidrive said:


> Oh I see. Try this:
> 
> 
> ```
> ...


it works good
but the 2 numbers arnt on the good place
N00-0=#
N00-1=#
N00-2=#
N00-3=#
N00-4=#
N00-5=#

need's to be

N00-0=#
N01-0=#
N02-0=#
N03-0=#
N04-0=#
N05-0=#

ect

N099-0=#
N00-1=#

if i flip set N!num!-%%b=!line:~%%b,1!
to set N%%b-!num!=!line:~%%b,1!
i get the same only the first 0 from !num! moved


----------



## foxidrive (Oct 20, 2012)

Explain how you want the cells numbered.

Cell 0, character 0

This is how it is now. 

N00-0


----------



## vista_narvas (Nov 6, 2012)

```
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
```
this are the pixels in 1 level
left to right is 0,1,2,3,4,5,6,7,8,9
below that 10,11,12,13,14,15,16,7,18,19
and more till 99
so i like to display the pixel first than the level

so if i have

```
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
```
than the first
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
is level 0
and will be
set N00-0=#
set N01-0=#
set N02-0=#
set N03-0=#
set N04-0=#
set N05-0=N
set N06-0=#
set N07-0=#
set N08-0=#
set N09-0=#
set N010-0=#
set N011-0=-
set N012-0=-
set N013-0=-
set N014-0=-
set N015-0=-
set N016-0=-
set N017-0=-
set N018-0=-
set N019-0=#
set N020-0=#
set N021-0=-
set N022-0=-
set N023-0=-
set N024-0=-
set N025-0=-
set N026-0=-
set N027-0=-
set N028-0=-
set N029-0=#
set N030-0=#
set N031-0=-
set N032-0=-
set N033-0=-
set N034-0=-
set N035-0=-
set N036-0=-
set N037-0=-
set N038-0=-
set N039-0=#
set N040-0=#
set N041-0=-
set N042-0=-
set N043-0=-
set N044-0=-
set N045-0=-
set N046-0=-
set N047-0=-
set N048-0=-
set N049-0=#
set N050-0=#
set N051-0=-
set N052-0=-
set N053-0=-
set N054-0=-
set N055-0=-
set N056-0=-
set N057-0=-
set N058-0=-
set N059-0=#
set N060-0=#
set N061-0=-
set N062-0=-
set N063-0=-
set N064-0=-
set N065-0=-
set N066-0=-
set N067-0=-
set N068-0=-
set N069-0=#
set N070-0=#
set N071-0=-
set N072-0=-
set N073-0=-
set N074-0=-
set N075-0=-
set N076-0=-
set N077-0=-
set N078-0=-
set N079-0=#
set N080-0=#
set N081-0=-
set N082-0=-
set N083-0=-
set N084-0=-
set N085-0=-
set N086-0=-
set N087-0=-
set N088-0=-
set N089-0=#
set N090-0=#
set N091-0=#
set N092-0=#
set N093-0=#
set N094-0=#
set N095-0=#
set N096-0=#
set N097-0=#
set N098-0=#
set N099-0=#

than this will be

```
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
##########
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
#--------#
##########
```
is level 0 and level 1
and will be
set N00-0=#
set N01-0=#
set N02-0=#
set N03-0=#
set N04-0=#
set N05-0=N
set N06-0=#
set N07-0=#
set N08-0=#
set N09-0=#
set N010-0=#
set N011-0=-
set N012-0=-
set N013-0=-
set N014-0=-
set N015-0=-
set N016-0=-
set N017-0=-
set N018-0=-
set N019-0=#
set N020-0=#
set N021-0=-
set N022-0=-
set N023-0=-
set N024-0=-
set N025-0=-
set N026-0=-
set N027-0=-
set N028-0=-
set N029-0=#
set N030-0=#
set N031-0=-
set N032-0=-
set N033-0=-
set N034-0=-
set N035-0=-
set N036-0=-
set N037-0=-
set N038-0=-
set N039-0=#
set N040-0=#
set N041-0=-
set N042-0=-
set N043-0=-
set N044-0=-
set N045-0=-
set N046-0=-
set N047-0=-
set N048-0=-
set N049-0=#
set N050-0=#
set N051-0=-
set N052-0=-
set N053-0=-
set N054-0=-
set N055-0=-
set N056-0=-
set N057-0=-
set N058-0=-
set N059-0=#
set N060-0=#
set N061-0=-
set N062-0=-
set N063-0=-
set N064-0=-
set N065-0=-
set N066-0=-
set N067-0=-
set N068-0=-
set N069-0=#
set N070-0=#
set N071-0=-
set N072-0=-
set N073-0=-
set N074-0=-
set N075-0=-
set N076-0=-
set N077-0=-
set N078-0=-
set N079-0=#
set N080-0=#
set N081-0=-
set N082-0=-
set N083-0=-
set N084-0=-
set N085-0=-
set N086-0=-
set N087-0=-
set N088-0=-
set N089-0=#
set N090-0=#
set N091-0=#
set N092-0=#
set N093-0=#
set N094-0=#
set N095-0=#
set N096-0=#
set N097-0=#
set N098-0=#
set N099-0=#

set N00-1=#
set N01-1=#
set N02-1=#
set N03-1=#
set N04-1=#
set N05-1=#
set N06-1=#
set N07-1=#
set N08-1=#
set N09-1=#
set N010-1=#
set N011-1=-
set N012-1=-
set N013-1=-

and more

so the first 0,1,2,3,4,5,6,7,8,9
lines will be
set N0#-0=something
line 10,11,12,13,14,15,16,17,18,19
will be
set N0#-1=something

i hope you understand


----------



## foxidrive (Oct 20, 2012)

Why is it nn-n for 0 to 9 and then nnn-n for the rest? Is that an error?

```
set N00-0=#
set N01-0=#
set N02-0=#
set N03-0=#
set N04-0=#
set N05-0=N
set N06-0=#
set N07-0=#
set N08-0=#
set N09-0=#
set N010-0=#
set N011-0=-
set N012-0=-
set N013-0=-
set N014-0=-
set N015-0=-
set N016-0=-
set N017-0=-
set N018-0=-
set N019-0=#
set N020-0=#
set N021-0=-
set N022-0=-
```


----------



## vista_narvas (Nov 6, 2012)

i make it so it is N0 than a number from 0 to 99
after 9 comes 10 so its still nn only looks like nnn


----------



## foxidrive (Oct 20, 2012)

Will each level be in a separate file? If so then what are the filenames called?


----------



## vista_narvas (Nov 6, 2012)

all levels in 1 file


----------



## foxidrive (Oct 20, 2012)

I think this works as you described.

You cannot use ! in the map, and you should avoid % and ^ too.


```
@echo off
setlocal enabledelayedexpansion
set pixel=-1
set level=0
for /f "delims=" %%a in (file.txt) do (
set "character=%%a"
for /L %%b in (0,1,9) do (
set /a pixel=pixel+1
set "N0!pixel!-!level!=!character:~%%b,1!"
)
if !pixel! EQU 99 (
set pixel=-1
set /a level=level+1
)
)
set n
pause
```


----------



## foxidrive (Oct 20, 2012)

How did that go for you?


----------



## vista_narvas (Nov 6, 2012)

it works good i think (it prints it in a different order than i had in mined but thats ok)
the only thing is if i have 99 levels it doesn't display them all
is there a way to echo them in a text file so i can put that in the game?


----------



## foxidrive (Oct 20, 2012)

I don't think that 99 levels is a limit. The sort that happens with the variables in the format you have chosen means that level 100 and 101 etc will be listed near level 1 and level 10.

Do you want the opposite of what I have done, so that it writes the identical file again, with all the pixel details from memory?


----------



## vista_narvas (Nov 6, 2012)

yes but i have 0 to 99 levels and i like to echo them to a other file so i can copy paste in the game
and still have a backup this way it doesn't have to load the text file every time it starts


----------



## foxidrive (Oct 20, 2012)

I don't understand what you are trying to achieve.


----------



## vista_narvas (Nov 6, 2012)

im trying to put the output from this script in a text file
so i can copy all the code into the game so its all compact and i the game doesn't need the file with all the levels


----------



## foxidrive (Oct 20, 2012)

%%a is level and %%b is pixel


```
@echo off
setlocal enabledelayedexpansion
set c=-1
for /L %%a in (0,1,99) do (
for /L %%b in (0,1,99) do (
set /a c=c+1
call set var=%%N0%%b-%%a%%
set /p "=!var!" >>"file2.txt" <nul
if !c! EQU 9 (
>>"file2.txt" echo.
set c=-1
)
)
)
```


----------



## vista_narvas (Nov 6, 2012)

i dont know how it works or how to use it
but it only adds empty lines to file2.txt


----------



## vista_narvas (Nov 6, 2012)

with some editing
i added this line

```
echo set N0!pixel!-!level!=!character:~%%b,1!>>file2.txt
```
and pasted it your code

```
setlocal enabledelayedexpansion
set pixel=-1
set level=0
for /f "delims=" %%a in (file.txt) do (
set "character=%%a"
for /L %%b in (0,1,9) do (
set /a pixel=pixel+1
set "N0!pixel!-!level!=!character:~%%b,1!"
)
if !pixel! EQU 99 (
set pixel=-1
set /a level=level+1
)
)
set n
pause
```
and i got the output that i needed thank you very much for your help


----------



## foxidrive (Oct 20, 2012)

vista_narvas said:


> i dont know how it works or how to use it
> but it only adds empty lines to file2.txt


You have to include the first batch file to populate the environment, or it will output empty lines.


----------

