1

I want to make a code to read specific lines and letters from a text file. for example, if savefile.txt said this:

AandV
7
3827

i want it to do something like this:

-set %checkpoint% to [all of row 1 of savefile.txt]
-set %PWR% to [all of row 2 of savefile.txt]
-set %avr% to [1st letter of row 3 of savefile.txt]
-set %zvr% to [2nd letter of row 3 of savefile.txt]
-set %pvr% to [3rd letter of row 3 of savefile.txt]
-set %a2vr% to [4th letter of row 3 of savefile.txt]

is there anyway to do this?

EXTRA: i would also like to know how to write into a specific line of a text file, like so:

if exist DARKENEDcp.txt (
    del DARKENEDcp.txt
)   
@echo %checkpoint%> [line 1 of DARKENEDcp.txt]
@echo %PWR%> [line 2 of DARKENEDcp.txt]
@echo %avr%%zvr%%pvr%%a2vr%> [line 3 of DARKENEDcp.txt]

if this is possible, that would be very useful for my game.

LORDMINE
  • 21
  • 2
  • Possible duplicate of [How to read file contents into a variable in a batch file?](https://stackoverflow.com/questions/3068929/how-to-read-file-contents-into-a-variable-in-a-batch-file) – Ari Cooper-Davis Nov 07 '19 at 21:51
  • 2
    Hey, welcome to Stack Overflow! If you search the site you'll find lots of information on how to read text from files into variables. In the future it's worth doing some research before you post your question here, such as searching Google or searching the site, so you can show us some code :) Also, in the future please keep distinct questions separate rather than ask 2 at once. Thanks! – Ari Cooper-Davis Nov 07 '19 at 21:52

1 Answers1

0

As your line lengths are clearly less than 1021 bytes and as long as each terminates with CRLF or LFCR, the following may help.

@(  Set /P "checkpoint="
    Set /P "PWR="
    Set /P "avr=")<"savefile.txt"
@(  Echo=%checkpoint%
    Echo=%PWR%
    Echo=%avr:~0,4%)>"DARKENEDcp.txt"

If you still need all of the variables defined beyond getting the output content you required:

@(  Set /P "checkpoint="
    Set /P "PWR="
    Set /P "avr=")<"savefile.txt"
@(  Set "a2vr=%avr:~3,1%"
    Set "pvr=%avr:~2,1%"
    Set "zvr=%avr:~1,1%"
    Set "avr=%avr:~0,1%")
@(  Echo=%checkpoint%
    Echo=%PWR%
    Echo=%avr%%zvr%%pvr%%a2vr%)>"DARKENEDcp.txt"
Compo
  • 36,585
  • 5
  • 27
  • 39