-- TreeShare-MediaRenameUNDO.sql
/* 
2017-07-05 Tom Holden ve3meo
Used after TreeShare-MediaRenameStep2.sql 
Requires the temp table zMediaNewNames created by that script so 
the database must remain open in the SQLite manager.

Changes the media links back to the Ancestry names and generates 
commands in temp table zMediUndoRenameCMD for a batch file to 
rename the media files back to the Ancestry names.
*/

-- generate Windows RENAME commands to change back to Ancestry filenames
DROP TABLE IF EXISTS zMediaUndoRenameCMD
;

CREATE TEMP TABLE zMediaUndoRenameCMD
AS
SELECT 'RENAME ' || ' "' || NewName ||'"' || ' "' || OldName || '"' AS CMD
FROM zMediaNewNames
;

-- restore media links to Ancestry filenames
UPDATE MultiMediaTable 
SET MediaFile = (SELECT OldName FROM zMediaNewNames z WHERE MultimediaTable.MediaID = z.MediaID)
WHERE MediaID IN (SELECT DISTINCT MediaID FROM zMediaNewNames)
;

---- Instructions ----
SELECT
'DO NOT CLOSE the SQLite manager until you are finished renaming or undoing.

Your Media Gallery will now show broken links for the renamed downloaded
files from Ancestry.com as it now points to the original Ancestry names.

To fix these broken links, copy the batch commands from zMediaRenameUndoCMD to a 
text editor. Delete the 1st line "CMD".
Save the text file as a .BAT or .CMD file to the media folder having the
same name as the database file and is a subfolder of its folder.
Open the Command Line Interpreter on this media folder and execute the file.

            ---------REDO---------------
DO NOT CLOSE the SQLite manager if you wish to redo the renamimg and relinking
because that will lose the temp tables.

Re-execute TreeShare-MediaRenameStep2.sql to change the media links and rerun 
the batch commands from zMediaRenameCMD. 
' 
AS Instructions

