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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/134001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:23:55  来源:igfitidea点击:

How can I load the contents of a text file into a batch file variable?

windowsbatch-filecommand-linecmd

提问by Keng

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

采纳答案by Roman Odaisky

Use for, something along the lines of:

使用for,类似的东西:

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

Maybe you'll have to do setlocal enabledelayedexpansionand/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!而不是%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.

我所知道的最好的 batch-file-black-magic-reference 是http://www.rsdn.ru/article/winshell/batanyca.xml。如果您不会俄语,您仍然可以使用提供的代码片段。

回答by Will Bickford

If your setcommand supports the /pswitch, 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).

这具有适用于未注册文件类型的额外好处(接受的答案没有)。

回答by Roman Odaisky

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 的环境变量。)

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

Bill 对 for 循环的建议可能正是您所需要的。您一次处理一行文件:

(use %iat a command line %%iin 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

回答by Curro

You can use:

您可以使用:

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

回答by pdavis

Create a file called "SetFile.bat" that contains the following line with no carriage returnat 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 之前从文件中删除/替换回车符。

回答by clichok

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