Script to

Discussion in 'Mac Programming' started by macstatic, Jun 4, 2016.

  1. macstatic, Jun 4, 2016
    Last edited: Jun 5, 2016

    macstatic macrumors 6502a

    Joined:
    Oct 21, 2005
    #1
    PS: I forgot to complete the title, but can't find a way to change it, nor delete the posting... :oops:


    I would like to restore some files (replace existing, damaged files from a backup). With hundreds of files spread in lots of folders/sub-folders this will take forever to do manually.

    So I was wondering if I could automate this with a script (Automator or Applescript perhaps)? In theory I believe it would do something like this:

    1) Open up a dialog window asking the for the base folder of the files to be restored (the root folder which contains all the sub-folders where the damaged files are located)
    2) Open up another dialog window asking where the backup files are located (a single folder with all the (OK) files)
    3) Looking at the backup folder, take a note of the first file's name.
    4) search through the entire backup folder (and its sub-folders) for a file with that exact same name and extension
    5) if found, move and overwrite the (damaged) file with the (OK) backup file. Possibly with a confirmation dialog first
    - if the same filename is found in several locations, present a dialog asking if said file (one by one) should be replaced or not​
    6) go back to step 1, continuing with the next file

    NOTE: a variation of the script could be made for step 2 where the exact same file/folder structure is used for the backup (of course only containing the files to replace with -the rest must be deleted using the Finder's search filtering). I assume this will speed up the restoring process as the script would only have to compare the two file structures which are identical.
    Also, as an added safety feature -in step 5, rename the original (damaged) files with a preceeding "ORIGINAL_" instead of overwriting them. Could come in handy in case something goes wrong. I can easily delete the in the finder by searching for files with said base folder using the name "ORIGINAL_".

    Does anyone know of such a script or how it could be made?
     
  2. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #2
    Do you really want to do that? Searching for files with same name will not work. Example is a file called "README.TXT" There might be many of those files in different folders

    Just do a full restore from the backup. Copy the base folder from the backup preserving its structure.
     
  3. macstatic, Jun 5, 2016
    Last edited: Jun 5, 2016

    macstatic thread starter macrumors 6502a

    Joined:
    Oct 21, 2005
    #3
    I should have explained in more detail...
    I'm restoring images and most of them have unique names. Actually, if having duplicate names is a problem I can remove those and have them restored manually as there aren't many of them.
    By doing a full restore I will overwrite the ones which have been updated (with EXIF metadata), so that'll cause more problems.
    Here's an illustration of what I'm looking for the script to do (with the base folder in this example being ~/Pictures/):
    file_filter script.png
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Take them out. I can see no obvious way of scripting those, except by doing some image analysis evaluating similarity, or by presenting all the images and asking the user how to replace. There aren't any such image analysis commands available by default, and the work involved in building a script to present an image-picker would far exceed doing it manually.


    So now the problem boils down to:
    For each file in a Replacements folder:
    1. Find a file with an identical name somewhere in a designated target folder hierarchy.
    2. Replace the found file with the replacement.​

    In theory, the script should also do the following, for safety:
    Search the entire target folder hierarchy for a matching filename.
    If any name appears more than once as a replaceable item, DO NOTHING and notify the user.​

    It might also do the following:
    If any file from Replacements matches no target filenames, notify the user.​

    When it comes to actually doing this, consider a couple points.

    First, there's no need to repeatedly search the disk for every file in the Replacements folder. Instead, traverse the target hierarchy once, and record the pathname of every file. Now it'll be much faster to search this single file, rather than searching through the directories every time. The 'find' command-line tool can produce this list. The 'grep' command can search the list.

    Second, I'd build this is a "script-making script". That is, it wouldn't actually move files from Replacements to their final destination. Instead, it would produce an output file of commands that will move the files. Then you can review that file of commands, and take out anything you don't completely agree with. Then you execute the resulting commands by running a shell that interprets the commands.
     
  5. macstatic thread starter macrumors 6502a

    Joined:
    Oct 21, 2005
    #5
    I agree -using such techniques to locate duplicates sounds like overcomplicating things.
    The most obvious solution which comes to mind is present the user with a small preview of the existing image next to the first (backup) image found with buttons marked [Cancel], [Replace] and [Next image].

    But, (and this is ofcourse an obvious thing which I've overlooked) it's not even possible to have those files with the same name distributed into their various locations, as a single folder of backup files can't contain several copies of the same file (only the first of many files with the same name would be replaced). In that case I need to keep the original structure as well.
    But would such a scenario make it easier for the script to handle?

    I mean, if I had an original filepath (the damaged file, to be replaced) and a backup path (the OK file which I want to move over and replace the original file with) and only the base folder would be different.... :

    /Volumes/MacHD/Users/UncleBill/Pictures/2015/01/Niagara_falls/img_21009.jpg
    /Volumes/backup2/Pictures/2015/01/Niagara_falls/img_21009.jpg

    I was thinking that if the script was told at the very start that the base folder for the backup would be /Volumes/Backup2/ and the original would be /Volumes/MacHD/Users/UncleBill/ then the script would instantly know where that file should be on the other drive....


    Yes, sounds good. And any "leftover" files (i.e. those not moved over) could manually be manually be moved over to the original drive. But I would also have to keep track of all those file locations myself, or use the Finder's search feature in order to track them down one by one. Preferrably color labelling the OK files first to avoid confusing regarding which file has been replaced and which ones haven't yet.

    Without understanding how the script should be written it appears a backup with an identical file structure would be a better way to do this. Put in more practical terms I'm probably looking for backup software which allows you to restore only certain files (in my case just the PNG files) as I assume that software would already understand how a path can be the same except the base part.


    Good point, have the script generate a database of some sort then.

    I'm not sure I quite follow.
    You're saying that the script should not do the actual file-handling, but generate another script (which does that) based on certain parameters and information gathered?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Much easier. Then it would become a simple syncing problem, because it eliminates the problems of "find where this goes and somehow resolve any duplicates".

    Yes, that's a sync. The 'rsync' command in Terminal can do this (and more).

    Apps that use 'rsync' would also be capable of this. Carbon Copy Cloner uses 'rsync'.

    Read Carbon Copy Cloner's website. Also search the Mac App Store for syncing apps.

    Yes. The resulting script would be a series of 'cp' or 'mv' commands. The point is that you can review them all before any are executed. Or if you're just doing a test, you don't have to run it at all, you just look at it and see if it would do the right thing.
     
  7. macstatic thread starter macrumors 6502a

    Joined:
    Oct 21, 2005
    #7
    Actually I already use something similar (Chronosync) and it would be ideal if I could just restore those specific files with it. But I'm told it's not possible to restore just specific files.

    I was hoping I could just filter it to restore all .PNG files within a certain base folder and it would leave everything else alone, but from what I understand you can only restore complete folders (or perhaps manually select certain files).
    Is CCC different in this respect?
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    https://bombich.com/kb/ccc4/restoring-from-disk-image

    If you do not want to restore everything, choose "Selected files..." from the Clone menu (below the Source selector) and deselect any item that you do not wish to restore.

    If you want to know exactly how it works, I suggest trying it. Make "backups" to disk images, then "restore" to other disk images. That way you aren't risking any real disks.
     

Share This Page