How can I load the contents of a text file into a

2019-01-16 10:13发布

I need to be able to load the entire contents of a text file and load it into a variable for further processing.

How can I do that?


Here's what I did thanks to Roman Odaisky's answer.

SetLocal EnableDelayedExpansion
set content=
for /F "delims=" %%i in (test.txt) do set content=!content! %%i

echo %content%
EndLocal

6条回答
再贱就再见
2楼-- · 2019-01-16 10:37

If your set command supports the /p switch, then you can pipe input that way.

set /p VAR1=<test.txt

set /? |find "/P"

The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.

This has the added benefit of working for un-registered file types (which the accepted answer does not).

查看更多
可以哭但决不认输i
3楼-- · 2019-01-16 10:38

Create a file called "SetFile.bat" that contains the following line with no carriage return at the end of it...

set FileContents=

Then in your batch file do something like this...

   @echo off
   copy SetFile.bat + %1 $tmp$.bat > nul
   call $tmp$.bat
   del $tmp$.bat

%1 is the name of your input file and %FileContents% will contain the contents of the input file after the call. This will only work on a one line file though (i.e. a file containing no carriage returns). You could strip out/replace carriage returns from the file before calling the %tmp%.bat if needed.

查看更多
放我归山
4楼-- · 2019-01-16 10:39
for /f "delims=" %%i in (count.txt) do set c=%%i
echo %c%
pause
查看更多
何必那么认真
5楼-- · 2019-01-16 10:42

You can use:

set content=
for /f "delims=" %%i in ('type text.txt') do set content=!content! %%i
查看更多
smile是对你的礼貌
6楼-- · 2019-01-16 10:48

Use for, something along the lines of:

set content=
for /f "delims=" %%i in ('filename') do set content=%content% %%i

Maybe you’ll have to do setlocal enabledelayedexpansion and/or use !content! rather than %content%. I can’t test, as I don’t have any MS Windows nearby (and I wish you the same :-).

The best batch-file-black-magic-reference I know of is at http://www.rsdn.ru/article/winshell/batanyca.xml. If you don’t know Russian, you still could make some use of the code snippets provided.

查看更多
虎瘦雄心在
7楼-- · 2019-01-16 10:55

Can you define further processing?

You can use a for loop to almost do this, but there's no easy way to insert CR/LF into an environment variable, so you'll have everything in one line. (you may be able to work around this depending on what you need to do.)

You're also limited to less than about 8k text files this way. (You can't create a single env var bigger than around 8k.)

Bill's suggestion of a for loop is probably what you need. You process the file one line at a time:

(use %i at a command line %%i in a batch file)

for /f "tokens=1 delims=" %%i in (file.txt) do echo %%i

more advanced:

for /f "tokens=1 delims=" %%i in (file.txt) do call :part2 %%i
goto :fin

:part2
echo %1
::do further processing here
goto :eof

:fin
查看更多
登录 后发表回答