4

how to rollback copy of files from SOURCE to DESTINATION? Let me give examples based on Magento (but it does not matter).

When I install a new module, I get a zip file, I extract it in a temporary folder like c:\temp\my_new_module

I have a bunch of directories and files extracted there, from the zip. Some of the directories map Magento directories, some are new directories.

Then I copy files from the main path (c:\temp\my_new_module) to magento root path (c:\magento)

Let's say I test the module few days later, and I don't like it, how can I rollback DESTINATION? ie, how to remove from DESTINATION=c:\magento:

  1. new directories with new files from c:\magento (that were copied from SOURCE)
  2. new files added to existing Magento directories

It means I want to preserve files and folders in DESTINATION that were already there before the copy paste! I don't want to touch anything from SOURCE (neither remove or whatever).

I'm looking for a script to do that (command line), I guess it would compare SOURCE and DESTINATION.

For Each File from SOURCE
    if FILE_EXIST in DESTINATION 
    Then DELETE FILE From DESTINATION
    If DIRECTORY_IS_EMPTY in DESTINATION 
    THEN DELETE DIRECTORY in DESTINATION

(the latter will preserver non-empty directories, ie the one that were created before the copy)

thanks for any help, Rod

Rod
  • 138

1 Answers1

2

My question is, days after I did that, how can I automate the removal of all Files I have copied?

You can use Robocopy to say look at a [source] directory for all files and subfolders and check recursively against another [destination] directory for the same files that already exist in source.

You can use the options to have it delete files from the source if it finds these same files already exist in the destination (the folder you copy these to) already, and it'll do that recursively but not remove any folders.

You can also use options to have it NOT copy files in the source that are not in the destination directory to NOT copy those over since you will complete the copy operation manually.

You only need to delete FILES from the source recursively that already exist in the destination. You will complete the copy operation of new files from source to destination manually, and only need something to help simplify cleaning of the files in source which have already been copied over manually to the destination.


Robocopy Script

I tested the scenario listed above and it worked just as I explain and just as expected so this method has been tested

@ECHO ON
SET Source=F:\TestSource
SET Destination=F:\TestDest

ROBOCOPY "%Source%" "%Destination%" *.* /S /IS /PURGE /MOV /NOCOPY
GOTO EOF

Further Resources

         /S :: copy Subdirectories, but not empty ones.
    /NOCOPY :: COPY NO file info (useful with /PURGE).
     /PURGE :: delete dest files/dirs that no longer exist in source.
       /MOV :: MOVe files (delete from source after copying).
        /IS :: Include Same files.