?
Solved

Registry script to check registry entries and add an entry in

Posted on 2014-04-08
9
Medium Priority
?
708 Views
Last Modified: 2014-04-17
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

0
Comment
Question by:rhiancohen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39985691
Can you ellaborate on what would be the criteria to me "any values in the keys below which relate to Excel addins" ?

~bp
0
 
LVL 85

Expert Comment

by:oBdA
ID: 39985711
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
 
LVL 1

Author Comment

by:rhiancohen
ID: 39985864
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rhiancohen
ID: 39986228
Thanks oBda. I'll take a look at the script and come back to you.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39986350
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
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39986436
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
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 39987015
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
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39987152
Okay, let me adjust for that...

~bp
0
 
LVL 1

Author Comment

by:rhiancohen
ID: 39988330
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question