简体   繁体   中英

How do I copy multiple folders to another path with a batch file?

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):

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% 

I think the syntax you want is

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

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:

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 ...

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 
  )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM