Search code examples
windowsbatch-filecommand-linexcopyrobocopy

Batch file to copy and rename files


I need a batch file that:

  1. Copies all items (directories and subdirectories) from folder a to folder b
  2. If I overwrite a file name/folder in folder a then it will correctly rename file/folder in folder b

I have accomplished the first task:

@echo off
xcopy "D:\...\abc\*.*" "D:\...\def" /E 
pause

Above copies all the content in the folder to by destination folder i.e.

Folder a: 
    -folder1, folder2
    -fileA, fileB

Is correctly present in folder b. However, if I rename a file in folder a and run the script:

rename folder1 -> folder0

Then this is how file structure looks:

Folder b:
    -folder1, folder2, folder0
    -fileA, fileB

I want folder1 to be renamed to folder0, how do I do this?

I could overwrite everything (not sure how with xcopy) but is there another solution to this?

I've looked at the various flags, but can't find what I'm after.


Solution

  • I think what you want it robocopy

    You can mirror your folderA to folderB on execution of this batch file:

    robocopy "D:\...\abc" "D:\...\def" /MIR /FFT /Z /XA:H /W:5
    

    A great tutorial can be found here, explaining the various params