批处理文件-从带空格的文本文件中读取 [英] Batch File - Read from a text file with spaces
问题描述
我写了一个批处理文件来读取位于特定路径中的文本文件. for循环从位于文件夹中的文本文件读取.如果同一路径中有空格,则for循环无法读取它.即使我将路径用引号引起来,它也仍然无法读取.该路径确实存在,我检查了多次,并确认该循环仅适用于不包含空格的路径名.
I wrote a batch file to read a text file located in a specific path. The for loop reads from a text file located in a folder. If this same path had spaces the for loop can't read it. Even if I wrap the path in quotes it still can't read it. The path does exist, I checked multiple times, and confirmed that the loop only works with pathnames that contain no spaces.
他们是我做错了什么,还是这是批处理循环的局限性?
Is their something that I'm doing wrong, or, is it a limitation of the batch for loop?
代码:
FOR /F "tokens=* delims=" %%x in (C:\Users\someuser\VirtualBoxLog\log.txt) DO SET read=%%x
如果此路径包含空格(例如C:\Users\someuser\VirtualBox Log\log.txt
),即使我将引号括起来并且存在VirtualBox日志文件夹,也无法从文本文件读取该文件.
If this path contained a space for example C:\Users\someuser\VirtualBox Log\log.txt
it can't read from the text file, even if I wrap it quotes and the folder VirtualBox Log existed.
推荐答案
您的问题的解决方案是将路径括在引号中:
The solution for your problem is enclosing the path in quotes:
"C:\Users\someuser\VirtualBox Log\log.txt"
但是现在又出现了另一个问题:for
将带引号的字符串解释为字符串,而不是文件名.
But now another problem rises: for
interprets a quotet string as a string, not as a filename.
要解决此问题,请使用usebackq
关键字:
To work around this, use the usebackq
keyword:
FOR /F "usebackq tokens=* delims=" %%x in ("C:\Users\someuser\VirtualBox Log\log.txt") DO SET read=%%x
这篇关于批处理文件-从带空格的文本文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!