How to find and replace a string in %PATH% system variable with variables in a batch file?

Alex Johnson

I am trying to upgrade Java with a batch file, and I need to change the PATH system variable to reflect that change. At the beginning of the PATH variable I have

C:\Program Files\Java\jdk1.8.0_51;...

I need to change the jdk value to be jdk1.8.0_60. I'm relatively new to command line and batch files, and so I may be misunderstanding something. Here is what I am trying.

I have a couple variables

jVersion=1.8.0_
javaPath=C:\Program Files\Java
newVersion=60
oldVersion=51

I found something about replacing strings with literal values like so

set PATH=%PATH:1.8.0_51=1.8.0_60%

but I can't get it to work with variables...

set PATH=%%PATH:%jVersion%%oldVersion%=%jVersion%%newVersion%%%

I don't know if you need 2 %'s around the outside, or just one, or !'s. I'm not super confident in my knowledge of delayed expansion. I also don't know if this is possible.

As a bonus, I would really like to be able to take whatever comes after ...\Java\ and replace it with my new value. This would be just in case I don't know the value in the PATH variable for the jdk

Thanks!

EDIT: By using the command call before a modified version of my code I was able to get it to work

call set PATH=%PATH:%jVersion%%oldVersion%=%jVersion%%newVersion%%

I'm still trying to figure out how to make it generic and change whatever comes after ...\jdk to the values I have.

prudviraj

paste the Below Code in a bat file and that should solve the problem

@echo off

setlocal EnableDelayedExpansion
set jVersion=1.8.0_
set "javaPath=C:\Program Files\Java"
set newVersion=60
set oldVersion=51
set PATH=%jVersion%%oldVersion%
echo before path change : !PATH!
set PATH=!PATH:%jVersion%%oldVersion%=%jVersion%%newVersion%!
echo final path change  : !PATH!
pause

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find and replace a string in %PATH% system variable with variables in a batch file?

From Dev

Registry: find and replace part of a path in a batch file

From Dev

Replace " " with "\ " in a file path string with variable expansion

From Dev

String replace with variable in batch

From Dev

How do return a path variable in a batch file?

From Dev

"The system couldn't find the path specified" batch file error

From Dev

How to search and replace a string in environment variable PATH?

From Dev

batch file to replace "=" to "?" in files or variables

From Dev

Windows batch file to find variable string in a html file

From Dev

How can I find and replace string with another string in batch script?

From Dev

How to add a %VAR% to the Windows system PATH within a batch file

From Dev

How to add a %VAR% to the Windows system PATH within a batch file

From Dev

batch file to search and replace a string

From Dev

Replace string % with %% using batch file

From Dev

Replace a string with wildcard in a batch file

From Dev

How in batch file set to variable up level path

From Dev

How to set PATH environment variable in batch file only once on Windows?

From Dev

How to set PATH environment variable in batch file only once on Windows?

From Dev

how to replace string that contains a variable, the next string that contains variables

From Dev

Batch file move command "The system cannot find the path specified." despite correctly identifying the file to be moved

From Dev

How do I use a batch file to recursively replace a string in files?

From Dev

Using a variable for a file path in batch file

From Dev

How to find the current batch file's name and store as a variable?

From Dev

How to Find & Replace Strings with Incrementing Variables in a Text File Using Awk

From Dev

Windows batch file to replace nth line with new string or extract nth line and store string in variable

From Dev

Windows batch file to replace nth line with new string or extract nth line and store string in variable

From Dev

Characters in a batch file string variable

From Dev

Batch File to Replace string in file name and extension

From Dev

How to find whether a non-command file is available in the system path?

Related Related

  1. 1

    How to find and replace a string in %PATH% system variable with variables in a batch file?

  2. 2

    Registry: find and replace part of a path in a batch file

  3. 3

    Replace " " with "\ " in a file path string with variable expansion

  4. 4

    String replace with variable in batch

  5. 5

    How do return a path variable in a batch file?

  6. 6

    "The system couldn't find the path specified" batch file error

  7. 7

    How to search and replace a string in environment variable PATH?

  8. 8

    batch file to replace "=" to "?" in files or variables

  9. 9

    Windows batch file to find variable string in a html file

  10. 10

    How can I find and replace string with another string in batch script?

  11. 11

    How to add a %VAR% to the Windows system PATH within a batch file

  12. 12

    How to add a %VAR% to the Windows system PATH within a batch file

  13. 13

    batch file to search and replace a string

  14. 14

    Replace string % with %% using batch file

  15. 15

    Replace a string with wildcard in a batch file

  16. 16

    How in batch file set to variable up level path

  17. 17

    How to set PATH environment variable in batch file only once on Windows?

  18. 18

    How to set PATH environment variable in batch file only once on Windows?

  19. 19

    how to replace string that contains a variable, the next string that contains variables

  20. 20

    Batch file move command "The system cannot find the path specified." despite correctly identifying the file to be moved

  21. 21

    How do I use a batch file to recursively replace a string in files?

  22. 22

    Using a variable for a file path in batch file

  23. 23

    How to find the current batch file's name and store as a variable?

  24. 24

    How to Find & Replace Strings with Incrementing Variables in a Text File Using Awk

  25. 25

    Windows batch file to replace nth line with new string or extract nth line and store string in variable

  26. 26

    Windows batch file to replace nth line with new string or extract nth line and store string in variable

  27. 27

    Characters in a batch file string variable

  28. 28

    Batch File to Replace string in file name and extension

  29. 29

    How to find whether a non-command file is available in the system path?

HotTag

Archive