0

I am trying to create a Windows batch script that will store the outputs of this command in a variable.

java -XshowSettings:properties 2>&1 | findstr "user.dir"

Output is something like this

user.dir = C:\Program Files (x86)\Java\jre1.8.0_291\bin

I want the following portion C:\Program Files (x86)\Java\jre1.8.0_291\bin to store in a variable in my script.

I've tried using for loop but its not performing the findStr command.

Any assistance would be greatly appreciated.

  • 1
    Isn't `user.dir` just what the current working directory is? Why wouldn't you just use the `%cd%` variable for that? – Squashman Dec 01 '21 at 23:41
  • 1
    In the future, please take the [tour] and read [ask] a good question. Please consider using the search facility before posting a question. The question I linked to shows you how to do this but here is the exact code you would use: `FOR /F "tokens=1,* delims== " %%G IN ('java -XshowSettings:properties -version 2^>^&1^|find /i "user.dir"') DO SET "%%G=%%H"` – Squashman Dec 02 '21 at 00:21
  • Yes sorry user.dir is the current directory...i meant java.home which is directory I want to store in the member variable – Treshauna Wright Dec 02 '21 at 02:37

1 Answers1

0
@echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION

REM STACKOVERFLOW EXTRA CODE BEGIN
if not "%~1"=="fakejava" goto real
echo.xyz.ABC = dontcare 1>&2
echo.user.dir = C:\Program Files (x86)\Java\jre1.8.0_291\bin 1>&2
echo.hello.world = foo\bar 1>&2
goto :EOF
:real
REM STACKOVERFLOW EXTRA CODE END

set myvar=
set javacmd=%0 fakejava
REM Replace the line above with your Java command: java -XshowSettings:properties 


for /F "tokens=1,* delims==" %%A in ('%javacmd% 2^>^&1') do (echo.%%~A|find /I "user.dir" >nul)&&set myvar=%%B

echo myvar=%myvar%
Anders
  • 90,169
  • 12
  • 105
  • 156