Solved

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

Posted on 2014-03-12
8
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 36

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 85

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 85

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 48

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

623 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