如何处理路径名称空间中的for循环? [英] How to handle spaces in path names in for loop?

查看:146
本文介绍了如何处理路径名称空间中的for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使用路径当前脚本,并且路径中包含有空格。我似乎无法得到它的工作虽然:

  C:\\ test目录> DIR
 在驱动器C卷没有标签。
 卷序列号是7486,CEE6 的C目录:\\ test目录08/31/2010下午7点28< D​​IR> 。
08/31/2010下午7点28< D​​IR> ..
08/31/2010下午7时28分20 echoit.cmd
08/31/2010下午7时28分94 TEST.CMD
               2文件(县)114个字节
               2目录(S)344141197312免费字节C:\\ test目录>键入echoit.cmd
关闭@echo
回声%*C:\\ test目录>键入TEST.CMD
关闭@echoFOR / F令牌= *%%一个在('%〜DP0 \\ echoit.cmd的Hello World')做(
    回声%%一
)C:\\ test目录>测试
C:\\测试不被识别为一个内部或外部命令,
可运行的程序或批处理文件。C:\\ test目录>


解决方案

修改 TEST.CMD 为以下内容:

 关闭@echo   FOR / F令牌= *%%一个在('%〜DP0 \\ echoit.cmd的Hello World)做(
    回声%%一
   )

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

Trying to use the path to the current script, and the path contains spaces in it. I can't seem to get it to work though:

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>

解决方案

Change test.cmd to the following:

   @echo off

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

You need to set the entire command, minus arguments, in quotes. The Windows command prompt treats a collection of words as a single command when the entire collection is quoted, that is why you must exclude the Hello World arguments. If you were to include that in the quotes, Windows would treat that as part of the command not as arguments.

这篇关于如何处理路径名称空间中的for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆