循环遍历目录,使用bat文件上传目录

时间:2016-07-15 17:57:17

标签: windows batch-file

我有一个bat文件,其中一部分看起来像:

set test=test
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F
xcopy ..\dir c:\path\%test%\dir\ /S /E /F

for循环不起作用,但xcopy会起作用。如果我将上面目录的内容移动到当前目录并更改代码以删除" .. \":

for /d %%x in (*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F

它有效。有人可以告诉我为什么for循环中的bat脚本无法查找目录?我接近这个错误吗?

编辑:我现在已经将命令更改为看到答案但仍然无效:

for /d %%~nxx in (..\*.%MUI%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F

我收到错误:

%~nxx was unexpected at this time

编辑#2: 我仍然无法让它工作,我的命令看起来像

for /d %%x in (..\*.%test%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F

for /d %%x in (..\*.%test%) do xcopy "%%x" c:\temp\%test%\%%~nxx\ /S /E /F

1 个答案:

答案 0 :(得分:0)

它可以。但是%%x将包含..\xyz.test,而不是xyz.test,这可能不是您想要的xcopy目标。

将其替换为%%~nxx(“名称和x的扩展”)以切断路径。

for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%~nxx\ /S /E /F