Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何批量读取包含空行的文本文件?_Batch File_Text - Fatal编程技术网

Batch file 如何批量读取包含空行的文本文件?

Batch file 如何批量读取包含空行的文本文件?,batch-file,text,Batch File,Text,我想让它也输出空行。例如,这是我的example.txt文件 Hello This is a text file. Hello World! 我运行它的代码是 for /f "delims=" %%a in (%cd%\example.txt) DO ( echo. %%a ) 但是它没有显示空行,而是像 Hello This is a text file. Hello World! 我如何使它也显示空行?谢谢 FOR/F忽略空行,但您可以使用findstr在每行

我想让它也输出空行。例如,这是我的example.txt文件

Hello 

This is a text file. Hello World!
我运行它的代码是

for /f "delims=" %%a in (%cd%\example.txt) DO ( 
echo. %%a
)
但是它没有显示空行,而是像

Hello
This is a text file. Hello World!

我如何使它也显示空行?谢谢

FOR/F忽略空行,但您可以使用findstr在每行前面加上行号,这样就不再有空行了

setlocal EnableDelayedExpansion
FOR /F "delims=" %%L in ('findstr /N "^" "%~dp0\example.txt"') DO (
   set "line=%%L"
   set "line=!line:*:=!" & rem Remove all characters to the first colon
   echo(!line!
)
延迟扩展的问题是它会破坏所有
^
文件中的字符

因此,您可以切换模式

setlocal DisableDelayedExpansion
FOR /F "delims=" %%L in ('findstr /N "^" "%~dp0\example.txt"') DO (
   set "line=%%L"
   setlocal EnableDelayedExpansion
   set "line=!line:*:=!" & rem Remove all characters to the first colon
   echo(!line!
   endlocal
)

尝试使用
Type
hey,因此我发现如果我只键入“%%a”,它会将文件中的所有文本视为命令,而不是键入“echo.%%a”。这是有用的!不管是哪种方式,谢谢你可以在[link]github.com/mrc2rules/LimitLess上看到我的代码,或许你可以提出一些建议