2011-02-04

Batch scripting: Reading paths from registry

Reading paths from registry can be extremely useful if you are making your own automated installation or something similar. Getting the path from registry removes the need for manual entry of the path.

As there exist a few differences when running commands from batch file and entering them directly into command window, I'll explain how to do it with a batch file.

Let's presume we have JDownloader installed on our computer.
Registry key "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" contains a string "Path" which has an installation path stored in it.

We can store its value in a temporary variable "installpath" using following command:

for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /v "Path"') do set installpath=%%b)

Our current command will only work on Windows with 32-bit architecture, because on Windows with 64-bit architecture that same registry key is stored in a different location. That location is: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader"

The reason for this is because 32-bit applications on 64-bit Windows put their registry keys under Wow6432Node branch. Using the fact that 64-bit Windows have SysWOW64 folder under Windows folder, we'll make our command architecture independent by modifying it like this:

if exist "%windir%\SysWOW64" (
    for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader" /v "Path"') do set installpath=%%b
) else (
    for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /v "Path"') do set installpath=%%b
)

If registry string which value you are getting has multiple words in its name, value of tokens should be increased by number of spaces inside the name. For example, if string is named "Install Path" you must change tokens to "tokens=3*".

If path you are reading from a registry key is stored in default string, there exists another problem. On Windows XP and earlier, "reg query" command returns the name of default string as "(NO NAME)", while on Windows Vista and later returns as "(Default)".

Because of this change, value of tokens is different as there is one additional space on Windows XP and earlier. If JDownloader had its path stored in default string, to make our command independent of Windows version, we would have to modify it like this:

for /f "tokens=2 delims=[]" %%x in ('ver') do set winver=%%x
set winver=%winver:~8,1%

if %winver% geq 6 (
    if exist "%windir%\SysWOW64" (
        for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader" /ve') do set installpath=%%b
     ) else (
        for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /ve') do set installpath=%%b
     )
) else (
    if exist "%windir%\SysWOW64" (
        for /f "tokens=3*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader" /ve') do set installpath=%%b
    ) else (
        for /f "tokens=3*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /ve') do set installpath=%%b
    )
)

There is one other thing I would like to mention, which doesn't effect our way of getting a path, but is good to be aware of when working with "reg query".

As there is a difference in a name of the default value, there also exists a difference in a way columns are being separated. While on Windows XP and earlier tabulator character has been used , on Windows Vista and later, it has been changed to four space characters.

That should be all. :)

2011-01-30

Telnet client in Windows Vista and 7

Recently I had a need for telnet client, and when tried to access the one that comes with Windows, I was surprised it was missing. At first I thought it was removed from Windows 7, but later found out it was only disabled since Vista came out.

To re-enable it go to Start > Control Panel > Programs and Features > Turn Windows features on or off. Tick Telnet Client and click on OK.

Opening command window in current Explorer window path

If you tend to use command window (cmd.exe) now and then, you may have come to situation when you wanted to open it in directory of the active Explorer window so you don't have to manually change directory.

That function is already integrated in Windows Explorer, all you have to do is hold Shift button while right clicking and you'll see 'Open command window here' which usually isn't present.

Note: This function is only present on Windows Vista and newer.

Multiple unknown devices in Device Manager with Hardware ID: sun_VBoxNetFltmp

After you have been using Oracle VM VirtualBox for a longer period, continuous updates and reinstallations may have left some unnecessary things in Device Manager.
You may have multiple instances of unknown devices in network section of Device Manager.

If you take a look at their properties, you will that everyone of them is VirtualBox Bridged Networking Driver Miniport. These are leftovers of previous versions of VirtualBox present because of the issues regarding permissions in registry.

I had an amazing number of 41 such unknown devices.

Because they refuse to uninstall the normal way, they have to be removed manually.
You will need to have administrator rights to take ownership and give yourself full permisions and auditing control for the following registry key in order to delete it, as this registry key is all that is left of them:
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\SUN_VBOXNETFLTMP

When you got rid of it, reinstall VirtualBox to make sure everything works fine.

Long time is being taken before internet connection is established

Connecting to internet whether using Ethernet cable or WiFi adapter should be almost instant in both cases. Sometimes, installation of a new software which modifies your internet connection in some way, may result in increase of that time as high as few minutes.

Software usually responsible for this is Virtual Machine or Antivirus software.
Uninstalling and then reinstalling your Ethernet and WiFi driver from Device Manager will most likely fix this issue.

Updating Toshiba Tecra BIOS: "The computer is not supported"

You may came to situation you want to update your BIOS for whatever reasons you find it necessary. Downloading the installation file from Toshiba's official site, and executing it may result in a message: "The computer is not supported."

This error occurs because Toshiba ACPI Common Modules aren't present on your system.
Although the installation guide they provide doesn't says anything about this, they are required prior to installation of BIOS update.

ACPI Common Modules came as a part of Value Added Package available for download on Toshiba's drivers section on their web site, presumably the same location you downloaded your BIOS update in the first place.

Installing Toshiba ACPI Common Modules and rebooting afterwards when asked, results in successful execution of BIOS update installation file.