Search code examples
windowsbatch-fileregistry

Shortening a script, and making it easy to add new variables for the future


I have made a script that deletes some registry keys. I have set all the key-names as variables, and the destination that leads to them as variables too.

I have added 3 destinations as variables, where I want the same registry-keys to be deleted in all 3 destinations.

As I am beginner at batch, I have not been able to make a sophisticated script, and the commands needed to delete all these registry-keys in all these 3 destinations, turn out to be really long and clumsy to say the least.

I also need this code to be future-secured, so in case we need a new registry key deleted in these 3 destinations, we'll be able to just add a new REG_KEY variable, and it will still work. Just another minor change to the code would be fine too.

Could anyone help me shorten it down somehow? You'll get an idea looking here: http://pastebin.com/VnA16y5i

Thanks in advance.


Solution

  • @echo off
        setlocal enableextensions disabledelayedexpansion
    
        for /f "delims==" %%a in ('set REG_KEY  2^>nul') do set "%%a="
        for /f "delims==" %%a in ('set REG_PATH 2^>nul') do set "%%a="
    
        SET "REG_KEY01={0EA09877-34E9-4160-B2DE-E7C7703E49ED}"
        SET "REG_KEY02={2F3A6749-B379-4879-9AF8-5C0F04084C74}"
        SET "REG_KEY03={350612EB-55FE-47DC-8E07-197B2409909B}"
        SET "REG_KEY04={628ED0F8-590B-49CF-A525-A1696BD79304}"
        SET "REG_KEY05={69BCC264-0D43-469F-8434-31E738982E7B}"
        SET "REG_KEY06={80416A15-214B-4F25-A025-ED6E875631F2}"
        SET "REG_KEY07={8EC141DE-D310-4A57-B363-02E00627B3F0}"
        SET "REG_KEY08={8EC376A3-F279-47D7-97AA-7BA2A2EB006E}"
        SET "REG_KEY09={915EABF2-2C1A-45C3-89DF-067C1AD39649}"
        SET "REG_KEY10={9AA9FEE7-9F99-4E69-947A-49F7DA0DDA3A}"
        SET "REG_KEY11={A43014F4-44F8-4539-8F87-C8471CB810B1}"
        SET "REG_KEY12={B242E104-74A3-4A32-B665-58677B671A9C}"
        SET "REG_KEY13={F63E747C-5B51-4A6E-9413-BF258F4653F3}"
        SET "REG_KEY14={AAAB700A-DDB7-4298-AB4B-B6E9F785059C}"
    
        SET "REG_PATH00=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components"
        SET "REG_PATH01=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Caphyon\Advanced Installer\LZMA"
        SET "REG_PATH02=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    
        for /f "tokens=1,* delims==" %%o in ('set REG_PATH'
        ) do for /f "tokens=1,* delims==" %%j in ('set REG_KEY'
        ) do echo reg delete "%%p\%%k" /f /va
    

    This code retrieves the content from the environment variables using two set commands, one set REG_PATH command to retrive the paths and, for each of the paths, a set REG_KEY to retrieve the list of the keys. The output lines are splitted to separate the name of the variable and the value inside it.

    To ease the management, variable numbers have been padded to ensure the order of execution match the definition order.

    reg delete operations are only echoed to console. If the output is correct, remove the echo command