Copy Existing Reference Numbers in Gedcom File.fh_lua

--[[
@Title: Copy Existing Reference Numbers in Gedcom File
@Type: Standard
@Author: Jane Taubman
@Version: 1.0
@Keywords: gedcom
@LastUpdated: 
@Licence: This plugin is copyright (c) 2021 Jane Taubman and contributors, and is licensed under the MIT License
which is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)
@Description: 
This plugin is designed to work on a Gedcom File PRIOR to opening the file in Family Historian. 
It will copy the currect record IDs from the old system to either the Custom ID, Automated ID or a UDF value.
This can then be used if needed to cross reference the old numbers, should Family Historian need to renumber.
As it does for non numeric fields or for very long numbers.
To use, please open the sample Project or "another" Project,
then run the program and select your gedcom file.
]]

fhu = require "fhUtils"
fhf = require "fhFileUtils"
stringx.import()
-------------------------------------------------------
-- Main Function
-------------------------------------------------------

function main()
    pFilename = ""
    pOption = 0
    -- Prompt User to Confirm Options
    ret, pFilename, pOption =
    iup.GetParam("Copy Existing Reference Numbers in Gedcom File", param_action,
    "File: %f\n"..
    "Write to: %l|Custom ID|Automated ID|_OLDREF (UDF)|\n",
    pFilename, pOption)
    if (ret == 0 or ret == false) then
        return
    end
    local pType = ''
    if pOption == 0 then
        pType = 'REFN'
    elseif pOption == 1 then
        pType = 'RIN'
    end
    local tText = fhf.readTextFile(pFilename,true):splitlines()
    local tNewText = {}
    -- Check for Custom IDs if option 0 selected
    if pOption == 0 then -- Custom ID
        for i,sLine in ipairs(tText) do            
            if sLine:sub(3,6) == 'REFN' then
                fhMessageBox('Aborted: Custom IDs already exist in \r\n'..pFilename,'MB_OK','MB_ICONSTOP')
                return
            end
        end
    end
    -- Check for Automated IDs if option 0 selected
    if pOption == 1 then -- Automated ID
        local bHead = false
        for i,sLine in ipairs(tText) do
            if sLine:sub(1,1) == '0' then
                if sLine:sub(3,6) == 'HEAD' then
                    bHead = true
                else
                    bHead = false
                end
            end
            if sLine:sub(3,5) == 'RIN' and bHead == false then
                fhMessageBox('Aborted: Automated IDs already exist in \r\n'..pFilename..' \r\n \r\n','MB_OK','MB_ICONSTOP')
                return
            end
        end
    end
    --
    -- Copy Table
    --
    local id
    for i,sLine in ipairs(tText) do
        if sLine:sub(1,1) == '0' then
            if sLine:sub(3,6) == 'HEAD' then
                bHead = true
            else
                bHead = false
            end
        end
        table.insert(tNewText,sLine)
        if sLine:startswith('0') and bHead == false then
            _,_,id = sLine:find('@(.*)@')
            if id then
                table.insert(tNewText,'1 '..pType..' '..id)
            end
        end
    end
    local sNewFile = pFilename:replace('.ged','-new.ged')
	 if fhf.fileExists(sNewFile) then
		local ans = fhMessageBox(sNewFile..' already exists overwrite?','MB_YESNO','MB_ICONQUESTION')
		if ans == 'No' then
			fhMessageBox('File save cancelled','MB_OK','MB_ICONSTOP')
			return
		end
	 end
    sText = table.concat(tNewText,'\r\n')
    fhf.createTextFile(sNewFile,true,true,sText)
	 fhMessageBox(sNewFile..'\r\nhas been created with the new IDs included','MB_OK','MB_ICONINFORMATION')
end

main();

Source:Copy-Existing-Reference-Numbers-in-Gedcom-File.fh_lua