Showing posts with label Batch Script. Show all posts
Showing posts with label Batch Script. Show all posts

Friday 26 September 2014

Read Registry Value Using Java - Get Oracle Home From Regedit In Java

import java.io.*;

public class RegQuery {


    public static String executeRegistryQuery(String query, String regToke) {
        try {
            Process process = Runtime.getRuntime().exec(query);
            StreamReader reader = new StreamReader(process.getInputStream());

            reader.start();
            process.waitFor();
            reader.join();

            String result = reader.getResult();
            int p = result.indexOf(regToke);

            if (p == -1)
                return null;

            return result.substring(p + regToke.length()).trim();
        }
        catch (Exception e) {
            return null;
        }
    }


    static class StreamReader extends Thread {
        private InputStream is;
        private StringWriter sw;

        StreamReader(InputStream is) {
            this.is = is;
            sw = new StringWriter();
        }

        public void run() {
            try {
                int c;
                while ((c = is.read()) != -1)
                    sw.write(c);
            }
            catch (IOException e) { ; }
        }

        String getResult() {
            return sw.toString();
        }
    }

    public static void main(String s[]) {

        String REGQUERY_UTIL = "reg query ";
        String REGDWORD_TOKEN = "REG_DWORD";

        String PERSONAL_FOLDER_CMD = REGQUERY_UTIL +
                "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
                + "Explorer\\Shell Folders\" /v Personal";
        String CPU_SPEED_CMD = REGQUERY_UTIL +
                "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\""
                + " /v ~MHz";
        String CPU_NAME_CMD = REGQUERY_UTIL +
                "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\""
                + " /v ProcessorNameString";

        String REGSTR_TOKEN = "REG_SZ";
        String key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\ORACLE\\KEY_OraDb11g_home1";
        String value = "ORACLE_HOME";
        String executeString =    REGQUERY_UTIL + "\"" + key + "\"" + " /v " + value;


        System.out.println("Query Result : "
                + executeRegistryQuery(executeString, REGSTR_TOKEN));
        System.out.println("Query Result : "
                + executeRegistryQuery(PERSONAL_FOLDER_CMD, REGSTR_TOKEN));
        System.out.println("Query Result : "
                + executeRegistryQuery(CPU_SPEED_CMD, REGDWORD_TOKEN));
        System.out.println("Query Result : "
                + executeRegistryQuery(CPU_NAME_CMD, REGSTR_TOKEN));
    }
}

Find value of registry key in a bat file

@echo off

set THEME_REGKEY=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover
set THEME_REGVAL=ameritech.net

REM Check for presence of key first.
reg query %THEME_REGKEY% /v %THEME_REGVAL% 2>nul || (echo No theme name present! & exit /b 1)

REM query the value. pipe it through findstr in order to find the matching line that has the value. only grab token 3 and the remainder of the line. %%b is what we are interested in here.
set REG_NAME=
for /f "tokens=2,*" %%a in ('reg query %THEME_REGKEY% /v %THEME_REGVAL% ^| findstr %THEME_REGVAL%') do (
    set REG_NAME=%%b
)

REM Possibly no value set
if not defined REG_NAME (echo No theme name present! & exit /b 1)

REM replace any spaces with +
set REG_NAME=%REG_NAME: =+%

REM open up the default browser, searching google for the theme name
echo %REG_NAME%