如何批量读取文本文件的全部内容 [英] How to read entire content of a text file in batch

查看:453
本文介绍了如何批量读取文本文件的全部内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取批处理文件中文本文件的全部内容. 我发现了这个:

I want to read entire content of a text file in a batch file. I found this :

对于/f"delims ="(file.txt)中的%% x,确实设置了content = %% x

for /f "delims=" %%x in (file.txt) do set content=%%x

但是"content"变量只有最后一行,我想将整个文件读入一个变量.

but the "content" variable just has the last line, I want to read entire file into a single variable.

推荐答案

来自您的评论

有什么方法可以在!content中包含行!

Is there any way to include lines in !content!

我假设您要在内容变量中使用换行符,这可以使用一个额外的变量来完成.

I assume you want linefeeds in your content variable, this can be done with an extra variable.

@echo off
setlocal EnableDelayedExpansion
set LF=^


rem ** The two empty lines are necessary
set "content="

for /f "delims=" %%x in (file.txt) do (
  set "content=!content!%%x!LF!"
)
echo(!content!

endlocal

这篇关于如何批量读取文本文件的全部内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆