Friday, May 22, 2009

Microsoft Internet Explorer Search Providers Import and Export

Microsoft Internet Explorer 7 and 8 and also Mozilla Fire Fox come with a feature called "Custom Search Providers" or Search Scopes. It allows you to add web sites and search engines to your search box to search them directly, if you need to. For example could you add Amazon.com search to your search providers and search the Amazon.com site directly from your search box, if you are looking for a book, CD or DVD and use Amazon.com as primary online shop for this kind of purchases.

ie-custsearchproviders1

You can get a selection of custom search providers at this Microsoft web site. There you can find a number of options for popular web sites and other search engines. Also available there is the option to create your own custom search provider for your favorite web site. This is possible, as long as the site provides a site search option and passes the search parameters, such as the search term in the URL.

If you have a web site yourself and want to offer your visitors the option to add your site search to their web browser, then you can do that as well. I did this for my small project SQLHunt.com, a MS SQL Server resources meta-search engine based on Google Custom Search Engine. See the link at the bottom of the homepage that reads "Widgets and Gadgets". One of the options there is to add the SQLHunt.com search to your web browser.

The search providers page for Internet Explorer on the Microsoft web site.

ie-custsearchproviders2

All nice and good and I made myself good use of this feature. I have by now probably 20+ custom search providers added to my search box. Some via the Microsoft web site, others custom or via the "add" option at a web site directly. There is unfortunately no easy way to copy your search providers to another computer or to back it up and re-install, if you do a re-install of your current system.

The search providers (or search scopes) are stored by Microsoft in the Windows System Registry. You can find your current search scopes and export them via the system tool RegEdit.exe to a file at

HKEY_Current_User\Software\Microsoft\Internet Explorer\SearchScopes

For each search provider exists a sub-key, in case that you only want to export specific search providers and not all of them. You can then take the exported file (with the extension .REG), copy it to the other machine and double click on it. You should be prompted to acknowledge that you want to import the file into the system registry. Do that and your search provider settings will be imported.

Here is a simple BATCH script to export and import your search provider settings.

SearchScope.bat Code

Download the source code for SearchScope.bat here (you need to remove the additional .txt extension of the file when you save the file to your local hard disk).

   1:  @echo off
   2:  CLS
   3:  if "%1"=="" (CALL :SELOPT) ELSE SET OPT=%1 
   4:  if "%2"=="" Call :GETFP   
   5:   
   6:  IF %OPT%==IMPORT Call :DoImport
   7:  IF %OPT%==EXPORT Call :DoExport
   8:  Echo Finished!
   9:  Echo.
  10:  pause
  11:  GOTO :EOF
  12:   
  13:  :SELOPT
  14:  echo Microsoft Custom Search Provider Import/Export Tool
  15:  echo by Carsten Cumbrowski aka Roy/SaC at http://www.roysac.com/blog
  16:  echo.
  17:  SET /p OTmp=Enter (I) to Import, (E) to Export or (A) to Abort: 
  18:  IF "%OTmp%"=="I" SET OPT=IMPORT&SET OK=1
  19:  IF "%OTmp%"=="i" SET OPT=IMPORT&SET OK=1
  20:  IF "%OTmp%"=="E" SET OPT=EXPORT&SET OK=1
  21:  IF "%OTmp%"=="e" SET OPT=EXPORT&SET OK=1
  22:  If NOT "%OK%"=="1" EXIT
  23:  GOTO :EOF
  24:   
  25:  :GETFP
  26:  echo.
  27:  echo Please Specify the name and path for the file to %OPT% 
  28:  Echo Enter "a' to abort.
  29:  echo. 
  30:  SET /P FNAME=File Name: 
  31:  if %OPT%==IMPORT (
  32:    IF "%FNAME%"=="" EXIT
  33:    IF NOT EXIST "%FNAME%" Call :GETFP
  34:  )
  35:  GOTO :EOF
  36:   
  37:  :DoImport
  38:  REG Import %FNAME%>NUL 2>&1
  39:  Echo Search Scopes Imported
  40:  Goto :EOF
  41:   
  42:  :DoExport
  43:  REG Export "HKCU\Software\Microsoft\Internet Explorer\SearchScopes" %FNAME%>Nul 2>&1
  44:  Echo Search Scopes Exported to %FNAME%
  45:  GOTO :EOF

Here is a more complex example script. It reads the settings from the HKEY_USER branch rather than the HKEY_CURRENT_USER one. The script looks up the SID for the current user and exports the registry settings, replacing the SID with a place holder to be substituted when imported again elsewhere.

This was more of a "proof of concept", since you do not have to go through all this length as the script above illustrates. I got the "inspiration" from the blog post "Working with Registry Keys in a Batch File" by "rhyous" at the LandDesk User Community.

However, this batch could be modified to export the settings from other users on the same machine. It was not easy to accomplish and very tricky to do. Just for a test, look at the code and tell honestly, if you understand every statement used there. :)

The only thing you would have to do, is replacing %%USERPROFILE%% with the path to the users home directory (e.G. "C:\Documents and Settings\UserName"

SearchScopesImportExport.bat Code

Download the source code for SearchScopesImportExport.bat here (you need to remove the additional .txt extension of the file when you save the file to your local hard disk).


   1:  @Echo off
   2:  cls
   3:  SETLOCAL ENABLEEXTENSIONS
   4:  SETLOCAL ENABLEDELAYEDEXPANSION
   5:  if "%*"=="" Goto :ShowUsage
   6:  if "%~2"=="" Goto :ShowUsage
   7:   
   8:  SET RootKey="HKLM\software\Microsoft\Windows NT\CurrentVersion\ProfileList"
   9:  Set Val=ProfileImagePath
  10:   
  11:  Call SET CurProf=%%USERPROFILE%%
  12:  SET CurSID=""
  13:   
  14:  Echo Determining Current User's SID ...
  15:  Call :GETSID %RootKey%
  16:  Echo The current users SID is %CurSID%
  17:  Call Set RegTmp=%Temp%\%CurSID%_SearchScopes.reg
  18:   
  19:  IF "%1"=="export" Call :ExportScopes "%~2"
  20:  IF "%1"=="import" Call :ImportScopes "%~2"
  21:   
  22:  Echo.
  23:  echo Done Processing
  24:  echo.
  25:  pause
  26:  echo.
  27:  Goto :EOF
  28:   
  29:   
  30:  :GETSID
  31:  FOR /F "tokens=7* delims=\" %%i IN ('REG QUERY "%~1"') DO (
  32:    FOR /F "tokens=2*" %%a in ('REG QUERY "%~1\%%~i" /v %Val% ^|FINDSTR %Val%') DO (
  33:       CALL SET ldclientdir=%%b
  34:       if "%CurProf%"=="!ldclientdir!" (
  35:         Set CurSID=%%~i
  36:       )
  37:    )
  38:  )
  39:  GOTO :EOF
  40:   
  41:  :ExportScopes
  42:  echo Exporting Search Scope Settings ...
  43:  REG Export "HKU\%CurSID%\Software\Microsoft\Internet Explorer\SearchScopes" %RegTmp%>Nul 2>&1
  44:  Echo Replacing SID with place-holder CURRENTUSERSID ...
  45:  Call :SandR %RegTmp% "%~1" %%CurSID%% CURRENTUSERSID
  46:  goto :eof
  47:   
  48:  :SandR
  49:  ::1 = input file
  50:  ::2 = output file
  51:  ::3 = search string
  52:  ::4 = replacement value
  53:  if EXIST "%~2" DEL /Q "%~2"
  54:  for /f "tokens=1,* delims=]" %%a in ('"type "%~1"|find /n /v """') do (
  55:     CALL SET _result=%%b
  56:     if NOT "%%b"=="" (
  57:       CALL SET _fchar=%%_result:~0,1%%
  58:       if "!_fchar!"=="[" (
  59:         set "line=%%b"
  60:         if defined line (
  61:            call set "line=echo.%%line:%3=%4%%"
  62:            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X>>"%~2"
  63:         )   ELSE echo.>>"%~2"
  64:       ) else (
  65:           echo %%b>>"%~2"
  66:       )
  67:     ) else (
  68:         echo.>>"%~2"
  69:     )
  70:  )
  71:  goto :eof
  72:   
  73:  :ImportScopes
  74:  if NOT EXIST "%~1" GOTO :ShowUsage
  75:  echo Preparing Registry Data for Import ...
  76:  echo Replacing place-holder CURRENTUSERSID with SID ...
  77:  if EXIST %RegTmp% Del %RegTmp%
  78:  Call :SandR "%~1" %RegTmp% CURRENTUSERSID %%CurSID%%
  79:  Echo Importing Search Scopes ...
  80:  if EXIST %RegTmp% REG Import %RegTmp%>NUL 2>&1
  81:  goto :eof
  82:   
  83:   
  84:  :ShowUsage
  85:  cls
  86:  echo.
  87:  echo USAGE
  88:  echo   Script.bat import^|export IMPORTFILE.REG^|OUTPUTFILE.REG
  89:  echo.
  90:  echo Example Export of current Search Scopes to a file 
  91:  echo with the name MySearchScopes to the local directory C:\Data
  92:  echo.
  93:  echo   Script.bat export c:\Data\MySearchScopes.reg
  94:  echo.
  95:  Echo Example Import of Search Scopes from file generated via this script
  96:  Echo.
  97:  Echo  script.bat import c:\import\MySearchScopes.reg
  98:  echo.
  99:  pause
 100:  echo.

That's it. I hope that you will find this little tool and information useful.

Cheers!

Carsten aka Roy/SAC

No comments:

Post a Comment

Hi, thanks for taking the time to comment at my blog.

Due to spam issues comments are not immediately posted on the site and require my manual approval first, before they become visible.

I try to approve comments as quickly as possible and usually within 24 hours.

To be notified about follow up comments that are made after yours, use the subscribe option with your email address and you will receive an email alert, if somebody else comments at this post in the future.

Also check out the rest of the website beyond this blog, visit RoySAC.com. Also see my YouTube channels, SACReleases for intros and demos.

Cheers!
Carsten aka Roy/SAC

Note: Only a member of this blog may post a comment.