如何使用批处理文件将多个文件夹复制到另一个路径?

时间:2021-01-20 02:06:54

How to make it go through certain folders(ex. 1-8 on the drive E:) and their trees and copy them on F: in the batch file(my doesn't work):

如何让它通过某些文件夹(例如驱​​动器E :)上的1-8和它们的树并将它们复制到F:批处理文件中(我的工作不正常):

set drive=E:
for %%a in (1,2,3,4,5,6,7,8) do (
  set folder=%%a
   for /R "%drive%\%%a" %%b in (*) do (
        copy "%%b" %drive%\%folder% 

2 个解决方案

#1


0  

I think the syntax you want is

我认为你想要的语法是

for %%F in (1 2 3 4 5 6 7 8) do (
    xcopy /e e:\%%F  f:\
)

#2


0  

You're trying to set and reuse an environment variable in a loop. This cannot work since cmd expands all environment variables when parsing a command, not when running it. So you need to enable delayed expansion:

您正在尝试在循环中设置和重用环境变量。这不起作用,因为cmd在解析命令时扩展所有环境变量,而不是在运行它时。所以你需要启用延迟扩展:

setlocal enabledelayedexpansion
set drive=E:
for %%a in (1,2,3,4,5,6,7,8) do (
  set folder=%%a
  for /R "%drive%\%%a" %%b in (*) do (
    copy "%%b" %drive%\!folder! 
  )
)

(you were also missing a few closing parenthesis, I added those for you)

(你也错过了一些右括号,我为你添加了这些括号)

But you could just as well use %%a. It should still exist in the inner loop ...

但你也可以使用%% a。它应该仍然存在于内循环中......

set drive=E:
for %%a in (1,2,3,4,5,6,7,8) do (
  for /R "%drive%\%%a" %%b in (*) do (
    copy "%%b" %drive%\%%a 
  )
)

#1


0  

I think the syntax you want is

我认为你想要的语法是

for %%F in (1 2 3 4 5 6 7 8) do (
    xcopy /e e:\%%F  f:\
)

#2


0  

You're trying to set and reuse an environment variable in a loop. This cannot work since cmd expands all environment variables when parsing a command, not when running it. So you need to enable delayed expansion:

您正在尝试在循环中设置和重用环境变量。这不起作用,因为cmd在解析命令时扩展所有环境变量,而不是在运行它时。所以你需要启用延迟扩展:

setlocal enabledelayedexpansion
set drive=E:
for %%a in (1,2,3,4,5,6,7,8) do (
  set folder=%%a
  for /R "%drive%\%%a" %%b in (*) do (
    copy "%%b" %drive%\!folder! 
  )
)

(you were also missing a few closing parenthesis, I added those for you)

(你也错过了一些右括号,我为你添加了这些括号)

But you could just as well use %%a. It should still exist in the inner loop ...

但你也可以使用%% a。它应该仍然存在于内循环中......

set drive=E:
for %%a in (1,2,3,4,5,6,7,8) do (
  for /R "%drive%\%%a" %%b in (*) do (
    copy "%%b" %drive%\%%a 
  )
)