Solved

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

Posted on 2014-03-12
8
345 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 45

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

9 Experts available now in Live!

Get 1:1 Help Now