批处理:从文件路径中包含空格的文件中读取行

glm*_*ndr 7 command-line batch-file

要从文件中读取行,请在批处理文件中执行以下操作:

for /f %%a in (myfile.txt) do (
    :: do stuff...
)
Run Code Online (Sandbox Code Playgroud)

现在假设你的档案在 C:\Program Files\myfolder

for /f %%a in ("C:\Program Files\myfolder\myfile.txt") do (
    echo %%a
)
Run Code Online (Sandbox Code Playgroud)

结果:

C:\Program Files\myfolder\myfile.txt
Run Code Online (Sandbox Code Playgroud)

这似乎将给定路径解释为字符串,因此%%a是您给定的路径.

到目前为止我发现的文档中没有任何相关内容.在我开枪之前,请有人帮助我.

ind*_*div 6

键入时获得的文档help 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.
Run Code Online (Sandbox Code Playgroud)

默认情况下,语法FOR /F如下.

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]
Run Code Online (Sandbox Code Playgroud)

此语法显示了您的type解决方法的工作原理.因为单引号说执行type命令并循环其输出.添加usebackq选项时,语法更改为:

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]
Run Code Online (Sandbox Code Playgroud)

现在,您可以双引号文件路径,单引号文字字符串,并在要执行的命令周围添加反引号(严重重音符号).

所以你想这样做:

for /f "usebackq" %%a in ("C:\Program Files\myfolder\myfile.txt") do (
    echo %%a
)
Run Code Online (Sandbox Code Playgroud)