Merge two folders - please help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dosmushi
Posts: 4
Joined: 17 Jul 2013 04:43

Merge two folders - please help

#1 Post by dosmushi » 17 Jul 2013 04:55

Hi Folks,

Im new to this forum and just started working on DOS.. i have a new requirement like,

1. Copy a Folder (Folder 1) which contains sub folders (Folder 2 Folder 3) to a new Location
2. Copy a second Folder (Folder 4) which contains same sub Folders (same folder names Folder 2 and Folder 3) and some additional files
3. And the challenge is in the Destination Folder step 1 and step 2 files should be merged not overwritten
4. Another challenge is the Destination folder name should be dynamically created(lets say using the appended name of Folder 1 and Folder 2)
5. Additionally all these folders are in my network drive.

So please help, i need to give a batch script for the above to my boss by tomorrow. Thanks in Advance.

Eg Scenario.

Folder 1 content i copied to Destination Folder .

Folder 2 content i copied to the same destination folder.

DestinationFolder should contains the Folder 1 and Folder 2 merged content and there are many sub folders with the same name in Folder 1 and 2.

Thanks

dosmushi
Posts: 4
Joined: 17 Jul 2013 04:43

Re: Merge two folders - please help

#2 Post by dosmushi » 17 Jul 2013 05:09

Requirement as below again..

Source
\\100.X.X.X\Folder1

Source
\\100.X.X.X\Folder10\FolderX

Common Destination
\\100.X.X.X\FolderDestination\Folder1FolderX

This folder should contain the merged (If there are any sub folders with same names) content of Folder1 and FolderX (Including sub folders and all the files)

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Merge two folders - please help

#3 Post by Samir » 17 Jul 2013 10:43

Looks like a couple of different requirements.

If your folder FolderX's are named the same, a simple xcopy command will do it unless there are identical file names. For identical names, you'll have to rename something unless you want the files actually merged.

I'm not sure I follow the others requirements completely. Some examples will help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merge two folders - please help

#4 Post by foxidrive » 17 Jul 2013 18:07

If there are any files with the same name it will ask you what to do.

Code: Select all

@echo off
xcopy /s/h/e/k/f/c  "\\100.X.X.X\Folder1\*.*"  "\\100.X.X.X\FolderDestination\Folder1FolderX\"
xcopy /s/h/e/k/f/c  "\\100.X.X.X\Folder10\FolderX\*.*"  "\\100.X.X.X\FolderDestination\Folder1FolderX\"

dosmushi
Posts: 4
Joined: 17 Jul 2013 04:43

Re: Merge two folders - please help

#5 Post by dosmushi » 17 Jul 2013 19:08

Thank you both.. i came up with the below script..

@echo off


set Source1=C:\New\Folder1
set Source2=C:\New\Folder2
Set Dest=C:\New\Newfolder

xcopy /s/h/e/k/f/c %Source1%\*.* %Dest%\
xcopy /s/h/e/k/f/c %Source2%\*.* %Dest%\

But the problem is, it overwrites the files in the destination. My intention is to do the merging.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merge two folders - please help

#6 Post by foxidrive » 17 Jul 2013 20:34

The /-y switch causes a prompt to appear on filename clashes

Code: Select all

@echo off

set "Source1=C:\New\Folder1"
set "Source2=C:\New\Folder2"
Set "Dest=C:\New\Newfolder"

xcopy /s/h/e/k/f/c /-y "%Source1%\*.*" "%Dest%\"
xcopy /s/h/e/k/f/c /-y "%Source2%\*.*" "%Dest%\"

dosmushi
Posts: 4
Joined: 17 Jul 2013 04:43

Re: Merge two folders - please help

#7 Post by dosmushi » 18 Jul 2013 00:55

thanks you..Now i am moving to my complete requirement...Trying the below. Gor help from many people. Its working fine if i need to append 2 folder names from two different paths.

@ECHO OFF


Set NasPath=C:\New\Folder1
Set NasFolder=%NasPath%
:GetFolder
Set GetFolderTemp=%NasFolder:*\=%
If Not %GetFolderTemp%==%NasFolder% (
Set NasFolder=%GetFolderTemp%
Goto :GetFolder
)




Exit /B

Set NasPath1=C:\New\Folder2

Set NasFolder1=%NasPath1%


:GetFolder

Set GetFolderTemp1=%NasFolder1:*\=%

If Not %GetFolderTemp1%==%NasFolder1% (
Set NasFolder1=%GetFolderTemp1%
Goto :GetFolder
)


Echo NasPath1=%NasPath1%
Echo NasFolder1=%NasFolder1%
Echo NasPath=%NasPath%
Echo NasFolder=%NasFolder%

Set Dest=C:\New\%NASFOLDER%_%NasFolder1%
ECHO %Dest%

Exit /B



If i have a folder structure like below.

SourceA= Folder1\Folder2\Folder3
SourceB=Folder4\Folder5

And my destination Foldername should be,

Dest=FolderX\FolderX1\Folder2_Folder5

How to achieve this?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merge two folders - please help

#8 Post by foxidrive » 18 Jul 2013 02:30

dosmushi wrote:How to achieve this?


What do you want to do? Please describe your aim.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Merge two folders - please help

#9 Post by Samir » 19 Jul 2013 15:46

foxidrive wrote:
dosmushi wrote:How to achieve this?


What do you want to do? Please describe your aim.
Yeah, I was a bit confused too. It seemed like an easy xcopy solution, but the conbined foldername requirements aren't clear.

Post Reply