How do you merge two directories, or move with replace, from the windows command line without copying?

batch-file, python, windows

Solution

The move-all-files-manually workaround in python. I'm still reeling from the stupidity.

def moveTree(sourceRoot, destRoot):
    if not os.path.exists(destRoot):
        return False
    ok = True
    for path, dirs, files in os.walk(sourceRoot):
        relPath = os.path.relpath(path, sourceRoot)
        destPath = os.path.join(destRoot, relPath)
        if not os.path.exists(destPath):
            os.makedirs(destPath)
        for file in files:
            destFile = os.path.join(destPath, file)
            if os.path.isfile(destFile):
                print "Skipping existing file: " + os.path.join(relPath, file)
                ok = False
                continue
            srcFile = os.path.join(path, file)
            #print "rename", srcFile, destFile
            os.rename(srcFile, destFile)
    for path, dirs, files in os.walk(sourceRoot, False):
        if len(files) == 0 and len(dirs) == 0:
            os.rmdir(path)
    return ok

Please post a proper answer if there ever is one!

Problem

So I just wrote a quick python script to move some large directories around (all on the same drive), incorrectly assuming windows command line tools weren't a complete joke and that `move Root\Dir1 Root\Dir2` would, like windows explorer GUI, merge the contents. I really don't care whether it replaces or skips duplicate files within the folders because there aren't any. Unfortunately (in an admin command prompt), ``` C:\>mkdir a C:\>mkdir b C:\>mkdir b\a C:\>move b\a . Overwrite C:\a? (Yes/No/All): yes Access is denied. ... :O ... ?? really ??!? ... no, actually really really ??? ``` It seems the only way is to copy and delete. Painfully pathetic. Related: How can I move the contents of one directory tree into another? how to merge two folders by batch cmd how do i fix: 'access denied' with the move command in windows 7? I'm not writing code to copy files one by one. Is there any way to achieve a folder move with replace without copying? I'd prefer to use some native executable if possible. I'd also be quite happy to use python if it supported it.

Original source

Related problems