How to set environment vars permanently with a shell script in Windows - windows

I have a .bat I use to set some environment vars before running a few programs.
I'd like to permanently set these environment vars, but I don't want to do it manually if possible. Is there a shortcut here? Is there any flag I can set to permanently add to PATH ?
The code is from setupvars.bat made available with OpenVino :
set ROOT=%~dp0
call :GetFullPath "%ROOT%\.." ROOT
set SCRIPT_NAME=%~nx0
set "INTEL_OPENVINO_DIR=%ROOT%"
set "INTEL_CVSDK_DIR=%INTEL_OPENVINO_DIR%"
where /q libmmd.dll || echo Warning: libmmd.dll couldn't be found in %%PATH%%. Please check if the redistributable package for Intel(R) C++ Compiler is installed and the library path is added to the PATH environment variable. System reboot can be required to update the system environment.
:: OpenCV
if exist "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat" (
call "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat"
) else (
set "OpenCV_DIR=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\lib"
set "PATH=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\bin;%PATH%"
)
:: OpenVX
set "OPENVX_FOLDER=%INTEL_OPENVINO_DIR%\openvx"
set "PATH=%INTEL_OPENVINO_DIR%\openvx\bin;%PATH%"
:: Inference Engine
set "InferenceEngine_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\share"
set "HDDL_INSTALL_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\hddl"
set "PATH=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Release;%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Debug;%HDDL_INSTALL_DIR%\bin;%PATH%"
if exist "%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions" (
set "ARCH_ROOT_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions"
)
:: Check if Python is installed
python --version 2>NUL
if errorlevel 1 (
echo Error^: Python is not installed. Please install Python 3.5. or 3.6 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
:: Check Python version
for /F "tokens=* USEBACKQ" %%F IN (`python --version 2^>^&1`) DO (
set version=%%F
)
echo %var%
for /F "tokens=1,2,3 delims=. " %%a in ("%version%") do (
set Major=%%b
set Minor=%%c
)
if "%Major%" geq "3" (
if "%Minor%" geq "5" (
set python_ver=okay
)
if "%Minor%" geq "6" (
set python_ver=okay
)
)
if not "%python_ver%"=="okay" (
echo Unsupported Python version. Please install Python 3.5 or 3.6 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
:: Check Python bitness
python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2 > NUL
if errorlevel 1 (
echo Error^: Error during installed Python bitness detection
exit /B 1
)
for /F "tokens=* USEBACKQ" %%F IN (`python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2^>^&1`) DO (
set bitness=%%F
)
if not "%bitness%"=="64" (
echo Unsupported Python bitness. Please install Python 3.5 or 3.6 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
set PYTHONPATH=%INTEL_OPENVINO_DIR%\python\python%Major%.%Minor%;%PYTHONPATH%
echo PYTHONPATH=%PYTHONPATH%
echo [setupvars.bat] OpenVINO environment initialized
exit /B 0
:GetFullPath
SET %2=%~f1
GOTO :EOF

For OpenVINO, you need to run the setupvars.sh script every time you open a command line. However, you can also add all the variables needed to your systems environment variables.
On your Windows® 10 system, go to Control Panel > System and Security > System > Advanced System Settings > Environment Variables.
See this document for a list of required variables and their values.

Related

Batch File: Activate conda environment then run a command and leave window open [duplicate]

Recently installed Anaconda (1.9) for my python project on win7
After installation, I built a python 3 support environment with instruction in this page. My next task is to activate my python environment automatically with built-in batch file.
I used the command in [Anaconda Command Prompt] shortcut I found in my start menu. It runs a batch-file called [anaconda.bat]
After observing the batch file I realized it seems to be capable of taking an input argument that is supposed to be the environment I would like to activate. So I copied the shortcut and modified it as
C:\Windows\System32\cmd.exe /k "C:\Anaconda\Scripts\anaconda.bat py3k"
Then I double clicked on the new shortcut, it opened a new command window but...the designated environment did not activate!
#echo off
rem +===========================================================================
rem | Initialisation
rem +===========================================================================
verify bogus-argument 2>nul
setlocal enableextensions enabledelayedexpansion
if ERRORLEVEL 1 (
echo error: unable to enable command extensions
goto :eof
)
for %%i in ("%~dp0..\envs") do (
set ANACONDA_ENVS=%%~fi
)
if not "%1" == "" (
if not exist "%ANACONDA_ENVS%\%1\python.exe" (
echo No environment named "%1" exists in %ANACONDA_ENVS%
goto :eof
)
set ANACONDA_ENV_NAME=%1
set ANACONDA=%ANACONDA_ENVS%\%1
title Anaconda (%ANACONDA_ENV_NAME%^)
) else (
set ANACONDA_ENV_NAME=
for %%i in ("%~dp0..") do (
set ANACONDA=%%~fi
)
title Anaconda
)
set ANACONDA_SCRIPTS=%ANACONDA%\Scripts
for %%i in ("python.exe") do (
for %%j in ("%ANACONDA%\python.exe") do (
if not "%%~f$PATH:i" == "%%~f$PATH:j" (
set ANACONDA_OLD_PATH="%PATH%"
set PATH=%ANACONDA%;%ANACONDA_SCRIPTS%;%PATH%;
echo Added %ANACONDA% and %ANACONDA_SCRIPTS% to PATH.
)
)
)
if not "%ANACONDA_ENV_NAME%" == "" (
echo Activating environment %ANACONDA_ENV_NAME%...
set PROMPT=[%ANACONDA_ENV_NAME%] $P$G
)
I have very little experience with bat language but I guess there may be something to do with this line
setlocal enableextensions enabledelayedexpansion
I tried to remove that line but kept trapped in the ERRORLEVEL 1 expression with message.
error: unable to enable command extensions
Can anyone suggest what I should do to make this bat-file work properly?
I don't think you need a batch file. Assuming that Anaconda and CMD are on your path (which they should be), you can try this as an alternative (it is what I do):
cmd "/c activate py3k && ipython --pylab"

Batch File Help: determining version to decide action

As our office upgrades to Window 7, I have been tasked to update the loginscript to work with Windows 7. The creators of said script are long gone, and I am not a batch file expert.
What I am trying to do is determine the OS. As I do some network administration duties, I need to be able to log on to a server without running the login script whereas I will need to the login script to run if I log into a Windows XP or Windows 7 computer.
I found I couldn't use the VER command as Windows 7 and Windows Server 2008 return the exact same results.
This is what I have:
if exist %loginscriptdir%\sysinfo.txt goto setver
if not exist %loginscriptdir%\sysinfo.txt wmic os get name /value > %loginscriptdir%\sysinfo.txt
type %loginscriptdir%\sysinfo.txt > %loginscriptdir%\sysinfo1.txt
:setver
set WinVer=Unknown
set errorlevel=0
If %WinVer% == "Unknown" (
findstr /c:"Windows XP Professional" %loginscriptdir%\sysinfo1.txt
if %errorlevel%==1 set WinVer=XP
) else (
findstr /c:"Windows 7 Enterprise" %loginscriptdir%\sysinfo1.txt
if %errorlevel%==1 set WinVer=Win7
)
set result=false
if %WinVer% == "XP" set result=true
if %WinVer% == "Win7" set result=true
if "%result%" == "false" (
goto skipicon1
Throughout the script, I wrote in breaks to find the values. Example:
REM -----
ECHO "%WinVer%"
ECHO "%result%"
ECHO "%errorlevel%"
ECHO Press any key to continue 4.
pause>null
REM -----
The fourth break comes at the end of the script I pasted above. These are the results:
"Unknown"
"false"
"0"
Press any key to continue 4.
Here you go. This is the best way I've found to get the OS accurately from win2kpro-winserver2k10. It also tells if it's 32/64 bit and what sp is installed but you don't have to. Just check %cap% in this example.
#echo off
setlocal
call :GetOS cap bit sp
echo %cap%%bit% (%sp%)
exit /b
:GetOS caption servicepack
setlocal
set arc=%PROCESSOR_ARCHITECTURE%
set key="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
for /f "tokens=3*" %%a in (
'reg query %key%^|findstr /i ProductName') do set cap=%%a %%b
for /f "tokens=3*" %%a in (
'reg query %key%^|findstr /i CSDVersion') do set sp=%%a %%b
endlocal & set %1=%cap% & set %2=%arc% & set %3=%sp%
exit /b
The issue with the code is the expansion of the %errorlevel% value. Since it is contained within a scope of parentheses, the value will not be updated till after the scope ends. Meaning that %errorlevel% will always equal its value when the scope began. To fix this you would have to use delayed expansion. setlocal enabledelayedexpansion and !errorlevel!. Here is a StackOverflow post about delayed expansion: Enable and Disable Delayed Expansion, what does it do?
You may just want to use the version detection method shown at ss64.com http://ss64.com/nt/ver.html
Here is the example from ss64.com but simplified:
#echo off
setlocal
:: Get windows Version
for /f "tokens=4,5,6 delims=[.] " %%A in ('ver') do (
set "Major=%%~A"
set "Minor=%%~B"
set "Build=%%~C"
)
if "%Major%.%Minor%"=="5.1" goto WinXP
if "%Major%.%Minor%"=="6.1" goto Win7
echo Unsupported Version Detected "%Major%.%Minor%"
goto End
:WinXP
echo Windows XP Detected
goto End
:Win7
echo Windows 7 or Server 2008 Detected
goto End
:End
endlocal
exit /b 0

Activate python 3 environemnt in Anaconda with batch-file

Recently installed Anaconda (1.9) for my python project on win7
After installation, I built a python 3 support environment with instruction in this page. My next task is to activate my python environment automatically with built-in batch file.
I used the command in [Anaconda Command Prompt] shortcut I found in my start menu. It runs a batch-file called [anaconda.bat]
After observing the batch file I realized it seems to be capable of taking an input argument that is supposed to be the environment I would like to activate. So I copied the shortcut and modified it as
C:\Windows\System32\cmd.exe /k "C:\Anaconda\Scripts\anaconda.bat py3k"
Then I double clicked on the new shortcut, it opened a new command window but...the designated environment did not activate!
#echo off
rem +===========================================================================
rem | Initialisation
rem +===========================================================================
verify bogus-argument 2>nul
setlocal enableextensions enabledelayedexpansion
if ERRORLEVEL 1 (
echo error: unable to enable command extensions
goto :eof
)
for %%i in ("%~dp0..\envs") do (
set ANACONDA_ENVS=%%~fi
)
if not "%1" == "" (
if not exist "%ANACONDA_ENVS%\%1\python.exe" (
echo No environment named "%1" exists in %ANACONDA_ENVS%
goto :eof
)
set ANACONDA_ENV_NAME=%1
set ANACONDA=%ANACONDA_ENVS%\%1
title Anaconda (%ANACONDA_ENV_NAME%^)
) else (
set ANACONDA_ENV_NAME=
for %%i in ("%~dp0..") do (
set ANACONDA=%%~fi
)
title Anaconda
)
set ANACONDA_SCRIPTS=%ANACONDA%\Scripts
for %%i in ("python.exe") do (
for %%j in ("%ANACONDA%\python.exe") do (
if not "%%~f$PATH:i" == "%%~f$PATH:j" (
set ANACONDA_OLD_PATH="%PATH%"
set PATH=%ANACONDA%;%ANACONDA_SCRIPTS%;%PATH%;
echo Added %ANACONDA% and %ANACONDA_SCRIPTS% to PATH.
)
)
)
if not "%ANACONDA_ENV_NAME%" == "" (
echo Activating environment %ANACONDA_ENV_NAME%...
set PROMPT=[%ANACONDA_ENV_NAME%] $P$G
)
I have very little experience with bat language but I guess there may be something to do with this line
setlocal enableextensions enabledelayedexpansion
I tried to remove that line but kept trapped in the ERRORLEVEL 1 expression with message.
error: unable to enable command extensions
Can anyone suggest what I should do to make this bat-file work properly?
I don't think you need a batch file. Assuming that Anaconda and CMD are on your path (which they should be), you can try this as an alternative (it is what I do):
cmd "/c activate py3k && ipython --pylab"

identify if java cannot be executed in windows batch script

I wrote the below batch script which asks for a JAVA_HOME path if its not present in environment, and then it verifies the java version. but before identifying java version it should also check whether java is present in the path (can be executed) or not. Please help me figure out if java -version can be executed or not and display proper message
#echo off
setlocal
set VERSION5="1.5"
IF "%JAVA_HOME%" == "" (
echo Enter path to JAVA_HOME:
set /p JAVA_HOME=
goto check_java_version
) ELSE (
echo Using %JAVA_HOME%
goto check_java_version
)
:check_java_version
for /f "tokens=3" %%g in ('%JAVA_HOME%\jre\bin\java -version 2^>^&1 ^| findstr /i "version"') do (
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
set JAVAVER=%JAVAVER:java version =%
for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
set VER=%%w
)
if not "%VER%" GEQ "5" goto wrong_version
set JAVA_BIN=%JAVA_HOME%\jre\bin
goto correct_java_version
:correct_java_version
REM echo JAVA Version is ok.
set JAVA_LIB=%cd%/lib
%JAVA_BIN%/java -cp %JAVA_LIB%/csm-runtime-1.0.jar;%JAVA_LIB%/groovy-all-1.8.1.jar;%JAVA_LIB%/commons-beanutils-1.8.3.jar;%JAVA_LIB%/csm-dbutil-1.0.jar;%JAVA_LIB%/commons-exec-1.1.jar;%JAVA_LIB%/log4j-1.2.8.jar;%JAVA_LIB%/commons-cli-1.2.jar -Dlog4j.configuration=com/ABC/csm/log4j.xml -Dendorsed_plugins_dir=./plugins com.ABC.csm.CSMMain %*
goto end_java_version
:wrong_version
echo Current JDK Version %VER%
echo Expected JDK %VERSION5% or greater. Please fix your SSATOP and try again.
goto end_java_version
:no_java
echo No JDK found in %JAVA_HOME%.
goto wrong_version
:end_java_version
endlocal
1 of the examples of invalid condition would be, instead of providing JAVA_HOME i.e., e:\csm\java I gave it as e:\csm\java\jre\bin which in this case should display proper error message that please provide a JAVA_HOME path
To check the existence of a program in the PATH, windows batch provides the ~%PATH: option of the SET command. See HELP CALL or HELP FOR.
Use this piece of code as a starting point.
:ProgInPath
set PROG=%~$PATH:1
goto :eof
and use it like this
call :ProgInPath java.exe
IF "%PROG%" == "" (
echo Java.exe not found
) else (
echo. %PROG%
)
in this example, if java.exe is in the PATH, it echoes its complete filespec.
I used the below snippet and it solved my problem
:check_java_existence
IF EXIST %JAVA_HOME%\jre\bin\java.exe (
echo java exists
) ELSE (
echo java does not exists
)

batch file to check if Python is installed

I've written a batch script that checks if Python is installed, if it's not installed - it initiates the Python installer contained in the same folder as itself.
I'm using the following code:
reg query "hkcu\software\Python 2.6"
if ERRORLEVEL 1 GOTO NOPYTHON
:NOPYTHON
ActivePython-2.6.4.8-win32-x86.msi
reg query "hklm\SOFTWARE\ActiveState\ActivePerl\" 1>>Output_%date%_%time%.log 2>&1
if ERRORLEVEL 1 GOTO NOPERL
reg query "hklm\SOFTWARE\Gtk+"
if ERRORLEVEL 1 GOTO NOPYGTK
:NOPERL
ActivePerl-5.10.1.1006-MSWin32-x86-291086.msi 1>>Output_%date%_%time%.log 2>&1
:NOPYGTK
pygtk_windows_installer.exe
But in some cases the installer starts up even if Python is installed. What is the problem here?
For those who just want a simple check if Python is installed and can be executed without going into the registy, in your batch file:
:: Check for Python Installation
python --version 2>NUL
if errorlevel 1 goto errorNoPython
:: Reaching here means Python is installed.
:: Execute stuff...
:: Once done, exit the batch file -- skips executing the errorNoPython section
goto:eof
:errorNoPython
echo.
echo Error^: Python not installed
Your code doesn't branch after the registry query is done. No matter what the first if ERRORLEVEL evaluates to, the next step is always to step into the :NOPYTHON label.
Ed: Here is an example how to make it work. The idea is to add another goto statement which will skip the :NOPYTHON label if desired.
reg query "hkcu\software\Python 2.6"
if ERRORLEVEL 1 GOTO NOPYTHON
goto :HASPYTHON
:NOPYTHON
ActivePython-2.6.4.8-win32-x86.msi
:HASPYTHON
reg query "hklm\SOFTWARE\ActiveState\ActivePerl\" 1>>Output_%date%_%time%.log 2>&1
Here's my method.
The python -V command will return the version number and the find command with the aid of the /v switch will search for the omittance of Python and there's also a normal one without that switch.
#echo off & title %~nx0 & color 5F
goto :DOES_PYTHON_EXIST
:DOES_PYTHON_EXIST
python -V | find /v "Python" >NUL 2>NUL && (goto :PYTHON_DOES_NOT_EXIST)
python -V | find "Python" >NUL 2>NUL && (goto :PYTHON_DOES_EXIST)
goto :EOF
:PYTHON_DOES_NOT_EXIST
echo Python is not installed on your system.
echo Now opeing the download URL.
start "" "https://www.python.org/downloads/windows/"
goto :EOF
:PYTHON_DOES_EXIST
:: This will retrieve Python 3.8.0 for example.
for /f "delims=" %%V in ('python -V') do #set ver=%%V
echo Congrats, %ver% is installed...
goto :EOF

Resources