简体   繁体   中英

How do you load information from a .txt file in batch?

So I have been doing some small batch programs lately but I can't manage to make the batch file load information. This is how i would do it.

:load_game
)
set /p something=
) > something.txt

In the txt file:

something_is_awesome

That is it^^

If i remember it right that is how you SAVE a file... now how do you LOAD it in a similar way?

Note: I would like to do multiple at once!

to WRITE a line to a file use echo my_information>something.txt (overwriting)

to READ a line from a file use set /p something=<something.txt

to write or read several lines:

echo First line>something.txt
echo second line>>something.txt
echo and a third one>>something.txt

or if you want to write all of them in one go:

@echo off
rem writing
(
  echo First line
  echo second line
  echo and a third one
)>something.txt

type something.txt
rem reading a
<something.txt (
  set /p one=
  set /p two=
  set /p three=
)
echo a. %one% %two% %three%

rem reading b
setlocal enabledelayedexpansion
set /a i=0
<something.txt (
  for /f "delims=" %%a in (something.txt) do (
    set /a i+=1
    set /p read[!i!]=
  )
)
echo b. %read[1]% %read[2]% %read[3]%

NOTE: contrary to what seems logical, set /p var=<<file.txt does NOT work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM