how to copy multiple files to a folder using xcopy
Asked Answered
P

2

8

would you please let me kno how can I copy multiple directories located in different locations to a backup directoy

sources(directories) are D:\share\t1 , D:\new\t3 , C:\media\t4 F:\save\bank destination directory is C:\shared\backup

thanks in advance

Persist answered 30/5, 2013 at 1:1 Comment(1)
possible duplicate of Copy Files with Their Folder into another Directory using XCopyNeoteny
G
7

Why not a for loop? I love it and it is the best fit for this arcane question:

 For %%a in (
 "D:\share\t1"
 "D:\new\t3"
 "C:\media\t4"
 "F:\save\bank"
  ) do (
xcopy /s /d "%%~a" "c:\shared\backup"
  )
Garb answered 30/5, 2013 at 2:38 Comment(10)
If you enclose the paths in double quotes and use "%%~a" then this allows the user to use long pathname elements as well as short pathnames.Grettagreuze
Thank a million, this for loop works perfect , only one small issue that I'm sure can be fixed by using another option but I'm not expert as youPersist
What "small issue" , please explain.Garb
sorry for clicking enter mistakenly , Thank a million, this for loop works perfect , only one small issue that I'm sure can be fixed by using another option but I'm not expert as you . this loop copies over all "files" which are under those 4 directories to destination I want to copy over t1 directory and all content underneath of it , thanks againPersist
@Garb Thank a million, this for loop works perfect , only one small issue that I'm sure can be fixed by using another option but I'm not expert as you . this loop copies over all "files" which are under those 4 directories to destination I want to copy over t1 directory and all content underneath of it , thanks again –Persist
You mean: copy D:\share\t1 with subfolders, all other without subfolders?Garb
no , I need to copy D:\share\t1 with subfolders , files (eveything under it + copy D:\new\t3 with subfolders and files + copy C:\media\t4 with subfolders and files + copy F:\save\bank with subfolders and files , I just did some search for options to use with xcopy, I believ /e shall do the job ,but it doesn't . xcopy /s /d /e "%%~a" "c:\shared\backup" copies over the subfolders and files under t1 but not "t1" itself (t1 is just an example, i want copy over all 4 in same fashion)Persist
if I give the command xcopy /s /d "d:\share\t1" "c:\shared\backup"it copies all files in t1 and all files in all subfolders of t1 to "c:\shared\backup". I tested it here.Garb
@Garb if i give xcopy /d /s /e /i /h /r "d:\share\t1" "c:\shared\backup\t1 then it works for one copy but dont know how to use it in your loop to pick "t1" (folder name) from source to create the same structure in destinationPersist
If you use the option /d only older files will be overwritten by newer ones, see help xcopy on the command line. And with my Windows version (XP) the folder t1 was created sucessfully.Garb
S
0

You can use a for loop to do this.

Try:

 For %%a in (D:\share\t1,D:\new\t3,C:\media\t4,F:\save\bank) do xcopy %%a c:\shared\backup
Skewback answered 30/5, 2013 at 1:27 Comment(2)
No, you don't. This is a hard-coded list of folder names. There's no advantage (or need) for a loop; you can achieve the same thing g with r separate lines, and make it easier to read and maintain. The loop is only necessary if the content is variable.Neoteny
@Ken White - you're right. I think this is plenty easy to read but I write a lot of batch files. I updated my answer.Skewback

© 2022 - 2024 — McMap. All rights reserved.