Program Files Environment Variables giving different results in Windows - windows

When I run the command set programfiles in the command prompt, I get
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
However, the following code in python
import os
print os.getenv("programfiles")
or
msgbox %A_ProgramFiles% and %ProgramFiles%
in Autohotkey
or
$env:ProgramFiles
in PowerShell
all results in C:\Program Files
I cannot understand why I'm getting different results for the Program Files Environment Variables in Windows

What you see is the difference between a 32-bit and 64-bit application.
32-bit cmd.exe (%SystemRoot%\SysWOW64\cmd.exe)
C:\>set programfiles
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
64-bit cmd.exe (%SystemRoot%\System32\cmd.exe)
C:\>set programfiles
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
32-bit powershell.exe (%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe)
PS C:\> dir env:\programfiles*
Name Value
---- -----
ProgramFiles(x86) C:\Program Files (x86)
ProgramFiles C:\Program Files (x86)
64-bit powershell.exe (%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe)
PS C:\> dir env:\programfiles*
Name Value
---- -----
ProgramFiles(x86) C:\Program Files (x86)
ProgramFiles C:\Program Files

Related

How to fix this cmd scripting string parsing error

My system path is:
C:\Users\angus\algs4\java\bin;C:\binaryTools\SMLNJ\bin\;C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\ProgramData\Oracle\Java\javapath;C:\binaryTools;C:\Program Files (x86)\ActiveState Komodo IDE 8\;C:\PHP\;C:\Program Files (x86)\Haskell\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\lib\extralibs\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\bin;C:\MinGW\msys\1.0\bin;C:\MinGW\bin;C:\Program Files\Java\jdk1.8.0_66\bin;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Services\IPT\;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Program Files\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\mingw\bin;C:\Python33;C:\Python33\Scripts;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Python27;C:\Python27\Scripts;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Collaborator Client;C:\Program Files\doxygen\bin;C:\Program Files\TortoiseGit\bin;
And I am trying to update a registry key (the system path) using a script but am getting a wierd error:
>myscript.cmd
\Python36-32\Scripts\ was unexpected at this time.
How can I fix the script error?
Here is the script:
echo off
SET PROGPATH=C:\dell\mypath
for /f "tokens=2*" %%a in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "SysPath=%%~b"
echo system path is: %SysPath%
echo %SysPath%>tmppath.txt
echo %PATH% | findstr /i %PROGPATH%
if %ERRORLEVEL% EQU 1 (
echo %PROGPATH% path not found in system path
REM we now add to syspath and then write back to registry
set UPDATEDPATH=%PROGPATH%;%SysPath%
REM we now just write UPDATEDPATH back to registry
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v "Path" /t REG_SZ /d "%UPDATEDPATH%"
echo %PROGPATH% should now be added to the system path
) else (
echo %PROGPATH% already in path
)
The problem line is: echo %PATH% | grep %PROGPATH%
you get the error wherever there is a
C:\Program Files (x86)\Java\jre1.8.0_45\bin;
or something like that.
ie the (x86)\J or (x86)\P etc
So how to fix that?
Was asked for more details. So here.
Before running script, my env is like this:
keyname:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Path key has value:
C:\Users\angus\algs4\java\bin;C:\binaryTools\SMLNJ\bin\;C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\ProgramData\Oracle\Java\javapath;C:\binaryTools;C:\Program Files (x86)\ActiveState Komodo IDE 8\;C:\PHP\;C:\Program Files (x86)\Haskell\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\lib\extralibs\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\bin;C:\MinGW\msys\1.0\bin;C:\MinGW\bin;C:\Program Files\Java\jdk1.8.0_66\bin;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Services\IPT\;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Program Files\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\mingw\bin;C:\Python33;C:\Python33\Scripts;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Python27;C:\Python27\Scripts;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Collaborator Client;C:\Program Files\doxygen\bin;C:\Program Files\TortoiseGit\bin;
>echo %PATH%
C:\Users\angus\algs4\java\bin;C:\binaryTools\SMLNJ\bin\;C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\ProgramData\Oracle\Java\javapath;C:\binaryTools;C:\Program Files (x86)\ActiveState Komodo IDE 8\;C:\PHP\;C:\Program Files (x86)\Haskell\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\lib\extralibs\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\bin;C:\MinGW\msys\1.0\bin;C:\MinGW\bin;C:\Program Files\Java\jdk1.8.0_66\bin;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Services\IPT\;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Program Files\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\mingw\bin;C:\Python33;C:\Python33\Scripts;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Python27;C:\Python27\Scripts;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Collaborator Client;C:\Program Files\doxygen\bin;C:\Program Files\TortoiseGit\bin;C:\Users\angus\algs4\bin;C:\Users\angus\algs4\java\bin;C:\Users\angus\AppData\Roaming\cabal\bin;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\binaryTools\emacs-24.5-bin-i686-mingw32\bin;C:\windows;C:\windows\system32;C:\Users\angus\AppData\Roaming\Dashlane\4.7.1.28771\bin\Firefox_Extension\{442718d9-475e-452a-b3e1-fb1ee16b8e9f}\components;C:\Users\angus\AppData\Roaming\Dashlane\4.7.1.28771\ucrt
now if I run the batch file contents above which I named path3.cmd I get:
>path3.cmd
\Python36-32\Scripts\ was unexpected at this time.
>
Trying
echo %PATH% | findstr /i /c:"%PROGPATH%"
or
echo "%PATH%" | findstr /i /c:"%PROGPATH%"
makes no difference.
The bug seems to be in this line:
set UPDATEDPATH=%PROGPATH%;%SysPath%

Adding Native Tools Command Prompt on VS2015 RC

Since I cannot locate Native Tools CMD under the Tools menu, I try to manually add it in External Tools. Few questions:
Regardless of what I choose for Command (ARM, x86 or x64 etc.), Command is always C:\Windows\System32\cmd.exe. Why the different CMDs end up having the same path to the native System32's CMD?
Referring to this answer, I should insert /k "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" to the arguments - what is the /k and what is the bat for this argument? Why do I need to pass a path as an argument to the command prompt?
What is Initial Directory?
Why the different CMDs end up having the same path to the native System32's CMD?
The VS2015* CMDs are just cmd.exe with some environment variables already set up for you. so for example instead of typing "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" to run InstallUtil.exe you will just type InstallUtil and it will work. if you didn't set up the environment variables you will get an error message saying that 'installutil' is not recognized as an internal or external command, operable program or batch file.
what is the /k and what is the bat for this argument? Why do I need to pass a path as an argument to the command prompt?
/k is a parameter for cmd.exe and what it does is run the commands specified by the string that follows (in this case it will execute what's inside "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat" and will carry out).
What is Initial Directory?
Initial directory is used to specify the working directory that your cmd.exe instance will start in
So in the end you'll have something like this for Visual Studio 2015:
The "arguments" for VS2015 is :
/k "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
I took a look at my start menu and right clicked on Developer Command Prompt for VS2015. Copied target %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat""
MSBuild Command Prompt for VS2015
Copied target %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsMSBuildCmd.bat""
https://connect.microsoft.com/VisualStudio/feedback/details/747807/vs2012-x64-native-tools-command-prompt

I cannot use the msg command in cmd (or batch for that matter). How can I fix this?

While in cmd or making a batch file, I cannot use the command msg. When I try to use it, it returns the error msg is not recognized as an internal or external command, operable program or batch file." I'm pretty sure the error is that im missing a msg.exe in system32, so could someone show how to obtain that? I'm running windows 8.1.
msg.exe is not available on all Windows platforms in all environments.
There is just %SystemRoot%\System32\msg.exe (64-bit), but no %SystemRoot%\SysWOW64\msg.exe (32-bit) on Windows 7 x64 Enterprise whereby 64-bit msg.exe must be accessed from within a 32-bit command process by using %SystemRoot%\Sysnative\msg.exe.
For details about System32, SysWOW64 and Sysnative see the Microsoft documentation page File System Redirector.
What does this mean?
A batch file being executed on 32-bit Windows needs to run %SystemRoot%\System32\msg.exe.
A batch file being executed on 64-bit Windows by 64-bit cmd.exe needs to run %SystemRoot%\System32\msg.exe.
A batch file being executed on 64-bit Windows by 32-bit cmd.exe needs to run %SystemRoot%\Sysnative\msg.exe.
It depends on architecture of parent process starting cmd.exe or the batch file which implicitly results in starting cmd.exe for execution of the batch file if the batch file is executed in 32-bit or 64-bit environment on 64-bit Windows.
So a batch file is called with using explicitly %SystemRoot%\Sysnative\cmd.exe from a 32-bit application on Windows x64 or inside the batch file %SystemRoot%\Sysnative\msg.exe is used on a Windows x64 machine while on a Windows x86 machine %SystemRoot%\System32\cmd.exe respectively %SystemRoot%\System32\msg.exe must be used.
Demo example for first variant with using 64-bit command line interpreter:
Batch file with name MsgDemo.bat:
#echo off
%SystemRoot%\System32\msg.exe /?
pause
is called from 32-bit process running on Windows x64 with:
%SystemRoot%\Sysnative\cmd.exe /C MsgDemo.bat
Demo example for second variant with referencing msg.exe correct:
#echo off
set "AppMsg=%SystemRoot%\System32\msg.exe"
if not "%ProgramFiles(x86)%" == "" (
rem Explicitly reference 64-bit version on Windows x64 as there is
rem no 32-bit version. But use Sysnative redirector only if the batch
rem file was started with 32-bit cmd.exe as otherwise System32 contains
rem msg.exe if it is not missing at all like on Windows 7 Home Premium.
if exist %SystemRoot%\Sysnative\* set "AppMsg=%SystemRoot%\Sysnative\msg.exe"
)
%AppMsg% /?
set "AppMsg="
pause
The redirector %SystemRoot%\Sysnative doesn´t exist for 64-bit processes, just for 32-bit processes.
%SystemRoot%\Sysnative is not a directory. Therefore if exist %SystemRoot%\Sysnative does not work, just if exist %SystemRoot%\Sysnative\*.
Copy msg.exe from any windows 7 PC from system32 and past target PC system32 directory.
Work fine for me

How to distinguish between different processes using the same name?

So I am running several instances of my program and I have to be able to distinguish between the instances in a good way from powershell. Since every instance is installed in a separate folder I thought that i could use:
get-process -Name MyProgram* | Select-Object name, path
and get a list showing me the folder from which the process was started. Unfortunately this returns with the path empty.
So I tried a bunch of other properties that might be unique but all of them come back empty.
What am I doing wrong? Or can I do something else perhaps?
I am using windows 8 btw.
Could you use PID? Or does your program start child-processes. Path works for me, like:
Get-Process iexplore | Select-Object ID, Name, Path
Id Name Path
-- ---- ----
10792 iexplore C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
13928 iexplore C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
17144 iexplore C:\Program Files\Internet Explorer\iexplore.exe
17772 iexplore C:\Program Files\Internet Explorer\IEXPLORE.EXE
20896 iexplore C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
Does ExecutablePath or CommandLine return anything?
Get-WmiObject -Class Win32_Process -Filter "Name LIKE 'iexplore%'" | Select-Object ProcessID, Name, ExecutablePath, Commandline
ProcessID Name ExecutablePath Commandline
--------- ---- -------------- -----------
17144 iexplore.exe C:\Program Files\Internet Explorer\iexplore.exe "C:\Program Files\Internet Explorer\iexplore.exe" ...
32016 iexplore.exe C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE "C:\Program Files (x86)\Internet Explorer\IEXPLORE...
36744 iexplore.exe C:\Program Files\Internet Explorer\IEXPLORE.EXE "C:\Program Files\Internet Explorer\IEXPLORE.EXE" ...
If the process doesn't belong to you, then you need to run it as admin/elevated.
You have to be running with elevated permissions to see the path information of processes you're not the owner of.

WOW64 woes (.lnk shortcuts)

I'm using Windows 7 (x64) and Delphi 2010.
I'm writing a component that will emulate the start menu. However, I've run into the following problems:
If I attempt to open a shortcut (.lnk file) with ShellExecute, this will fail whenever %ProgramFiles% is part of the target path of the shortcut (it will then look at the C:\Program Files (x86) folder instead of C:\Program Files);
ShGetFileInfo fails to extract the correct index of the icon in the system image list if %ProgramFiles% is part of the path to the icon file (same problem as above).
Is there any workaround to the above issues or do I have to wait for native 64-bit Delphi to become available for this to work?
I think you should call Wow64DisableWow64FsRedirection before and Wow64RevertWow64FsRedirection aftyer.
Ok, a small example that demonstrates the problem.
I invoke ShellExecute with the following parameters:
ShellExecute(Handle, 'open', 'C:\Users\...\Internet Explorer (64-bit).lnk', nil, nil, SW_SHOWNORMAL);
The target of Internet Explorer (64-bit).lnk is:
C:\Program Files\Internet Explorer\iexplore.exe
However, the 32-bit version of iexplore.exe is opened nonetheless. In this case the path doesn't even use %ProgramFiles%, so somehow ShellExecute will translate C:\Program Files to C:\Program Files (x86) internally. I have no idea how to make it open the 64-bit version of iexplore.exe instead.
Another problem, after calling Wow64DisableWow64FsRedirection, ShellExecute will no longer open folders.
The following environment variables will always point to the right direction on a 64-bit machine, and will be undefined on a 32-bit machine:
from a 32-bit shell on a 32-bit architecture:
C:\>echo %processor_architecture%
x86
C:\>echo %programfiles(x86)%
%programfiles(x86)%
C:\>echo %programw6432%
%programw6432%
C:\>echo %programfiles%
C:\Program Files
from a 32-bit shell on a 64-bit architecture:
C:\>echo %processor_architecture%
x86
C:\>echo %programfiles(x86)%
C:\Program Files (x86)
C:\>echo %programw6432%
C:\Program Files
C:\>echo %programfiles%
C:\Program Files (x86)
from a 64-bit shell:
C:\>echo %processor_architecture%
AMD64
C:\>echo %programfiles(x86)%
C:\Program Files (x86)
C:\>echo %programw6432%
C:\Program Files
C:\>echo %programfiles%
C:\Program Files
Try substituting them before running the application.

Resources