7

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

user2434611
  • 125
  • 2
  • 3
  • 6
  • possible duplicate of [Copy Files with Their Folder into another Directory using XCopy](http://stackoverflow.com/questions/7304359/copy-files-with-their-folder-into-another-directory-using-xcopy) – Ken White May 30 '13 at 01:09

2 Answers2

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"
  )
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • 1
    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. – foxidrive May 30 '13 at 02:43
  • 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 – user2434611 May 30 '13 at 08:19
  • What "small issue" , please explain. – Endoro May 30 '13 at 08:25
  • 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 again – user2434611 May 30 '13 at 08:39
  • @Endoro 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 – – user2434611 May 30 '13 at 08:41
  • You mean: copy `D:\share\t1` with subfolders, all other without subfolders? – Endoro May 30 '13 at 08:47
  • 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) – user2434611 May 30 '13 at 09:15
  • 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. – Endoro May 31 '13 at 06:07
  • @Endoro 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 destination – user2434611 May 31 '13 at 06:59
  • 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. – Endoro May 31 '13 at 07:10
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
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
  • 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. – Ken White May 30 '13 at 01:34
  • @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. – Matt Williamson May 30 '13 at 01:40