Solved

Robocopy to move subdirectores, without deleting root directory?

Posted on 2009-04-08
14
6,807 Views
Last Modified: 2016-10-04
Is it possible to use robocopy to move all folders from C:\robo\from to C:\robo\to without deleting C:\robo\from?

Here's what I'm currently using...

robocopy C:\robo\from C:\robo\to /MOVE /E /R:2 /TEE /LOG+:C:\robo\movelog.txt
0
Comment
Question by:csho
14 Comments
 
LVL 30

Expert Comment

by:flubbster
ID: 24099994
Change MOVE to MOV

MOVE deletes files and directories from source.
MOV only deletes files from source
0
 
LVL 11

Expert Comment

by:ecsrd
ID: 24100033
This will work for you:

robocopy C:\robo\from C:\robo\to /MIR /R:2 /TEE /LOG+:C:\robo\movelog.txt

Basically, just remove the /MOVE and replace with /MIR.  The /MOVE command deletes the files and directories from the source!
0
 

Author Comment

by:csho
ID: 24100095
Thanks for the quick replies, although neither of those switches are doing what I need. When all is said and done, I want the C:\robo\from directory to be empty with all of the subdirectories moved to C:\robo\to

The MOV and MIR command are both leaving the subdirectory in C:\robo\from
0
 
LVL 11

Expert Comment

by:ecsrd
ID: 24100288
err, you said you wanted the data moved "WITHOUT" deleting from the source:
"Is it possible to use robocopy to move all folders from C:\robo\from to C:\robo\to without deleting C:\robo\from?"

By default, there is no option to prevent robocopy from deleting the source directory if you use the MOVE command, and the MOV command will leave teh sub directories beneath the source directory.

What I would suggest is that (since it sounds like you're doing this often) you write a simple back script:

@echo off
robocopy C:\robo\from C:\robo\to /MOVE /E /R:2 /TEE /LOG+:C:\robo\movelog.txt
md C:\robo\from

That will move your data, and then re-create the from folder that is deleted, and also remove the sub-directories as you wanted.
0
 
LVL 11

Expert Comment

by:ecsrd
ID: 24100297
argh, wtb edit button :D - back = batch*
0
 

Author Comment

by:csho
ID: 24100347
I had thought of that, but what is actually happening is the source directory (c:\robo\from) is "in use" so it is bombing out. The folder is "live" in that other processes could be looking for that folder so deleting it isn't an option even if it is only for a nanosecond.

I'm thinking robocopy isn't the tool I should be using for this, but I love how it handles things.
0
 
LVL 11

Expert Comment

by:ecsrd
ID: 24100378
Yeah, robocopy has some limitations, it is still by far my favorite file copy utility that I aim to use as often as possible.  There are some instances that it can't handle though :S.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Expert Comment

by:ecsrd
ID: 24100413
The reason it doesn't work is because the /MOVE command actually is invoking the "RD /S" command (remove all directories and files in the specified directory in addition to the directory itself.  Used to remove a directory tree." - and the limitation is the "RD" command, since there is no switch that just removes the sub-directories, not including the base directory...
0
 
LVL 11

Accepted Solution

by:
ecsrd earned 500 total points
ID: 24100462
I have come up with a solution for you! - this will involve you placing a placeholder file in the directory that NEVER gets deleted (the base directory will only be removed if there are no files present in the directory):

Make a file called placeholder.txt in C:\robo\from

Then use the following script:

robocopy C:\robo\from C:\robo\to /MOVE /E /R:2 /XF "placeholder.txt" /TEE /LOG+:C:\robo\movelog.txt
0
 

Author Closing Comment

by:csho
ID: 31568152
Genius! I've seen lots of other workarounds out there that were much much more complicated. Thanks!
0
 
LVL 11

Expert Comment

by:ecsrd
ID: 24101092
Glad I could help!
0
 

Expert Comment

by:buchiag
ID: 36467712
Thanks, Great
0
 

Expert Comment

by:rexne1
ID: 40565573
I could not get this to work? Still shows the same error while using Robocopy V10.
0
 

Expert Comment

by:Mith Mithu
ID: 41828747
Hi is it possible to move files from One folder to another Using Robocopy ,
I want to move only Rpt files  without overwriting if there exists  File with same name,  Also files from all subfolders in the directory also shoud moved.

I have done so far Below.

@echo off
    setlocal enableextensions enabledelayedexpansion

    rem configure directories
    set "source=E:\Source /S"
    set "target=E:\Destination"

    rem move non existing files to target
    call :doMove    

    rem if we still have files
    if exist "%source%\*.rpt" (

        rem generate a timestamp
        set timestamp=_%date:/=%_%time::=%
        set timestamp=!timestamp:,=!

        rem rename the remaining files with timestamp
        ren "%source%\*.rpt" "*.!timestamp!.rpt"

        rem and move the remainig files to target
        call :doMove
    )

    endlocal
    exit /b

:doMove
    robocopy "%source%" "%target%" "*.rpt" /fp /njh /njs /ndl /xc /xn /xo /xx /mov
    goto :EOF


Result
-----------
It moves all file from the path, But it does nt move files from the subfolders inside the path.
Please help me.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
Know what services you can and cannot, should and should not combine on your server.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

760 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

20 Experts available now in Live!

Get 1:1 Help Now