I am trying to iterate all the registry key in order to find (contains) and delete jre1.5.0_14 values. Is there a way to do it?
The code below just finds jre1.5.0_14 under a specific key! I do want to iterate all of the keys. By the way if clause gets if it is equal to jre1.5.0_14, but it should be if it contains jre1.5.0_14.
Thanks in advance.
Best Regards.
#echo off
setlocal
set KEY_NAME="HKEY_CURRENT_USER\Software\Microsoft\Notepad"
set VALUE_NAME=jre1.5.0_14
FOR /F "skip=2 tokens=3" %%A IN ('REG QUERY %KEY_NAME%') DO if "%%A"=="%VALUE_NAME%" (
#echo %%A
)
I am posting this per your last comment. You (probably) need to go through all the keys, and you don't want /d at the end.
If you know the string is in a specific key, use this (which I think is what you were originally after):
For /f %%a in ('reg query HKCR /f jre1.5.0_14') do (
rem do deletion here
echo %%a &&pause
)
Use the following to look through all the keys. It runs the query on each key and if "hkey" is found in the output (indicating the query turned up something) it writes the line--the location--to regout.txt. The second for loop goes through each line of the file, where you can do a reg delete command.
#echo off
SETLOCAL EnableDelayedExpansion
del regout.txt >NUL 2>&1
for %%a in (HKCU, HKLM, HKU, HKCR) do REG QUERY %%a /f jre1.5.0_14 | FIND /i "hkey" >>regout.txt
for /f %%a in (regout.txt) do (
rem do deletion here
echo %%a &&pause
)
exit
If the file is empty the string wasn't found and you should run regedit in Windows again to confirm it's there.
Edit: Attempt to resolve "The process can not access the file because the file is being used by another process . . .
Try this code: It uses different file names and creates a tmp file with query output, then copies it to txt. The new txt file should definitely be unlocked.
#echo off
SETLOCAL EnableDelayedExpansion
del regout.txt >Nul 2>&1
del regout.tmp >Nul 2>&1
del output.txt >Nul 2>&1
for %%a in (HKCU, HKLM, HKU, HKCR) do REG QUERY %%a /f jre1.5.0_14 | FIND /i "hkey" >>output.tmp
copy output.tmp output.txt /y
for /f %%a in (output.txt) do (
rem do deletion here
echo %%a &&pause
del output.tmp
)
exit
Related
This is irritating. I am trying to incorporate a trigger in a script to run if the major version of Java is 1.6,1.7, etc.
In the line I am trying to set the variable in, I pull the Java version and pipe it to for with tokens and delims identified, resulting in the "do" as a set command for variable %jver%. However, echoing %jver% results in "echo is on". Why wont it set the variable? Everything looks legit until %jver% is used.
Yes, I double the percentages for the script. The code here is for use at the command prompt.
Here is the line:
%systemroot%\system32\java.exe -version 2>&1 | for /f "tokens=1-4 delims=. " %a in ('findstr /i "version"') do (set jver=%~c.%d)
#rao thank you for helping me discover an alternate solution to meet my intent.
My workaround solution to this is the following line:
for /f "tokens=1-4 delims=. " %a in ('java -version 2^>^&1 ^| findstr /i "version"') do (SET JVER=%~c.%d)
Looks this is answered in this post by Patrick Cuff
Here is the solution adding from mentioned original post
#echo off
setlocal
set VERSION6="1.6.0_21"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
#echo Output: %%g
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
#echo Output: %JAVAVER%
I'm using the following code to check whether there is a java process with the specific name.
for /f "tokens=1" %i in ('jps -m ^| find "TouchPosApplication"') do ( EXIT /B )
how can I use this as an if condition rather than iterating. and this command exits the batch code even if there is no process named such. is there any thing wrong with this code.
This is my entire batch script
start start-chrome.bat
for /f "tokens=1" %i in ('jps -m ^| find "TouchPosApplication"') do ( EXIT /B )
ant main
set var=False
start start-chrome.bat
for /f "tokens=1" %%i in ('jps -m ^| find "TouchPosApplication"') do set var=True
if "%var%"=="True" (
Echo Found
) else (
ant main
)
I'm on phone so can't test it but
goto label
will jump to and execute ant main whereas goto finish would end program.
label:
ant main
finish:
PS. I don't recall batch accepting single quotes, which are apostrophes really.
I have a set of files, labeled file1.java, file2.java, ...
I want to pipe the output of the files to .txt files, such as output1.txt, output2.txt, etc. corresponding to each java file.
I know that I can do this by doing:
javac file1.java
java file1 > output1.txt
However, is there a way to do this of all files in a given directory? (instead of doing it manually)
This is a good for loop example:
Command line usage:
for /f %f in ('dir /b c:\') do echo %f
Batch file usage:
for /f %%f in ('dir /b c:\') do echo %%f
if the directory contains files with space in the names, you need to change the delimiter the for /f command is using. for example, you can use the pipe char.
for /f "delims=|" %%f in ('dir /b c:\') do echo %%f
If the directory name itself has a space in the name, you can use the usebackq option on the for:
for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f
And if you need to use output redirection or command piping, use the escape char (^):
for /f "usebackq delims=|" %%f in (dir /b "c:\program files" ^|
findstr /i microsoft) do echo %%f
It shows both command line + batch for loop examples. Then you will have the filenames and can just run some commands on those filenames like javac and java.
Just try to break it down to small tasks:
Get list of files (all or just .class)
Loop through each file and call javac and java on them
Google is your best friend - search for "DOS batch" + what you need
Your job is much easier if you restructure your output file names a bit: file1.java --> file1_output.txt
From the command line:
for %F in (*.java) do #javac "%F" >"%~nF_output.txt"
If in a batch file:
#echo off
for %%F in (*.java) do javac "%%F" >"%%~nF_output.txt"
If you keep your original naming convention, then you must parse out the number form each file name. Very doable, but it doesn't seem worthwhile in your case.
This question already has answers here:
Get java version from batch file
(5 answers)
Closed 9 years ago.
I need to set JAVA_HOME variable using batch script
Tried below codes, but no use.
for /f %%j in ("java.exe") do #echo.%%~dp$PATH:j
the above script gives empty
FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY
"HKLM\Software\JavaSoft\Java Runtime Environment" /v CurrentVersion')
DO set CurVer=%%B
the above script gives 1.6
dir java.exe /B /S
the above script gives C:\Windows\System32 but actually java installed in C:\Program Files\Java\jdk1.6.0_31
I need to set JAVA_HOME as C:\Program Files\Java\jdk1.6.0_31.
How to get this?
I got solution. this gives exactly the installed path.
FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Development Kit" /v CurrentVersion') DO
set CurVer=%%B ECHO %CurVer%
FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Development Kit\%CurVer%" /v JavaHome')
DO set JavaPath=%%B ECHO %JavaPath%
Path to Java 1.6 JavaHome:
#ECHO OFF &SETLOCAL
FOR /F "tokens=2*" %%a IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.6" /v JavaHome') DO set "JavaHome16=%%b"
ECHO %JavaHome16%
Path to Java 1.7 JavaHome:
#ECHO OFF &SETLOCAL
FOR /F "tokens=2*" %%a IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.7" /v JavaHome') DO set "JavaHome17=%%b"
ECHO %JavaHome17%
how about:
setx JAVA_HOME "path/to/it"
get it by doing %JAVA_HOME%
This will work if your JAVA_HOME is configured.save this as someName.bat and run.
echo
where java
pause
OK then use this, By running this will creates a text file where same place you put this bat contains the JAVA path
#ECHO OFF
SET TITLE=JavaHome.bat
TITLE=%TITLE%
SETLOCAL ENABLEDELAYEDEXPANSION
SET JDKBIN=\bin
SET JREBIN=\jre\bin
SET JBINARY=%JREBIN%\java.exe
SET SCRIPTDIR=%~dp0
SET SCRIPTDIR=%SCRIPTDIR:~0,-1%
IF EXIST "%SCRIPTDIR%\javahome.txt" (
ECHO.
ECHO JDK home already set in javahome.txt file.
GOTO :LOCKFILE
)
IF DEFINED JAVA_HOME (
ECHO.
ECHO JAVA_HOME is already set to !JAVA_HOME!
CALL :STRIP "!JAVA_HOME!">"!SCRIPTDIR!\javahome.txt"
ECHO Created !SCRIPTDIR!\javahome.txt file containing JAVA_HOME
GOTO :END
)
SET "dir=%~f0"
:DIRLOOP
CALL :FGETDIR "%dir%"
IF EXIST "%dir%\%JBINARY%" (
ECHO Parent directory search found JAVA_HOME at %dir%
GOTO :SEARCHSET
)
IF "%dir:~-1%" == ":" (
ECHO Parent directory search reached root and "%JBINARY%" was not found.
GOTO :REGISTRY
)
GOTO :DIRLOOP
:SEARCHSET
SET JAVA_HOME=%dir%
ECHO %JAVA_HOME%>javahome.txt
ECHO Created file %SCRIPTDIR%\javahome.txt with value %JAVA_HOME%
GOTO :END
:REGISTRY
:: registry search section
:: runs only when JAVA_HOME not set, file search fails, and javahome.txt doesn't exist
ECHO Searching registry for JAVA_HOME...
ECHO. 2>merged.txt
ECHO. 2>list.txt
ECHO. 2>uniquelist.txt
IF NOT EXIST reg32.txt ECHO. 2>reg32.txt
IF NOT EXIST reg64.txt ECHO. 2>reg64.txt
START /w REGEDIT /e reg32.txt "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\JavaSoft\Java Development Kit"
TYPE reg32.txt | FIND "JavaHome" > merged.txt
START /w REGEDIT /e reg64.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
TYPE reg64.txt | FIND "JavaHome" >> merged.txt
FOR /f "tokens=2 delims==" %%x IN (merged.txt) DO (
CALL :STRIP "%%~x" >>list.txt
)
FOR /F "tokens=* delims= " %%a IN (list.txt) DO (
SET str=%%a
FIND /I ^"!str!^" list.txt>nul
FIND /I ^"!str!^" uniquelist.txt>nul
IF ERRORLEVEL 1 ECHO !str!>>uniquelist.txt
)
:PROMPT
ECHO Select a JDK from the list:
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%y IN (uniquelist.txt) DO (
SET /A COUNT += 1
ECHO !COUNT!: %%~y
)
SET /P NUMBER=Type a number here:
IF "%NUMBER%" GTR "%COUNT%" GOTO :PROMPT
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%z IN (uniquelist.txt) DO (
SET /A COUNT += 1
IF !COUNT!==!NUMBER! (
SET JAVA_HOME=%%~z
)
)
ECHO %JAVA_HOME%>javahome.txt
GOTO CLEANUP
:: batch functions section
:FGETDIR
SET "dir=%~dp1"
SET "dir=%dir:~0,-1%"
EXIT /B 0
:STRIP
REM Strip quotes and extra backslash from string
SET n=%~1
SET n=%n:\\=\%
SET n=%n:"=%
IF NOT "%n%"=="" ECHO %n%
GOTO :EOF
:: cleanup and end
:CLEANUP
REM cleanup of registry search
DEL /Q merged.txt
DEL /Q list.txt
DEL /Q uniquelist.txt
DEL /Q reg32.txt
DEL /Q reg64.txt
GOTO :LOCKFILE
:: if all fails
:FAILED
IF NOT DEFINED JAVA_HOME (
ECHO Error: JAVA_HOME not set in system vars, file search failed, && javahome.txt didn't exist.
GOTO :END
)
:LOCKFILE
ECHO.
SET /P JAVA_HOME=<"%SCRIPTDIR%\javahome.txt"
ECHO The file %SCRIPTDIR%\javahome.txt shows JAVA_HOME to be %JAVA_HOME%
:END
FOR /l %%a in (30,-1,1) do (TITLE %TITLE% -- Closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)
FYI : original reference: https://gist.github.com/djangofan/1445440
I am looking for a batch file snippet that somehow reads the Windows registry and detects which Java JDK is on a Windows system and then asks the user which one they want to use and remembers the choice.
Here is what I have so far... needs some modifications. This script only finds the first JDK... it doesn't handle multiples.
#echo off
SETLOCAL EnableDelayedExpansion
:: findJDK.bat
start /w regedit /e reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
type reg1.txt | find "JavaHome" > reg2.txt
if errorlevel 1 goto ERROR
for /f "tokens=2 delims==" %%x in (reg2.txt) do (
set JavaTemp=%%~x
echo Regedit: JAVA_HOME path : !JavaTemp!
)
if errorlevel 1 goto ERROR
echo.
set JAVA_HOME=%JavaTemp%
set JAVA_HOME=%JAVA_HOME:\\=\%
echo JAVA_HOME was found to be %JAVA_HOME%
goto END
:ERROR
echo reg1.txt is: & type reg1.txt
echo reg2.txt is: & type reg2.txt
echo
:END
del reg2.txt
del reg1.txt
pause>nul
You could check the entries under HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment in the registry
Ok, I finally figured out the answer. It took a while, and this code is really rough, but it works. If someone wants to improve it, I will award them the answer points:
#ECHO off
SET TITLE=detectJDK.bat
TITLE=%TITLE%
SETLOCAL ENABLEDELAYEDEXPANSION
IF EXIST jdkhome.txt (
ECHO JDK home already set in jdkhome.txt file.
GOTO :END
)
ECHO. 2>merged.txt
ECHO. 2>list.txt
ECHO. 2>uniquelist.txt
IF NOT EXIST reg32.txt ECHO. 2>reg32.txt
IF NOT EXIST reg64.txt ECHO. 2>reg64.txt
START /w REGEDIT /e reg32.txt "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\JavaSoft\Java Development Kit"
TYPE reg32.txt | FIND "JavaHome" > merged.txt
START /w REGEDIT /e reg64.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
TYPE reg64.txt | FIND "JavaHome" >> merged.txt
FOR /f "tokens=2 delims==" %%x IN (merged.txt) DO (
CALL :STRIP "%%~x" >>list.txt
)
FOR /F "tokens=* delims= " %%a IN (list.txt) DO (
SET str=%%a
FIND /I ^"!str!^" list.txt>nul
FIND /I ^"!str!^" uniquelist.txt>nul
IF ERRORLEVEL 1 ECHO !str!>>uniquelist.txt
)
ECHO Select a JDK from the list:
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%y IN (uniquelist.txt) DO (
SET /A COUNT += 1
ECHO !COUNT!: %%~y
)
SET /P NUMBER=Type a number here:
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%z IN (uniquelist.txt) DO (
SET /A COUNT += 1
IF !COUNT!==!NUMBER! (
SET JAVA_HOME=%%~z
)
)
ECHO.
ECHO JAVA_HOME was found to be %JAVA_HOME%
ECHO %JAVA_HOME%>jdkhome.txt
GOTO CLEANUP
:CLEANUP
DEL /Q merged.txt
DEL /Q list.txt
DEL /Q uniquelist.txt
DEL /Q reg32.txt
DEL /Q reg64.txt
GOTO :END
:: Strip quotes and extra backslash from string
:STRIP
SET n=%~1
SET n=%n:\\=\%
SET n=%n:"=%
IF NOT "%n%"=="" ECHO %n%
GOTO :EOF
:END
FOR /l %%a in (5,-1,1) do (TITLE %TITLE% -- Closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)
:: end
Do you really need to use the Windows registry? If not, you can check for the existence of the JAVA_HOME and/or JDK_HOME environment variables. If they exist, then they point to the installation directory of the Java install, and you can probably assume that the Java installation they point to is the one the user wants to use.