Batch files: How to read a file?

2019-01-01 01:13发布

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?

8条回答
呛了眼睛熬了心
2楼-- · 2019-01-01 01:17

Use:
set /p var=< file.ext
It's the simplest way to do it works on most versions of batch.

查看更多
刘海飞了
3楼-- · 2019-01-01 01:18

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!

查看更多
美炸的是我
4楼-- · 2019-01-01 01:28

You can use the for command:

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!

查看更多
旧时光的记忆
5楼-- · 2019-01-01 01:29

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

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)

查看更多
初与友歌
6楼-- · 2019-01-01 01:31

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

查看更多
余生请多指教
7楼-- · 2019-01-01 01:36

A code that displays the contents of the myfile.txt file on the screen

set %filecontent%=0
type %filename% >> %filecontent%
echo %filecontent%

查看更多
登录 后发表回答