Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何在for循环中处理路径名中的空格?_Batch File - Fatal编程技术网

Batch file 如何在for循环中处理路径名中的空格?

Batch file 如何在for循环中处理路径名中的空格?,batch-file,Batch File,正在尝试使用当前脚本的路径,但该路径中包含空格。但我似乎无法让它发挥作用: C:\Test Directory>dir Volume in drive C has no label. Volume Serial Number is 7486-CEE6 Directory of C:\Test Directory 08/31/2010 07:28 PM <DIR> . 08/31/2010 07:28 PM <DIR>

正在尝试使用当前脚本的路径,但该路径中包含空格。但我似乎无法让它发挥作用:

C:\Test Directory>dir
 Volume in drive C has no label.
 Volume Serial Number is 7486-CEE6

 Directory of C:\Test Directory

08/31/2010  07:28 PM    <DIR>          .
08/31/2010  07:28 PM    <DIR>          ..
08/31/2010  07:28 PM                20 echoit.cmd
08/31/2010  07:28 PM                94 test.cmd
               2 File(s)            114 bytes
               2 Dir(s)  344,141,197,312 bytes free

C:\Test Directory>type echoit.cmd
@echo off
echo %*

C:\Test Directory>type test.cmd
@echo off

for /f "tokens=*" %%a in ('%~dp0\echoit.cmd Hello World') do (
    echo %%a
)

C:\Test Directory>test
'C:\Test' is not recognized as an internal or external command,
operable program or batch file.

C:\Test Directory>
C:\testdirectory>dir
驱动器C中的卷没有标签。
卷序列号为7486-CEE6
C:\Test目录的目录
2010年8月31日晚上7:28。
2010年8月31日晚上7:28。。
2010年8月31日07:28 PM 20 echoit.cmd
08/31/2010 07:28 PM 94 test.cmd
2个文件114字节
2 Dir(s)344141197312可用字节
C:\Test Directory>键入echoit.cmd
@回音
回声%*
C:\Test目录>type Test.cmd
@回音
对于(''%dp0\echoit.cmd Hello World')中的/f“tokens=*”%%a,请执行以下操作(
回声%%a
)
C:\Test目录>测试
“C:\Test”未被识别为内部或外部命令,
可操作的程序或批处理文件。
C:\Test目录>

test.cmd
更改为以下内容:

   @echo off

   for /f "tokens=*" %%a in ('"%~dp0\echoit.cmd" Hello World') do (
    echo %%a
   )

您需要在引号中设置整个命令(减去参数)。当引用整个集合时,Windows命令提示符将单词集合视为单个命令,这就是为什么必须排除
Hello World
参数的原因。如果要将其包含在引号中,Windows会将其视为命令的一部分,而不是参数。

是否尝试添加引号

for /f "tokens=*" %%a in ('"%~dp0\echoit.cmd" Hello World') do (
    echo %%a
)

使用~fs0,即如何

C:\Test Directory>type test.cmd
@echo off

for /f "tokens=*" %%a in ('%~fs0\echoit.cmd Hello World') do (
 echo %%a
)

其中%%fsI-将%I扩展为仅包含短名称的完整路径名

太好了,现在将其用于“c:\program files(x86)\”而不是“c:\Test Directory”,它应该以同样的方式用于“c:\program files(x86)”。你看到了什么?我没有令牌=*在添加了它并在用法周围加上“”引号后,它修复了我的间距问题。