Solved

Need to copy subfolders to new location and merge folders with same name

Posted on 2014-03-12
8
346 Views
Last Modified: 2014-04-04
I have a drive with thousands of folders and subfolders with files in them, and I now need to take Folder Level2 & Folder Level 3 and copy to New location as Folder Level 1 and Folder 2 (I want to leave behind folder level 1 in the orginal drive.

Note that Folder Level2 & 3 will have folder identical names in many instances, so they will need to merge in the new location.

Example:
Z:\OldFiles\ABC\123\456\*.* will get copied to Z:\NewFiles\123\456\*.*
Z:\OldFiles\DEF\001\123\*.* will get copied to Z:\NewFiles\001\123\*.*
Z:\OldFiles\HIJ\123\456\*.* will get copied to Z:\NewFiles\123\456\*.* (and merge, as there is already a folder there, now)

I had asked this question previously via Q28385722, but that solution did not merge the destination folders.

Note that the actual filenames within the folders will always be unique.
0
Comment
Question by:DebbieFost
8 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 39925136
Any other deeper folder than your examples?
0
 
LVL 13

Expert Comment

by:Santosh Gupta
ID: 39925177
Try this..

@echo off

set BaseDir=Z:\OldFiles\ABC
set DestDir=Z:\NewFiles

xcopy "%BaseDir%\*.*" "%DestDir%\" /s /e /c /q /h /r /k /o /y 

Open in new window

0
 

Author Comment

by:DebbieFost
ID: 39925222
Santosh -  you have hard coded the first level to just folder ABC - that will not pickup on all the other 1st level folders (i.e. DEF)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 83

Expert Comment

by:oBdA
ID: 39925959
Try this; since you're dealing with thousands of folders, it creates a robocopy log file for each "ABC\123" folder, plus a summary log. To reduce information overflow, the robocopy log files will only show a summary and errors, but not successfully copied files and folders. If you want full logs, remove the "/nfl" (no file list) and "/ndl" (no directory list) from the robocopy options. The robocopy log files will append if you do several runs. If you want to overwrite, remove the "+" after "/log" in the robocopy options.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=Z:\OldFiles
set TargetFolder=Z:\NewFiles
set LogFolder=C:\Temp\Log
set SummaryLog=%LogFolder%\_Summary.csv
(set Delim=	)
if not exist "%LogFolder%" md "%LogFolder%"
if not exist "%SummaryLog%" >"%SummaryLog%" echo Result%Delim%Date%Delim%Time%Delim%Source%Delim%Target%Delim%Log
for /d %%a in ("%SourceFolder%\*.*") do (
	echo [!Time!] Processing '%%~nxa'
	for /d %%s in ("%%a\*.*") do (
		echo [!Time!]	- '%%~nxs' ...
		robocopy.exe "%%s" "%TargetFolder%\%%~nxs" /e /r:0 /np /nfl /ndl /log+:"%LogFolder%\%%~nxa-%%~nxs.log" >NUL
		set LogLine=%Delim%!Date!%Delim%!Time!%Delim%"%%s"%Delim%"%TargetFolder%\%%~nxs"%Delim%"%LogFolder%\%%~nxa-%%~nxs.log"
		if errorlevel 4 (
			echo [!Time!] 	... Error!
			>>"%SummaryLog%" echo "ERR"!Logline!
		) else (
			echo [!Time!] 	... OK.
			>>"%SummaryLog%" echo "OK"!LogLine!
		)
	)
)

Open in new window

0
 

Author Comment

by:DebbieFost
ID: 39926781
Wow - that is brilliant, OBda!!!! I tested it and it works just like I want it. However, is it possible to DELETE the files from the Z:\Oldfiles locations as files are copied (i.e. MOVED) - as I will run out of disk space on my server.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39926819
Just edit line 14 and add /mov (if you want to keep the folder structure without files) or /move (if you want to delete empty folders as well) to the robocopy options.
		robocopy.exe "%%s" "%TargetFolder%\%%~nxs" /e /mov /r:0 /np /nfl /ndl /log+:"%LogFolder%\%%~nxa-%%~nxs.log" >NUL

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39979302
I've requested that this question be closed as follows:

Accepted answer: 500 points for oBdA's comment #a39925959

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now