Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question

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.

share|improve this answer
    
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 Oct 12 '12 at 17:48
    
@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 Oct 12 '12 at 19:24
    
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 Oct 12 '12 at 21:39
    
@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 Oct 12 '12 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .