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.
Windows 10 can automatically merge the contents of two folders that have the same name when you copy the folder from one location to another. Locate the two folders you want to merge. If they do not have the same name, rename them so that the names are identical. Select one of the two folders, and tap Ctrl+C.
rsync -av /source/ /destination/ (after checking) rm -rf /source/ You can use the -l option of the cp command, which creates hard links of files on the same filesystem instead of full-data copies. The following command copies the folder source/folder to a parent folder ( destination) which already contains a directory with the name folder.
Locate the two folders you want to merge. If they do not have the same name, rename them so that the names are identical. Select one of the two folders, and tap Ctrl+C. Navigate to the second folder’s location. Tap the Ctrl+V keyboard shortcut. The two folders will be merged automatically.
For example, if you want to move all text files from current folder to a new location you can use the below command. To move all files starting with letter ‘A’, you can use below command. Example: To move the directory ‘data’ to ‘D:\data\folder1\’ 1.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With