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

寵の児 提交于 2019-12-08 08:12:05

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/30686018/how-do-you-load-information-from-a-txt-file-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!