从文本文件读取-批处理 [英] Reading From A Text File - Batch

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

问题描述

我有一个文本文件a.txt:

I have a text file, a.txt:

Hello World
下午好

Hello World
Good Afternoon

我写了一个批处理脚本来逐行读取该文件的内容:
FOR /F "tokens=* delims=" %%x in (a.txt) DO echo %%x

I have written a batch script to read contents of this file line by line:
FOR /F "tokens=* delims=" %%x in (a.txt) DO echo %%x

由于定界符(空格)的默认行为,我得到的输出为"Hello" "World". 如何覆盖此行为以将输出作为"Hello World" "Good Afternoon"

I am getting output as "Hello" "World" due to default behaviour of delimiter(space). How can I override this behaviour to get the ouptut as "Hello World" "Good Afternoon"

推荐答案

您的代码用于/f"tokens = * delims ="(a.txt)中的%% x做回显%% x"在大多数Windows上均适用操作系统,除非您已修改命令.

Your code "for /f "tokens=* delims=" %%x in (a.txt) do echo %%x" will work on most Windows Operating Systems unless you have modified commands.

因此,您可以在执行"for/f"命令以跟进字符串之前,将其"cd"导入目录以进行读取.例如,如果文件"a.txt"位于C:\ documents and settings \%USERNAME%\ desktop \ a.txt,那么您将使用以下内容.

So you could instead "cd" into the directory to read from before executing the "for /f" command to follow out the string. For instance if the file "a.txt" is located at C:\documents and settings\%USERNAME%\desktop\a.txt then you'd use the following.

cd "C:\documents and settings\%USERNAME%\desktop"
for /f "tokens=* delims=" %%x in (a.txt) do echo %%x
echo.
echo.
echo.
pause >nul
exit

但是由于x的原因这在您的计算机上不起作用,因此有一种更简便,更有效的方法来执行此操作.使用类型"命令.

But since this doesn't work on your computer for x reason there is an easier and more efficient way of doing this. Using the "type" command.

@echo off
color a
cls
cd "C:\documents and settings\%USERNAME%\desktop"
type a.txt
echo.
echo.
pause >nul
exit

或者,如果您希望他们选择要从中批量写入的文件,则可以执行以下操作.

Or if you'd like them to select the file from which to write in the batch you could do the following.

@echo off
:A
color a
cls
echo Choose the file that you want to read.
echo.
echo.
tree
echo.
echo.
echo.
set file=
set /p file=File:
cls
echo Reading from %file%
echo.
type %file%
echo.
echo.
echo.
set re=
set /p re=Y/N?:
if %re%==Y goto :A
if %re%==y goto :A
exit

这篇关于从文本文件读取-批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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