简体   繁体   English

将文本文件内容读入批处理变量

[英]Read text file content into batch variable

I have files: 我有文件:
y:\\a.txt Y:\\ A.TXT
y:\\b.txt Y:\\ b.txt

I have x.bat: 我有x.bat:

@echo off  
y:  
cd \  
for /f %%f in ('dir /b y:\*.txt') do (  
set content=  
set /p content=<y:\%%f  
echo y:\%%f  
echo content=%content%  
)

and the result: 结果:

y:\a.txt  
content=  
y:\b.txt  
content=  

Why does not contain %content% variable the first lie of the text files? 为什么文本文件的第一个谎言不包含%content%变量? The files are not empty. 文件不为空。

Modify your batch script as follows: 修改您的批处理脚本,如下所示:

@echo off  
setlocal enableDelayedExpansion
y:\
cd \
set content=
for /f %%f in ('dir /b *.txt') do (  
set /p content=<%%~dpff
echo file:%%~dpff , content:!content!
)

EDIT. 编辑。 Note the ~dpf to get the file path. 注意~dpf以获取文件路径。 As @foxidrive has mentioned it wouldn't have worked without a proper path. 正如@foxidrive所提到的,没有正确的路径就无法工作。

The output is along the lines of: 输出如下:

file:y:\a.txt , content:lorem
file:y:\b.txt , content:ipsum
file:y:\c.txt , content:Ocaml, Haskell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM