windows 从批处理文件中的文本文件中读取行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3184107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading lines from a text file in batch file
提问by Nathan W
I know this has been asked 100 times before but every bit of code I try just doesn't seem to work. I am trying to compile a file and then check for a error file and read it line by line and dump it into command line:
我知道这之前已被问过 100 次,但我尝试的每一段代码似乎都不起作用。我正在尝试编译一个文件,然后检查错误文件并逐行读取它并将其转储到命令行中:
This is what I have so far:
这是我到目前为止:
Set var1= %1
Set var2= %var1:.mb=.ERR%
echo %var1%
echo %var2%
"C:\Program Files\MapInfo\MapBasic\mapbasic.exe" -D %var1%
FOR /f "delims=" %%a in (%var2%) do echo %%a
So I am taking the path to the file as a command line arg this works fine, then taking the .mb off and replacing it with .ERR and running the program to complie the .mb file, everything works fine up until I hit the for loop.
所以我将文件的路径作为命令行 arg 这工作正常,然后关闭 .mb 并用 .ERR 替换它并运行程序来编译 .mb 文件,一切正常,直到我点击 for环形。
The result for loop looks like this, when running with echo on:
在打开 echo 的情况下运行时,for 循环的结果如下所示:
FOR /F "delims=" %a in ("C:\Documents and Settings\woodrown\My Documents\Visual Studio 2008\Projects\MapInfoInteropTemplate1\MapInfoInteropTemplate1.ERR") do echo %a
but when it runs it just prints out
"C:\Documents and Settings\woodrown\My Documents\Visual Studio 2008\Projects\MapInfoInteropTemplate1\MapInfoInteropTemplate1.ERR"
not the files content.
但是当它运行时,它只打印出
"C:\Documents and Settings\woodrown\My Documents\Visual Studio 2008\Projects\MapInfoInteropTemplate1\MapInfoInteropTemplate1.ERR"
不文件内容。
I can get it to work if I give it a hard name and no path with spaces, such as 'MapInfoInteropTemplate1.ERR' but I want it to use %var2%
for the file name to read.
如果我给它一个硬名称并且没有带空格的路径,例如“MapInfoInteropTemplate1.ERR”,我可以让它工作,但我希望它%var2%
用于读取文件名。
If I put '
around the path it just launches my default text editor.
如果我'
绕过路径,它只会启动我的默认文本编辑器。
I ended up skipping the whole batch file generation of computer programing so this is really strange to me, sorry if it's a basic question.
我最终跳过了计算机编程的整个批处理文件生成,所以这对我来说真的很奇怪,对不起,如果这是一个基本问题。
回答by Joey
Use the usebackq
option to for
:
使用该usebackq
选项for
:
FOR /F "usebackq delims=" %%a in ("C:\Documents and Settings\woodrown\My Documents\Visual Studio 2008\Projects\MapInfoInteropTemplate1\MapInfoInteropTemplate1.ERR") do echo %%a
Otherwise it will interpret whatever you put between double-quoets as a string to tokenize, not as a file to read. This is also stated in the help:
否则,它会将您放在双引号之间的任何内容解释为要标记的字符串,而不是要读取的文件。这在帮助中也有说明:
or, if usebackq option present:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]