如何将文本文件的内容加载到批处理文件变量中?

时间:2022-06-30 05:57:42

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.

感谢Roman Odaisky的回答,这就是我的所作所为。

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

echo %content%
EndLocal

6 个解决方案

#1


14  

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 :-).

也许你必须做setlocal enabledelayedexpansion和/或使用!内容!而不是%content%。我无法测试,因为我附近没有任何MS Windows(我希望你也一样:-)。

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.

我所知道的最好的批处理文件 - 黑魔法参考是在http://www.rsdn.ru/article/winshell/batanyca.xml。如果你不懂俄语,你仍然可以使用提供的代码片段。

#2


50  

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

如果你的set命令支持/ p开关,那么你可以通过这种方式管道输入。

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.

/ P开关允许您将变量的值设置为用户输入的输入行。在读取输入行之前显示指定的promptString。 promptString可以为空。

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

这具有为未注册的文件类型工作的额外好处(接受的答案没有)。

#3


2  

You can use:

您可以使用:

set content=
for /f "delims=" %%i in ('type text.txt') do set content=!content! %%i

#4


2  

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.)

您可以使用for循环来完成此操作,但是没有简单的方法将CR / LF插入到环境变量中,因此您将拥有一行中的所有内容。 (您可以根据自己的需要解决此问题。)

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.)

您也可以通过这种方式限制少于约8k的文本文件。 (你不能创建一个大于8k的单个env变量。)

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

比尔对for循环的建议可能就是你所需要的。您一次处理一行文件:

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

(在批处理文件中的命令行%% i中使用%i)

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

#5


0  

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

创建一个名为“SetFile.bat”的文件,其中包含以下行,其末尾没有回车符...

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.

%1是输入文件的名称,%FileContents%将包含调用后输入文件的内容。这只适用于单行文件(即不包含回车符的文件)。如果需要,可以在调用%tmp%.bat之前从文件中删除/替换回车符。

#6


-1  

for /f "delims=" %%i in (count.txt) do set c=%%i
echo %c%
pause

#1


14  

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 :-).

也许你必须做setlocal enabledelayedexpansion和/或使用!内容!而不是%content%。我无法测试,因为我附近没有任何MS Windows(我希望你也一样:-)。

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.

我所知道的最好的批处理文件 - 黑魔法参考是在http://www.rsdn.ru/article/winshell/batanyca.xml。如果你不懂俄语,你仍然可以使用提供的代码片段。

#2


50  

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

如果你的set命令支持/ p开关,那么你可以通过这种方式管道输入。

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.

/ P开关允许您将变量的值设置为用户输入的输入行。在读取输入行之前显示指定的promptString。 promptString可以为空。

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

这具有为未注册的文件类型工作的额外好处(接受的答案没有)。

#3


2  

You can use:

您可以使用:

set content=
for /f "delims=" %%i in ('type text.txt') do set content=!content! %%i

#4


2  

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.)

您可以使用for循环来完成此操作,但是没有简单的方法将CR / LF插入到环境变量中,因此您将拥有一行中的所有内容。 (您可以根据自己的需要解决此问题。)

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.)

您也可以通过这种方式限制少于约8k的文本文件。 (你不能创建一个大于8k的单个env变量。)

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

比尔对for循环的建议可能就是你所需要的。您一次处理一行文件:

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

(在批处理文件中的命令行%% i中使用%i)

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

#5


0  

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

创建一个名为“SetFile.bat”的文件,其中包含以下行,其末尾没有回车符...

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.

%1是输入文件的名称,%FileContents%将包含调用后输入文件的内容。这只适用于单行文件(即不包含回车符的文件)。如果需要,可以在调用%tmp%.bat之前从文件中删除/替换回车符。

#6


-1  

for /f "delims=" %%i in (count.txt) do set c=%%i
echo %c%
pause