0

I have a batch script that does a windows system analysis. But in order to collect browser info, I have to check if browsers are installed. The path of all browsers depends on system architecture (Program Files or Program Files (x86)). So I also check if system is 32bit or 64bit and store the path in a variable %program_files%. But although I have google chrome in my pc, the program prints an error: "Chrome doesn't seem to be installed". Below is the code described:

Setlocal EnabledDelayedExpansion
Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegQry% > %usbpath%CheckOS.txt
Find /i "x86" < %usbpath%CheckOS.txt >   %usbpath%StringCheck.txt

IF %ERRORLEVEL% == 0 (
     SET PROGRAM_FILES=%PROGRAMFILES%
) ELSE (
      SET "PROGRAM_FILES= %PROGRAMFILES(x86)%"
)

ECHO Check Google Chrome installation
IF NOT EXIST "!PROGRAM_FILES!\Google\Chrome\" GOTO SKIPG_CHROME

ECHO Running Google ChromeCacheView

:SKIPG_CHROME
ECHO Google Chrome doesn't seem to be installed. ChromeCacheView skipped.
marym92
  • 38
  • 6
  • Shouldn't the line `IF NOT EXIST "!PROGRAM_FILES!\...` actually be `%` instead of `!`? So `IF NOT EXIST "%PROGRAM_FILES%\...`. – Martin Aug 20 '15 at 14:57
  • I tried that but it does not work due to the parenthesis of path Program Files (x86) – marym92 Aug 20 '15 at 14:59
  • You're missing a "%", `SET "PROGRAM_FILES= %PROGRAMFILES(x86)%"` – Happy Face Aug 20 '15 at 15:00
  • My bad... Sorry! I forgot to write it down. I have it in my code. It's not that! – marym92 Aug 20 '15 at 15:05
  • Invalid `Setlocal` parameter, `Enabled` should be `Enable` – Happy Face Aug 20 '15 at 15:12
  • I fixed that but I still get the error! – marym92 Aug 20 '15 at 15:22
  • Missing a closing parenthesis for `if` stsatement – Happy Face Aug 20 '15 at 16:00
  • It's not that either! I already have seen that an fixed it! But the problem still continues! – marym92 Aug 20 '15 at 16:03
  • There is an evil space in `SET "PROGRAM_FILES= %PROGRAMFILES(x86)%"`. Remove it! – JosefZ Aug 20 '15 at 16:40
  • Take a look on [Silently Updating Firefox via Command Prompt (Windows)](http://stackoverflow.com/a/29809234/3074564) with a batch file to determine if Firefox is installed and in which directory and also of which version. I'm quite sure the same method can be used also for Chrome. It is always better to get directly from registry if an application is installed and where as searching around. See also [this answer](http://stackoverflow.com/a/31603179/3074564) why it is quite safe and often better to use `App Paths`. – Mofi Aug 20 '15 at 17:15

2 Answers2

0

%ERRORLEVEL% returns 0 if the statement is true (which is your OS is x86-based). So now I know why batch keep showing Google Chrome doesn't seem......

By going through your script, it seems like you're trying to detect operating system. The combination of systeminfo and find /i will do the trick easier.

Replace the whole

Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegQry% > %usbpath%CheckOS.txt
Find /i "x86" < %usbpath%CheckOS.txt >   %usbpath%StringCheck.txt

With

systeminfo | find /i "x64-based" > %usbpath%StringCheck.txt
Happy Face
  • 1,061
  • 7
  • 18
  • I think, using __systeminfo__ is not a good idea for this task. On the computer I'm currently writing this comment this command exits currently with `loading network card information ... error provider load failure` output as last message. Therefore it is not possible to determine at all if installed Windows is x86 or x64. – Mofi Aug 20 '15 at 17:52
0

Much better for your task to determine possible location of Chrome would be getting application path from App Paths registry key(s).

But here is a simple batch solution avoiding all the typing mistakes you made:

@echo off
goto GetProgFilesDir

rem If the environment variable "ProgramFiles(x86)" exists with a non
rem empty value, this batch file is running on Windows x64 with a
rem separate standard program files directory for x86 applications.
rem Otherwise the batch file is running on Windows x86 with just
rem one standard program files directory for all applications.

:GetProgFilesDir
if "%ProgramFiles(x86)%" == "" (
    set "PROGRAM_FILES=%ProgramFiles%"
) else (
    set "PROGRAM_FILES=%ProgramFiles(x86)%"
)

echo Check Google Chrome installation
if not exist "%PROGRAM_FILES%\Google\Chrome" goto SKIPG_CHROME

echo Google Chrome is installed.
goto :EOF

:SKIPG_CHROME
echo Google Chrome doesn't seem to be installed.

I don't recommend determining version of Windows by evaluating processor architecture. I have a machine with an x64 processor, but installed and running is Windows x86 and therefore there is no directory Program Files (x86) although processor is 64-bit.

Mofi
  • 46,139
  • 17
  • 80
  • 143