Link to home
Start Free TrialLog in
Avatar of rhiancohen
rhiancohenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Registry script to check registry entries and add an entry in

Hiya,

I am trying to find a bat script to do the following

1. The script searches through the registry for any values in the keys below which relate to Excel addins and if there are already values there, ignore them and then add 2 Excel Addins to the next available OPENX keys

HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN1
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN2
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN3
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN4

I had some help from someone on here to use a script which kind of reversed this idea and checked through the OPEN keys to see if a value existed and if so deleted it which is below but didn't know if something similar could be used for this query

@echo off
setlocal enabledelayedexpansion
set RemoveList="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam" "D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"
for %%a in ("OPEN" "OPEN1" "OPEN2") do (
	set Data=
	for /f "tokens=2*" %%o in ('reg.exe query "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options" /v "%%~a" 2^>NUL') do set Data=%%~p
	for %%r in (%RemoveList%) do (
		if /i "!Data!"=="%%~r" (
			ECHO reg.exe delete "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options" /v "%%~a" /f
		)
	)
)

Open in new window

Avatar of Bill Prew
Bill Prew

Can you ellaborate on what would be the criteria to me "any values in the keys below which relate to Excel addins" ?

~bp
Try the script below.
Set any number of variables "AddList[x]" to the Addins you want to add.
Set the variable LastIndex to the last index you want to scan.
Set the variable Quiet to 1 to suppress any output except error messages.
@echo off
setlocal enabledelayedexpansion
set AddinList[1]="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam"
set AddinList[2]="D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"
set Quiet=0
set LastIndex=10
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
for /f "tokens=1* delims==" %%a in ('set AddinList[') do (
	if %Quiet%==0 echo Processing '%%b'
	set Value=
	set Data=%%b
	set Data=!Data:"=\"!
	for /l %%i in (0, 1, %LastIndex%) do (
		if "!Value!"=="" (
			if %%i==0 (set TestValue=OPEN) else (set TestValue=OPEN%%i)
			if %Quiet%==0 echo Checking !TestValue! ...
			reg.exe query "%Key%" /v "!TestValue!" >NUL 2>&1
			if errorlevel 1 (
				if %Quiet%==0 echo ... free.
				set Value=!TestValue!
			) else (
				if %Quiet%==0 echo ... already used.
			)
		)
	)
	if "!Value!"=="" (
		echo Unable to find an empty index in the range of 0..%LastIndex%
		goto :eof
	) else (
		reg.exe add "%Key%" /v "!Value!" /t REG_SZ /d "!Data!" /f >NUL
		if errorlevel 1 (
			echo ERROR: Could not create value '!Value!' with data '!Data!' at '%Key%'
		) else (
			if %Quiet%==0 echo Value successfully added.
		)
	)
)

Open in new window

Avatar of rhiancohen

ASKER

To elaborate, our team of developers log into a Terminal Server Farm and they may add for example. They probably won't all add the same addins

Analysis Toolpack
Euro Currency Tools
Solver Addin
Custom company addin 1
Custom company addin 2

We have a script on this particular Terminal Server farm  which looks for 2 Excel addins which are added from another Terminal Server Farm and removes them or Excel grumbles when you open it on this farm that the addins don't exist which they don't.

However when they log back into the other Terminal Server Farm where the addins exist, they need those 2 Excel Addins loaded back in automatically. However as they are roaming profiles, when they log into this server there will already be OPEN values for whatever addins follow their profile so we need to add the 2 Excel addins to the next available empty OPENx keys

Hope this makes sense
Thanks oBda. I'll take a look at the script and come back to you.
I came at it slightly differently, give this a try too and see if it works and makes sense.

@echo off
setlocal EnableDelayedExpansion

REM Set to Y to enable some display of progress, N to disable output
set DEBUG=Y

REM Define base key, and new keys to add
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
set AddList="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam","D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"

REM Load existing keys from registry into local variables
call :LoadExisting

REM Add each new key desired (if not already present)
for %%A in (%AddList%) do call :AddNew %%A

REM Exit main script
exit /b


REM Subroutine to gather exiting OPEN keys from registry
:LoadExisting
  for /f "tokens=1,2*" %%A in ('reg query "%Key%" /v "open*" ^| findstr /i /r /c:"open.*"') do (
    if /i "%%A" EQU "OPEN" (
      set Open[0]=%%C
    ) else (
      set Index=%%A
      set Open[!Index:OPEN=!]=%%C
      if "%DEBUG%" EQU "Y" echo Existing: [Open[!Index:OPEN=!]=%%C]
    )
  )
  exit /b

REM Subroutine to check if a key exists, and add it in not to the next available OPEN* value
:AddNew [key-to-add]
  set Found=N
  for /f "tokens=1* delims==" %%A in ('set Open[') do (
    if %1 EQU %%B (
      set Found=Y
      if "%DEBUG%" EQU "Y" echo Skipped: [%%B]
    )
  )
  if "!Found!" EQU "N" (
    set Added=N
    for /l %%I in (0, 1, 99) do (
      if "!Added!" EQU "N" (
        if not defined Open[%%I] (
          set Added=Y
          set Value=OPEN
          if %%I GTR 0 set Value=!Value!%%I
          set Data=%1
          set Data=!Data:"=\"!
          reg.exe add "%Key%" /v !Value! /t REG_SZ /d "!Data!" /f >NUL
          if "%DEBUG%" EQU "Y" echo Added: [!Value!=%1]
        )
      )
    )
  )
  exit /b

Open in new window

~bp
A few small fixes / enhancements to the above...

@echo off
setlocal EnableDelayedExpansion

REM Set to Y to enable some display of progress, N to disable output
set DEBUG=Y

REM Define base key, and new keys to add
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
set AddList="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam","D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"

REM Load existing keys from registry into local variables
call :LoadExisting

REM Add each new key desired (if not already present)
for %%A in (%AddList%) do call :AddNew %%A

REM Exit main script
exit /b


REM Subroutine to gather exiting OPEN keys from registry
:LoadExisting
  for /f "tokens=1,2*" %%A in ('reg query "%Key%" /v "open*" ^| findstr /i /r /c:"open.*"') do (
    if /i "%%A" EQU "OPEN" (
      set Open[0]=%%C
      if "%DEBUG%" EQU "Y" echo Existing: [Open[0]=%%C]
    ) else (
      set Index=%%A
      set Open[!Index:OPEN=!]=%%C
      if "%DEBUG%" EQU "Y" echo Existing: [Open[!Index:OPEN=!]=%%C]
    )
  )
  exit /b

REM Subroutine to check if a key exists, and add it in not to the next available OPEN* value
:AddNew [key-to-add]
  set Found=N
  for /f "tokens=1* delims==" %%A in ('set Open[ 2^>NUL') do (
    if %1 EQU %%B (
      set Found=Y
      if "%DEBUG%" EQU "Y" echo Skipped: [%%B]
    )
  )
  if "!Found!" EQU "N" (
    set Added=N
    for /l %%I in (0, 1, 99) do (
      if "!Added!" EQU "N" (
        if not defined Open[%%I] (
          set Added=Y
          set Value=OPEN
          if %%I GTR 0 set Value=!Value!%%I
          set Data=%1
          set Data=!Data:"=\"!
          reg.exe add "%Key%" /v !Value! /t REG_SZ /d "!Data!" /f >NUL
          set OPEN[%%I]=!Data!
          if "%DEBUG%" EQU "Y" echo Added: [!Value!=%1]
        )
      )
    )
  )
  exit /b

Open in new window

~bp
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, let me adjust for that...

~bp
Hiya oDba and Bill,

Thanks very much to you both for your assistance.

I've just tested oDBa's script at the moment and this seems to work great thanks very much.