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
Solved

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

Posted on 2014-03-12
8
350 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 84

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 84

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

809 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