Batch copy multiple folders and their subfolders into a merge folder

1

1

I have a folder X:\Export that has several folders:

X:\Export
+---Export1
|   \---various files …
+---Export2
|   \---… and subfolders
+---Export3
|   \---etc.
\---etc.            (names vary widely)

Each “Export” folder has the same subdirectory structure, but they have different files (with possibly some name collisions). I would like to copy all the subfolders and the files of X:\Export\Export1, X:\Export\Export2, X:\Export\Export3, etc., to a folder X:\Export\merged, keeping the subdirectory structure.

Pseudocode of what I would like to do but cannot get working properly:

create new folder "merged"
for (every folder X in a given directory Y (i.e., X:\Export)
    copy every file in X keeping directory structure to "merged"
If conflict then overwrite

DjLenny

Posted 2012-10-12T15:35:59.377

Reputation: 11

Answers

1

How about

@echo off
md merged
for /f "delims=" %%f in ('dir /b /ad') do (
    if not %%f==merged xcopy "%%f" merged /s /y
)

The /y option causes files to be overwritten without asking for confirmation.  Replace /s with /e if you want to copy empty subdirectories.


Additional explanation: for /f "options"(optional) variable in ('command') runs command, captures the output, assigns tokens (strings) from that output to the variable, and then runs the command(s) after the dodir /b means show only filenames (no attributes, no . and ..); dir /ad means list only directories (no files).

The default behavior for for /f is to create a token from each line of output by taking all characters up to the first space.  The "delims=" option tells it to take the entire line.  This bit of paranoia makes the script work even if you have directory names with spaces in them.  I probably could have gotten away with saying xcopy %%f merged; but I need the double quotes around %%f (xcopy "%%f" merged) to handle the case where %%f contains spaces.

Scott

Posted 2012-10-12T15:35:59.377

Reputation: 17 653

It works if run through command prompt from that directory, however if run by running the .bat file it defaults to c:\Windows. how can i make it run in the directory where the .bat is located

forgot to mention that occasionally this is on a network drive. – DjLenny – 2012-10-12T17:48:50.830

@DjLenny: “… if run by running the .bat file it defaults to C:\Windows.”  I don’t understand how or why that would happen — what version of Windows are you running?  But to fix the problem, put a cd into the script; e.g., cd/d X:\Export.  If you need to be able to use it in different locations, make it take an argument (cd/d %1). – Scott – 2012-10-12T19:24:29.560

wondows 7, i figured out my problem, if i run it locally it works, if it is on an Unmapped network drive it doesnt work because of the UNC address of the file. Once i mapped the drive it worked just fine, also it worked in powershell. thanks for the script, works like a charm.

now a question for future understanding:

What is the relevance of "delims=" and ('dir /b /ad') the rest i understand' – DjLenny – 2012-10-12T21:39:10.250

@DjLenny: I added more explanation to my answer.  (You can get even more information by typing dir /? and for /?.)  If my answer solves your problem, please “accept” it. – Scott – 2012-10-12T22:35:29.453