批处理脚本打开文本文件

时间:2013-03-08 13:23:58

标签: batch-file

我有一个批处理脚本来读取文本文件。如果文件名不包含特殊字符,它工作正常,但如果文件名包含特殊字符则失败。

Ex文件名:localhost_access_log.2013-03-08

代码:

for /f "skip=%* tokens=*" %%b in (Y:\Program Files\Apache Software Foundation\Tomcat 7.0\logs\localhost_access_log.2013-03-08.txt) do (
    echo "Data Copied"
)

请帮我看看如何阅读这些文件。

1 个答案:

答案 0 :(得分:1)

在命令行输入help for以获取有关for命令的文档(我承认这是很难阅读的),并且你将其隐藏在中途:

For file names that contain spaces, you need to quote the filenames with
double quotes.  In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.

所以你的代码应该改为:

for /f "usebackq skip=%* tokens=*" %%b in ("Y:\Program Files\Apache Software Foundation\Tomcat 7.0\logs\localhost_access_log.2013-03-08.txt") do (
    echo "Data Copied"
)