批处理文件:如何读取文件?

时间:2021-08-11 01:35:25

How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode?

如何从批处理文件读取文件(文本或二进制文件)?有一种方法可以在二进制模式或文本模式下读取它?

6 个解决方案

#1


37  

You can use the for command:

您可以使用for命令:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

Type

类型

for /?

at the command prompt. Also, you can parse ini files!

在命令提示符。此外,您还可以解析ini文件!

#2


57  

Under NT-style cmd.exe, you can loop through the lines of a text file with

在NT-style cmd。exe,您可以对文本文件的行进行循环。

FOR /F %i IN (file.txt) DO @echo %i

Type "help for" on the command prompt for more information. (don't know if that works in whatever "DOS" you are using)

在命令提示上键入“help for”以获取更多信息。(不知道你用的是什么“DOS”)

#3


31  

The FOR-LOOP generally works, but there are some issues. The FOR doesn't accept empty lines and lines with more than ~8190 are problematic. The expansion works only reliable, if the delayed expansion is disabled.

for循环通常是有效的,但也存在一些问题。FOR不接受空行和超过~8190的行是有问题的。如果延迟扩展是禁用的,扩展只能是可靠的。

Detection of CR/LF versus single LF seems also a little bit complicated.
Also NUL characters are problematic, as a FOR-Loop immediatly cancels the reading.

CR/LF与单LF的检测似乎也有点复杂。NUL字符也有问题,因为for循环立即取消了读取。

Direct binary reading seems therefore nearly impossible.

因此,直接二进制阅读几乎是不可能的。

The problem with empty lines can be solved with a trick. Prefix each line with a line number, using the findstr command, and after reading, remove the prefix.

空行的问题可以用一个小技巧来解决。使用findstr命令在每行前面加上一个行号,并在读取后删除前缀。

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    echo(!var!
    ENDLOCAL
)

Toggling between enable and disabled delayed expansion is neccessary for the safe working with strings, like ! or ^^^xy!z.
That's because the line set "var=%%a" is only safe with DisabledDelayedExpansion, else exclamation marks are removed and the carets are used as (secondary) escape characters and they are removed too.
But using the variable var is only safe with EnabledDelayedExpansion, as even a call %%var%% will fail with content like "&"&.

在启用和禁用延迟扩展之间进行切换是安全使用字符串的必要条件,比如!或^ ^ ^ xy ! z。这是因为行设置的“var=% a”只与DisabledDelayedExpansion是安全的,其他的感叹号被删除,而carets被用作(次要的)转义字符,它们也被删除了。但是,使用变量var只可以安全的使用EnabledDelayedExpansion,因为即使是调用%%var%%也会以“&”之类的内容失败。

EDIT: Added set/p variant
There is a second way of reading a file with set /p, the only disadvantages are that it is limited to ~1024 characters per line and it removes control characters at the line end.
But the advantage is, you didn't need the delayed toggling and it's easier to store values in variables

编辑:添加的set/p变体有第二种方法读取带有set/p的文件,唯一的缺点是每行限制为~1024个字符,并在行端删除控制字符。但好处是,你不需要延迟的切换,更容易存储变量的值。

@echo off
setlocal EnableDelayedExpansion
set "file=%~1"

for /f "delims=" %%n in ('find /c /v "" %file%') do set "len=%%n"
set "len=!len:*: =!"

<%file% (
  for /l %%l in (1 1 !len!) do (
    set "line="
    set /p "line="
    echo(!line!
  )
)

For reading it "binary" into a hex-representation
You could look at SO: converting a binary file to HEX representation using batch file

为了将它“二进制”读取到一个HEX表示中,您可以这样看:将二进制文件转换为十六进制表示使用批处理文件。

#4


0  

Well theres a lot of different ways but if you only want to DISPLAY the text and not STORE it anywhere then you just use: findstr /v "randomtextthatnoonewilluse" filename.txt

有很多不同的方法,但如果你只想显示文本,而不是将它存储在任何地方,那么你只需使用:findstr /v "randomtextthatnoonewilluse" filename.txt。

#5


0  

One very easy way to do it is use the following command:

一个非常简单的方法是使用以下命令:

set /p mytextfile=< %pathtotextfile%\textfile.txt
echo %mytextfile%

This will only display the first line of text in a text file. The other way you can do it is use the following command:

这将只显示文本文件中的第一行文本。另一种方法是使用以下命令:

type %pathtotextfile%\textfile.txt

This will put all the data in the text file on the screen. Hope this helps!

这将把所有数据放在屏幕上的文本文件中。希望这可以帮助!

#6


-4  

if you want just to display it on cmd, you can use this :

如果你想在cmd上显示它,你可以用这个:

cat myfile.txt 

#1


37  

You can use the for command:

您可以使用for命令:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

Type

类型

for /?

at the command prompt. Also, you can parse ini files!

在命令提示符。此外,您还可以解析ini文件!

#2


57  

Under NT-style cmd.exe, you can loop through the lines of a text file with

在NT-style cmd。exe,您可以对文本文件的行进行循环。

FOR /F %i IN (file.txt) DO @echo %i

Type "help for" on the command prompt for more information. (don't know if that works in whatever "DOS" you are using)

在命令提示上键入“help for”以获取更多信息。(不知道你用的是什么“DOS”)

#3


31  

The FOR-LOOP generally works, but there are some issues. The FOR doesn't accept empty lines and lines with more than ~8190 are problematic. The expansion works only reliable, if the delayed expansion is disabled.

for循环通常是有效的,但也存在一些问题。FOR不接受空行和超过~8190的行是有问题的。如果延迟扩展是禁用的,扩展只能是可靠的。

Detection of CR/LF versus single LF seems also a little bit complicated.
Also NUL characters are problematic, as a FOR-Loop immediatly cancels the reading.

CR/LF与单LF的检测似乎也有点复杂。NUL字符也有问题,因为for循环立即取消了读取。

Direct binary reading seems therefore nearly impossible.

因此,直接二进制阅读几乎是不可能的。

The problem with empty lines can be solved with a trick. Prefix each line with a line number, using the findstr command, and after reading, remove the prefix.

空行的问题可以用一个小技巧来解决。使用findstr命令在每行前面加上一个行号,并在读取后删除前缀。

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    echo(!var!
    ENDLOCAL
)

Toggling between enable and disabled delayed expansion is neccessary for the safe working with strings, like ! or ^^^xy!z.
That's because the line set "var=%%a" is only safe with DisabledDelayedExpansion, else exclamation marks are removed and the carets are used as (secondary) escape characters and they are removed too.
But using the variable var is only safe with EnabledDelayedExpansion, as even a call %%var%% will fail with content like "&"&.

在启用和禁用延迟扩展之间进行切换是安全使用字符串的必要条件,比如!或^ ^ ^ xy ! z。这是因为行设置的“var=% a”只与DisabledDelayedExpansion是安全的,其他的感叹号被删除,而carets被用作(次要的)转义字符,它们也被删除了。但是,使用变量var只可以安全的使用EnabledDelayedExpansion,因为即使是调用%%var%%也会以“&”之类的内容失败。

EDIT: Added set/p variant
There is a second way of reading a file with set /p, the only disadvantages are that it is limited to ~1024 characters per line and it removes control characters at the line end.
But the advantage is, you didn't need the delayed toggling and it's easier to store values in variables

编辑:添加的set/p变体有第二种方法读取带有set/p的文件,唯一的缺点是每行限制为~1024个字符,并在行端删除控制字符。但好处是,你不需要延迟的切换,更容易存储变量的值。

@echo off
setlocal EnableDelayedExpansion
set "file=%~1"

for /f "delims=" %%n in ('find /c /v "" %file%') do set "len=%%n"
set "len=!len:*: =!"

<%file% (
  for /l %%l in (1 1 !len!) do (
    set "line="
    set /p "line="
    echo(!line!
  )
)

For reading it "binary" into a hex-representation
You could look at SO: converting a binary file to HEX representation using batch file

为了将它“二进制”读取到一个HEX表示中,您可以这样看:将二进制文件转换为十六进制表示使用批处理文件。

#4


0  

Well theres a lot of different ways but if you only want to DISPLAY the text and not STORE it anywhere then you just use: findstr /v "randomtextthatnoonewilluse" filename.txt

有很多不同的方法,但如果你只想显示文本,而不是将它存储在任何地方,那么你只需使用:findstr /v "randomtextthatnoonewilluse" filename.txt。

#5


0  

One very easy way to do it is use the following command:

一个非常简单的方法是使用以下命令:

set /p mytextfile=< %pathtotextfile%\textfile.txt
echo %mytextfile%

This will only display the first line of text in a text file. The other way you can do it is use the following command:

这将只显示文本文件中的第一行文本。另一种方法是使用以下命令:

type %pathtotextfile%\textfile.txt

This will put all the data in the text file on the screen. Hope this helps!

这将把所有数据放在屏幕上的文本文件中。希望这可以帮助!

#6


-4  

if you want just to display it on cmd, you can use this :

如果你想在cmd上显示它,你可以用这个:

cat myfile.txt