Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-12
8
Medium Priority
?
361 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
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 2000 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 49

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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 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