使用输出中的空格或句点解析For / F的结果

时间:2014-12-12 17:44:47

标签: batch-file cmd robocopy

我正在尝试运行For循环来扫描目录并将输出传递给RoboCopy命令。这适用于所有文件,除非它们有空格路径。作为示例,其中一个目录将是\ Win7_Images \ 1.Copy到USB \

这是我的原始代码。

For /f %%b In ('Dir /b /s /a:d "%fp_server%\pc_images\Win7_Images"') Do (Robocopy %%b I:\Images *.wim /xo /r:1 /Log+:I:\Images\filecopy.log)

我已经搜索了几个引用for循环空格的帖子,但似乎无法让这个工作使用他们的建议。 这是我得到的:

2014/12/12 11:56:02 ERROR 2 (0x00000002) Accessing Source Directory \\ServerName\pc_images\Win7_Images\1\
The system cannot find the file specified.

如您所见,它在此期间停止了。

This post建议使用delims=,所以我尝试了以下

For /f "delims=" %%b In ('Dir /b /s /a:d "%fp_server%\pc_images\Win7_Images"') Do (Robocopy %%b i:\Images *.wim /xo /r:1)

这也不起作用,我得到以下内容:

开始时间:2014年12月12日星期五12:25:38

来源 - \ SERVER \ pc_images \ Win7_Images \ 1 \      Dest - H:\ Projects \ SA projects \ Windows Imaging automation \ Create \

Files : a
    Bootable
    USB
    Drive

这里的最后一个难题是我真的不需要复制这个目录,但我只是不想输出错误。我试图以这种方式排除该目录:

For /f %%b In ('Dir /b /s /a:d "%fp_server%\pc_images\Win7_Images"') Do (Robocopy %%b I:\Images *.wim /xo /xd "%FP_Sever%\PC_Images\Win7_images\1. Create a Bootable USB Drive" /r:1)

并且因为robocopy输出表明在我试过这个

之后一切都被丢弃了
For /f %%b In ('Dir /b /s /a:d "%fp_server%\pc_images\Win7_Images"') Do (Robocopy %%b I:\Images *.wim /xo /xd "%FP_Sever%\PC_Images\Win7_images\1\ /r:1) 

所以我的问题是1.如何获得具有句点和/或空格的解析路径以及为什么RoboCopy中的目录不被忽略?

1 个答案:

答案 0 :(得分:1)

如果在内联命令的路径中包含引号,则最好使用usebackq语句中的FOR参数:

REM Updated parameters after the /f switch
REM  and changed the command to use ` instead of ' marks.
For /f "usebackq tokens=* delims=" %%b In (`Dir /b /s /a:d "%fp_server%\pc_images\Win7_Images"`) Do (...)

关于您的Robocopy命令,请确保引用%%b命令中使用的DO参数,因为它本身可能包含空格:

Robocopy "%%b" ...

更新完整命令:

For /f "usebackq tokens=* delims=" %%b In (`Dir /b /s /a:d "%fp_server%\pc_images\Win7_Images"`) Do (Robocopy "%%b" I:\Images *.wim /xo /xd "%FP_Sever%\PC_Images\Win7_images\1. Create a Bootable USB Drive" /r:1)