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

LVL 2
rhiancohenVMware Senior ConsultantAsked:
Who is Participating?
 
oBdACommented:
Updated version, now ignores if the Addin already exists:
@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 Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
set Quiet=0
set LastIndex=10
for /f "tokens=1* delims==" %%a in ('set AddinList[') do (
	if %Quiet%==0 echo Processing '%%b'
	set Value=
	set FoundAt=
	set Data=%%b
	set CompareData=!Data:"=!
	for /l %%i in (0, 1, %LastIndex%) do (
		if %%i==0 (set TestValue=OPEN) else (set TestValue=OPEN%%i)
		reg.exe query "%Key%" /v "!TestValue!" >NUL 2>&1
		if errorlevel 1 (
			if "!Value!"=="" (
				if %Quiet%==0 echo !TestValue! ... free.
				set Value=!TestValue!
			)
		) else (
			if %Quiet%==0 echo !TestValue! ... already used.
			for /f "tokens=2*" %%o in ('reg.exe query "%Key%" /v "!TestValue!"') do set CompareDataExisting=%%p
			set CompareDataExisting=!CompareDataExisting:"=!
			if /i "!CompareData!"=="!CompareDataExisting!" set FoundAt=!TestValue!
		)
	)
	if "!Value!"=="" (
		echo ERROR: Unable to find an empty index in the range of 0..%LastIndex%
		goto :eof
	)
	if "!FoundAt!"=="" (
		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.
		)
	) else (
		if %Quiet%==0 echo Found data in '!FoundAt!', nothing added.
	)
)

Open in new window


BillPrew,
using a list format like AddList won't work for every case. The data can contain additional arguments like /R "C:\Program Files\Microsoft Office\Office14\Library\Analysis\ANALYS32.XLL" which can't be correctly parsed in a list.
In a case like this, your AddNew function will fail, too.
0
 
Bill PrewCommented:
Can you ellaborate on what would be the criteria to me "any values in the keys below which relate to Excel addins" ?

~bp
0
 
oBdACommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
rhiancohenVMware Senior ConsultantAuthor Commented:
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
0
 
rhiancohenVMware Senior ConsultantAuthor Commented:
Thanks oBda. I'll take a look at the script and come back to you.
0
 
Bill PrewCommented:
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
0
 
Bill PrewCommented:
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
0
 
Bill PrewCommented:
Okay, let me adjust for that...

~bp
0
 
rhiancohenVMware Senior ConsultantAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.